Problem/Motivation

In #3425218: Fix PHPStan errors 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

Issue fork feeds-3456961

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

MegaChriz created an issue. See original summary.

megachriz’s picture

Status: Active » Needs work
Parent issue: » #3425218: Fix PHPStan errors
megachriz’s picture

Status: Needs work » Needs review

I'm leaving this issue open for a few days before merging it.

  • MegaChriz committed 9b60a9bd on 8.x-3.x
    Issue #3456961 by MegaChriz: Added StateFactory for fixing a PHPStan...
megachriz’s picture

Status: Needs review » Fixed

I merged the code!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.