phpstan-drupal
phpstan-drupal copied to clipboard
Stub files override @property for FieldItem classes
(I found/tested this issue with drupal-check 1.4.0)
I wanted to write type-definitions for a custom FieldItem class, so I can use $item->my_custom_field_property
properties in my code. I read in https://github.com/mglaman/phpstan-drupal/issues/359 that @property
annotations should also work. I thought with this I can save the stub file and have everything in one file.
But the current implementation in phpstan-drupal seems to ignore the @property
annotation completely. Here is the minimal example which already generates an error, but is correct from my point of view.
LayoutSectionItem (@property
) generates an error, EntityReferenceItem with sub-file works.
Example:
# foo.php
<?php
/** @var \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem $item */
$section = $item->section;
/** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item */
$target_id = $item->target_id;
$entity = $item->entity;
The format of the @property
annotation is wrong. phpstan requires $variable (docs: https://phpstan.org/writing-php-code/phpdocs-basics#magic-properties), but core uses it without $. (Phpstorm supports both, no visual error.)
Examples:
\Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem
\Drupal\menu_link_content\Entity\MenuLinkContent
https://docs.phpdoc.org/3.0/guide/references/phpdoc/tags/property.html#property-property-read-property-write uses $.
Drupal core is very wrong then. I always thought it needed $
We can add a stubfile here fixing Drupal core with "todo remove after" and link to Drupal core issue for fix there
Core-Issue: https://www.drupal.org/project/drupal/issues/3279502