mglaman / phpstan-drupal

Extension for PHPStan to allow analysis of Drupal code.
https://phpstan-drupal.mglaman.dev/
MIT License
198 stars 76 forks source link

Add @mixin to FieldItemListInterface to explain that properties of its first item are being forwarded #782

Open ceesgeene opened 4 months ago

ceesgeene commented 4 months ago

Feature request

Using @mixin it is possible to explain that instances of FieldItemListInterface forward the properties its items:

/**
 * @template T of FieldItemInterface
 * @extends ListInterface<T>
 * @property mixed $value
 * @mixin T
 */
interface FieldItemListInterface extends ListInterface {
}

This would allow for things like:

/** @var \Drupal\Core\Field\FieldItemListInterface<\Drupal\text\Plugin\Field\FieldType\TextWithSummaryItem> $node_body */
$node_body = $node->get('body');

$body = $node_body->processed;

Unfortunately @mixin also forwards methods. Hopefully @mixin-properties will be introduced, or @mixin will auto-detect what is being forwarded:

phpstan/phpstan #11259: Fine-grained mixin support

mglaman commented 4 months ago

Unfortunately @mixin also forwards methods

What would be the consequence? That BodyItem methods would appear as valid on the list?

ceesgeene commented 4 months ago

Yes, so for example phpstan incorrectly assumes $node->get('body')->mainPropertyName() is valid.

mglaman commented 4 months ago

Ah, yeah. It'd be great for some kind of mixin that only supported certain magic methods