9
9
use PhpParser \Node ;
10
10
use PHPStan \Analyser \Scope ;
11
11
use PHPStan \PhpDoc \ResolvedPhpDocBlock ;
12
+ use PHPStan \PhpDocParser \Ast \PhpDoc \PhpDocTagNode ;
12
13
use PHPStan \Reflection \ClassReflection ;
13
14
use ReflectionAttribute ;
15
+ use ReflectionClass ;
16
+ use ReflectionException ;
14
17
use function array_key_exists ;
15
18
use function preg_match ;
16
19
@@ -39,12 +42,12 @@ protected function doProcessNode(ClassReflection $reflection, Node\Stmt\Class_ $
39
42
40
43
$ phpDoc = $ reflection ->getResolvedPhpDoc ();
41
44
if ($ phpDoc instanceof ResolvedPhpDocBlock) {
42
- if ($ this ->isAnnotated ($ phpDoc, ' @FieldType ' ) && preg_match ('/category\s?=\s?@Translation/ ' , $ phpDoc ->getPhpDocString ()) === 1 ) {
45
+ if ($ this ->hasFieldTypeAnnotation ($ phpDoc ) && preg_match ('/category\s?=\s?@Translation/ ' , $ phpDoc ->getPhpDocString ()) === 1 ) {
43
46
$ errors [] = self ::DEPRECATION_MESSAGE ;
44
47
}
45
48
}
46
49
47
- $ fieldTypeAttributes = $ this ->getPluginAttribute ($ reflection, FieldType::class );
50
+ $ fieldTypeAttributes = $ this ->getFieldTypeAttributes ($ reflection );
48
51
if ($ fieldTypeAttributes instanceof ReflectionAttribute) {
49
52
$ arguments = $ fieldTypeAttributes ->getArguments ();
50
53
if (array_key_exists ('category ' , $ arguments ) && $ arguments ['category ' ] instanceof TranslatableMarkup) {
@@ -54,4 +57,47 @@ protected function doProcessNode(ClassReflection $reflection, Node\Stmt\Class_ $
54
57
55
58
return $ errors ;
56
59
}
60
+
61
+ /**
62
+ * Checks whether a PHP doc block contains a field type annotation.
63
+ *
64
+ * @param \PHPStan\PhpDoc\ResolvedPhpDocBlock $phpDoc
65
+ * The PHP doc block object.
66
+ *
67
+ * @return bool
68
+ * True if it does, otherwise false.
69
+ */
70
+ private function hasFieldTypeAnnotation (ResolvedPhpDocBlock $ phpDoc ): bool
71
+ {
72
+ foreach ($ phpDoc ->getPhpDocNodes () as $ docNode ) {
73
+ foreach ($ docNode ->children as $ childNode ) {
74
+ if (($ childNode instanceof PhpDocTagNode) && $ childNode ->name === '@FieldType ' ) {
75
+ return true ;
76
+ }
77
+ }
78
+ }
79
+
80
+ return false ;
81
+ }
82
+
83
+ /**
84
+ * Checks whether a given class has a field type attribute.
85
+ *
86
+ * @param \PHPStan\Reflection\ClassReflection $reflection
87
+ * The class reflection object.
88
+ *
89
+ * @return ReflectionAttribute|null
90
+ * The attribute, or null.
91
+ */
92
+ private function getFieldTypeAttributes (ClassReflection $ reflection ): ?ReflectionAttribute
93
+ {
94
+ try {
95
+ $ nativeReflection = new ReflectionClass ($ reflection ->getName ());
96
+ $ attribute = $ nativeReflection ->getAttributes (FieldType::class);
97
+ } catch (ReflectionException $ e ) {
98
+ return null ;
99
+ }
100
+
101
+ return $ attribute [0 ] ?? null ;
102
+ }
57
103
}
0 commit comments