Add StateFactory for fixing a PHPStan issue

Created on 25 June 2024, 3 months ago
Updated 17 July 2024, 2 months ago

Problem/Motivation

In 🐛 Fix PHPStan errors Needs work we're trying to fix all PHPStan issues. However, there is one case that is a bit more challenging to fix there and that is about the State classes needing dependency injection:

------ ---------------------------------------------------------------------- 
  Line   src/Feeds/State/CleanState.php                                        
 ------ ---------------------------------------------------------------------- 
  70     \Drupal calls should be avoided in classes, use dependency injection  
         instead                                                               
  167    \Drupal calls should be avoided in classes, use dependency injection  
         instead                                                               
 ------ ---------------------------------------------------------------------- 
------ ---------------------------------------------------------------------- 
  Line   src/State.php                                                         
 ------ ---------------------------------------------------------------------- 
  155    \Drupal calls should be avoided in classes, use dependency injection  
         instead                                                               
  169    \Drupal calls should be avoided in classes, use dependency injection  
         instead                                                               
 ------ ----------------------------------------------------------------------

In src/Entity/Feed.php, in the method getState() there is also a comment that creating State objects should happen in a factory instead:

/**
   * {@inheritdoc}
   */
  public function getState($stage) {
    if (!isset($this->states[$stage])) {
      $state = $this->getStateStorage()->get($stage);

      if (empty($state)) {
        // @todo move this logic to a factory or alike.
        switch ($stage) {
          case StateInterface::CLEAN:
            $state = new CleanState($this->id());
            break;

          default:
            $state = new State();
            break;
        }
      }

      $this->states[$stage] = $state;
    }
    return $this->states[$stage];
  }

So that is what we should be doing on this issue.

Proposed resolution

Create a StateFactory and a StateFactoryInterface class. Make a service of the StateFactory class and use that in Feed::getState().

Remaining tasks

  • Implement the StateFactory class and declare it as a service.
  • Make use of the service in Feed::getState().

User interface changes

API changes

Data model changes

📌 Task
Status

Fixed

Version

3.0

Component

Code

Created by

🇳🇱Netherlands megachriz

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024