Skip to content

Commit c9fafc5

Browse files
authored
Merge pull request #244 from eiriksm/fix/fielditemlistinterface-allowed
Allow magic methods for variables of type FieldItemListInterface
2 parents 958911d + 6c185cb commit c9fafc5

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/Reflection/EntityFieldsViaMagicReflectionExtension.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use PHPStan\Reflection\ClassReflection;
66
use PHPStan\Reflection\PropertiesClassReflectionExtension;
77
use PHPStan\Reflection\PropertyReflection;
8+
use PHPStan\TrinaryLogic;
9+
use PHPStan\Type\ObjectType;
810

911
/**
1012
* Allows field access via magic methods
@@ -32,7 +34,7 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
3234
// Content entities have magical __get... so it is kind of true.
3335
return true;
3436
}
35-
if ($reflection->implementsInterface('Drupal\Core\Field\FieldItemListInterface')) {
37+
if (self::classObjectIsSuperOfFieldItemList($reflection)->yes()) {
3638
return FieldItemListPropertyReflection::canHandleProperty($classReflection, $propertyName);
3739
}
3840

@@ -45,10 +47,22 @@ public function getProperty(ClassReflection $classReflection, string $propertyNa
4547
if ($reflection->implementsInterface('Drupal\Core\Entity\EntityInterface')) {
4648
return new EntityFieldReflection($classReflection, $propertyName);
4749
}
48-
if ($reflection->implementsInterface('Drupal\Core\Field\FieldItemListInterface')) {
50+
if (self::classObjectIsSuperOfFieldItemList($reflection)->yes()) {
4951
return new FieldItemListPropertyReflection($classReflection, $propertyName);
5052
}
5153

5254
throw new \LogicException($classReflection->getName() . "::$propertyName should be handled earlier.");
5355
}
56+
57+
protected static function classObjectIsSuperOfFieldItemList(\ReflectionClass $reflection) : TrinaryLogic
58+
{
59+
$classObject = new ObjectType($reflection->getName());
60+
$interfaceObject = self::getFieldItemListInterfaceObject();
61+
return $interfaceObject->isSuperTypeOf($classObject);
62+
}
63+
64+
protected static function getFieldItemListInterfaceObject() : ObjectType
65+
{
66+
return new ObjectType('Drupal\Core\Field\FieldItemListInterface');
67+
}
5468
}

tests/src/Reflection/EntityFieldsViaMagicReflectionExtensionTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ public function dataHasProperty(): \Generator
6767
];
6868
yield 'field item list: value' => [
6969
\Drupal\Core\Field\FieldItemList::class,
70-
'target_id',
70+
'value',
71+
true,
72+
];
73+
yield 'field item list_interface: value' => [
74+
\Drupal\Core\Field\FieldItemListInterface::class,
75+
'value',
7176
true,
7277
];
7378
// @todo support more proeprties.

0 commit comments

Comments
 (0)