4
4
5
5
use Composer \Autoload \ClassLoader ;
6
6
use ReflectionClass ;
7
+ use ReflectionClassConstant ;
7
8
use ReflectionMethod ;
9
+ use Reflector ;
8
10
use function array_keys ;
9
11
use function strlen ;
10
12
use function strpos ;
@@ -32,24 +34,40 @@ public function shouldMarkMethodAsUsed(ReflectionMethod $method): ?VirtualUsageD
32
34
return null ;
33
35
}
34
36
35
- $ reflectionClass = $ method ->getDeclaringClass ();
36
- $ methodName = $ method ->getName ();
37
+ return $ this ->shouldMarkMemberAsUsed ($ method );
38
+ }
39
+
40
+ protected function shouldMarkConstantAsUsed (ReflectionClassConstant $ constant ): ?VirtualUsageData
41
+ {
42
+ if (!$ this ->enabled ) {
43
+ return null ;
44
+ }
45
+
46
+ return $ this ->shouldMarkMemberAsUsed ($ constant );
47
+ }
37
48
38
- $ usage = VirtualUsageData::withNote ('Method overrides vendor one, thus is expected to be used by vendor code ' );
49
+ /**
50
+ * @param ReflectionMethod|ReflectionClassConstant $member
51
+ */
52
+ private function shouldMarkMemberAsUsed (Reflector $ member ): ?VirtualUsageData
53
+ {
54
+ $ reflectionClass = $ member ->getDeclaringClass ();
55
+ $ memberString = $ member instanceof ReflectionMethod ? 'Method ' : 'Constant ' ;
56
+ $ usage = VirtualUsageData::withNote ($ memberString . ' overrides vendor one, thus is expected to be used by vendor code ' );
39
57
40
58
do {
41
- if ($ this ->isForeignMethod ($ reflectionClass , $ methodName )) {
59
+ if ($ this ->isForeignMember ($ reflectionClass , $ member )) {
42
60
return $ usage ;
43
61
}
44
62
45
63
foreach ($ reflectionClass ->getInterfaces () as $ interface ) {
46
- if ($ this ->isForeignMethod ($ interface , $ methodName )) {
64
+ if ($ this ->isForeignMember ($ interface , $ member )) {
47
65
return $ usage ;
48
66
}
49
67
}
50
68
51
69
foreach ($ reflectionClass ->getTraits () as $ trait ) {
52
- if ($ this ->isForeignMethod ($ trait , $ methodName )) {
70
+ if ($ this ->isForeignMember ($ trait , $ member )) {
53
71
return $ usage ;
54
72
}
55
73
}
@@ -61,14 +79,19 @@ public function shouldMarkMethodAsUsed(ReflectionMethod $method): ?VirtualUsageD
61
79
}
62
80
63
81
/**
82
+ * @param ReflectionMethod|ReflectionClassConstant $member
64
83
* @param ReflectionClass<object> $reflectionClass
65
84
*/
66
- private function isForeignMethod (
85
+ private function isForeignMember (
67
86
ReflectionClass $ reflectionClass ,
68
- string $ methodName
87
+ Reflector $ member
69
88
): bool
70
89
{
71
- if (!$ reflectionClass ->hasMethod ($ methodName )) {
90
+ if ($ member instanceof ReflectionMethod && !$ reflectionClass ->hasMethod ($ member ->getName ())) {
91
+ return false ;
92
+ }
93
+
94
+ if ($ member instanceof ReflectionClassConstant && !$ reflectionClass ->hasConstant ($ member ->getName ())) {
72
95
return false ;
73
96
}
74
97
0 commit comments