2
2
3
3
namespace mglaman \PHPStanDrupal \Reflection ;
4
4
5
+ use Drupal \Core \Config \Entity \ConfigEntityInterface ;
6
+ use Drupal \Core \Entity \ContentEntityInterface ;
7
+ use Drupal \Core \Entity \EntityInterface ;
8
+ use Drupal \Core \Field \FieldItemListInterface ;
5
9
use PHPStan \Reflection \ClassReflection ;
6
10
use PHPStan \Reflection \PropertyReflection ;
11
+ use PHPStan \Reflection \ReflectionProvider ;
7
12
use PHPStan \TrinaryLogic ;
8
13
use PHPStan \Type \MixedType ;
9
14
use PHPStan \Type \ObjectType ;
@@ -23,50 +28,63 @@ class EntityFieldReflection implements PropertyReflection
23
28
/** @var string */
24
29
private $ propertyName ;
25
30
26
- public function __construct (ClassReflection $ declaringClass , string $ propertyName )
31
+ private ReflectionProvider $ reflectionProvider ;
32
+
33
+ public function __construct (ClassReflection $ declaringClass , string $ propertyName , ReflectionProvider $ reflectionProvider )
27
34
{
28
35
$ this ->declaringClass = $ declaringClass ;
29
36
$ this ->propertyName = $ propertyName ;
37
+ $ this ->reflectionProvider = $ reflectionProvider ;
30
38
}
31
39
32
40
public function getReadableType (): Type
33
41
{
34
42
if ($ this ->propertyName === 'original ' ) {
35
- if ($ this ->declaringClass -> isSubclassOf ( ' Drupal\Core\Entity\ContentEntityInterface ' )) {
36
- $ objectType = ' Drupal\Core\Entity\ ContentEntityInterface' ;
37
- } elseif ($ this ->declaringClass -> isSubclassOf ( ' Drupal\Core\Config\Entity\ConfigEntityInterface ' )) {
38
- $ objectType = ' Drupal\Core\Config\Entity\ ConfigEntityInterface' ;
43
+ if ($ this ->isContentEntityType ( )) {
44
+ $ objectType = ContentEntityInterface::class ;
45
+ } elseif ($ this ->isConfigEntityType ( )) {
46
+ $ objectType = ConfigEntityInterface::class ;
39
47
} else {
40
- $ objectType = ' Drupal\Core\Entity\ EntityInterface' ;
48
+ $ objectType = EntityInterface::class ;
41
49
}
42
50
return new ObjectType ($ objectType );
43
51
}
44
52
45
- if ($ this ->declaringClass -> isSubclassOf ( ' Drupal\Core\Entity\ContentEntityInterface ' )) {
53
+ if ($ this ->isContentEntityType ( )) {
46
54
// Assume the property is a field.
47
- return new ObjectType (' Drupal\Core\Field\ FieldItemListInterface' );
55
+ return new ObjectType (FieldItemListInterface::class );
48
56
}
49
57
50
58
return new MixedType ();
51
59
}
52
60
61
+ private function isContentEntityType (): bool
62
+ {
63
+ return $ this ->declaringClass ->isSubclassOfClass ($ this ->reflectionProvider ->getClass (ContentEntityInterface::class));
64
+ }
65
+
66
+ private function isConfigEntityType (): bool
67
+ {
68
+ return $ this ->declaringClass ->isSubclassOfClass ($ this ->reflectionProvider ->getClass (ConfigEntityInterface::class));
69
+ }
70
+
53
71
public function getWritableType (): Type
54
72
{
55
73
if ($ this ->propertyName === 'original ' ) {
56
- if ($ this ->declaringClass -> isSubclassOf ( ' Drupal\Core\Entity\ContentEntityInterface ' )) {
57
- $ objectType = ' Drupal\Core\Entity\ ContentEntityInterface' ;
58
- } elseif ($ this ->declaringClass -> isSubclassOf ( ' Drupal\Core\Config\Entity\ConfigEntityInterface ' )) {
59
- $ objectType = ' Drupal\Core\Config\Entity\ ConfigEntityInterface' ;
74
+ if ($ this ->isContentEntityType ( )) {
75
+ $ objectType = ContentEntityInterface::class ;
76
+ } elseif ($ this ->isConfigEntityType ( )) {
77
+ $ objectType = ConfigEntityInterface::class ;
60
78
} else {
61
- $ objectType = ' Drupal\Core\Entity\ EntityInterface' ;
79
+ $ objectType = EntityInterface::class ;
62
80
}
63
81
return new ObjectType ($ objectType );
64
82
}
65
83
66
84
// @todo Drupal allows $entity->field_myfield = 'string'; does this break that?
67
- if ($ this ->declaringClass -> isSubclassOf ( ' Drupal\Core\Entity\ContentEntityInterface ' )) {
85
+ if ($ this ->isContentEntityType ( )) {
68
86
// Assume the property is a field.
69
- return new ObjectType (' Drupal\Core\Field\ FieldItemListInterface' );
87
+ return new ObjectType (FieldItemListInterface::class );
70
88
}
71
89
72
90
return new MixedType ();
0 commit comments