Skip to content

Commit 55d38eb

Browse files
committed
Allow @Property annotations on Entity classes
- Updates EntityFieldsViaMagicReflectionExtensionTest to test annotated properties are skipped in the same way as explicit properties. - Adds an annotation to ReflectionEntityTest fixture - No integration test to make sure annotations are actually respected, not sure if this is actually needed?
1 parent 83444da commit 55d38eb

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

src/Reflection/EntityFieldsViaMagicReflectionExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class EntityFieldsViaMagicReflectionExtension implements PropertiesClassReflecti
2020

2121
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
2222
{
23-
if ($classReflection->hasNativeProperty($propertyName)) {
23+
if ($classReflection->hasNativeProperty($propertyName) || array_key_exists($propertyName, $classReflection->getPropertyTags())) {
2424
// Let other parts of PHPStan handle this.
2525
return false;
2626
}

tests/fixtures/drupal/modules/phpstan_fixtures/src/Entity/ReflectionEntityTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Drupal\Core\Field\BaseFieldDefinition;
88
use Drupal\user\UserInterface;
99

10+
/**
11+
* @property \Drupal\Core\Field\EntityReferenceFieldItemListInterface $user_id
12+
*/
1013
final class ReflectionEntityTest extends ContentEntityBase {
1114

1215
public static function baseFieldDefinitions(EntityTypeInterface $entity_type)

tests/src/Reflection/EntityFieldsViaMagicReflectionExtensionTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Drupal\Core\Field\FieldItemListInterface;
77
use Drupal\entity_test\Entity\EntityTest;
88
use Drupal\module_installer_config_test\Entity\TestConfigType;
9+
use Drupal\phpstan_fixtures\Entity\ReflectionEntityTest;
910
use mglaman\PHPStanDrupal\Reflection\EntityFieldsViaMagicReflectionExtension;
1011
use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait;
1112
use PHPStan\Testing\PHPStanTestCase;
@@ -53,6 +54,12 @@ public function dataHasProperty(): \Generator
5354
'foobar',
5455
false
5556
];
57+
yield 'annotated properties are skipped on content entities' => [
58+
// @phpstan-ignore-next-line
59+
ReflectionEntityTest::class,
60+
'user_id',
61+
false
62+
];
5663
// @todo really entity is only supported on EntityFieldItemList
5764
yield 'field item list: entity' => [
5865
\Drupal\Core\Field\FieldItemList::class,

0 commit comments

Comments
 (0)