Fix PHPStan L2 error "Variable $foo in PHPDoc tag @var does not exist. "

Created on 21 November 2022, almost 3 years ago
Updated 13 November 2023, almost 2 years ago

Problem/Motivation

Amongst the "new" errors found when running PHPStan on level 2 is: Variable $foo in PHPDoc tag @var does not exist.

This child-issue exists to fix all of those.

Steps to reproduce

- Run PHPStan on level 2 and see the above error amongst all others.

Proposed resolution

- Solve all of the the above mentioned reported errors.
- Run PHPStan on level 2 and don't see the above mentioned error any more.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 5 hours ago

Created by

πŸ‡³πŸ‡±Netherlands spokje

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡±Netherlands spokje
  • First commit to issue fork.
  • Merge request !13021Resolve #3322821 "Fix phpstan l2" β†’ (Open) created by mstrelan
  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Updated steps to reproduce. Rerolled as an MR. Hiding patches.

    While I agree it would be nice to use early returns and not assign variables inside if statements, I think it makes sense why phpstan behaves the way it does. If the condition is falsey then the variable will not match the type we're hinting. We can only hint it once we the condition evaluates to true.

    Happy to work on refactoring the methods but I fear it would need to be split many more times to satisfy reviewers.

  • Pipeline finished with Success
    12 days ago
    Total: 1229s
    #575242
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Not sure I follow, just looking at the first file core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php isn't it correct there as that's where $revision is set?

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    @smustgrave

    Well loadRevision can return null, so in that case the @var doc would be wrong. But that is kind of moot because $revision is not used outside the if statement. Either way, phpstan doesn't like it the way it is.

    It's worth also noting that loadRevision returns a RevisionableInterface, but invokeFieldMethod and doDeleteRevisionFieldItems expect a ContentEntityInterface, so we need to tell phpstan that we've got the latter.

    To avoid the @var tag we could write it like this:

      /**
       * {@inheritdoc}
       */
      public function deleteRevision($revision_id) {
        $revision = $this->loadRevision($revision_id);
    
        // @todo throw an exception if there is no revision at this point.
        if (!$revision instanceof ContentEntityInterface) {
          return;
        }
    
        // Prevent deletion if this is the default revision.
        if ($revision->isDefaultRevision()) {
          throw new EntityStorageException('Default revision can not be deleted');
        }
    
        $this->invokeFieldMethod('deleteRevision', $revision);
        $this->doDeleteRevisionFieldItems($revision);
        $this->invokeHook('revision_delete', $revision);
      }
    
Production build 0.71.5 2024