Skip to content

Node is not subtype of NodeInterface.. says phpstan. #927

@donquixote

Description

@donquixote

Bug report

When I use an inline @var doc to describe an entity which was only type-hinted from the entity interface, phpstan says the entity class is not a subtype of the entity interface.

This happens with Drupal entity classes, but not with regular old classes and interfaces like \Reflector and \ReflectionClass.

So I suspect it is a side effect from phpstan-drupal.

Code snippet that reproduces the problem

use Drupal\node\NodeInterface;
class C {
  /**
   * @param \Drupal\node\NodeInterface $node
   *
   * @return mixed
   */
  public function foo(NodeInterface $node): mixed {
    /** @var \Drupal\node\Entity\Node $node */
    return $node;
  }
}
 ------ ------------------------------------------------------------------------------------------------------------ 
  Line   src/Service/AdvancedEntityAutocompleteMatcher.php                                                           
 ------ ------------------------------------------------------------------------------------------------------------ 
  **     PHPDoc tag @var with type Drupal\node\Entity\Node is not subtype of native type Drupal\node\NodeInterface.  
         🪪  varTag.nativeType                                                                                       
 ------ ------------------------------------------------------------------------------------------------------------

Why would I do this?

I have a project with some magic __set() assign like $entity->x = $y. The entity is type-hinted using an entity interface, and phpstan (level 2) complains that the property does not exist.

For now I want a stop-gap solution that makes phpstan happy but does not change the code itself, because this part is not really tested yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions