forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodPrototypeFinder.php
More file actions
126 lines (111 loc) · 3.72 KB
/
MethodPrototypeFinder.php
File metadata and controls
126 lines (111 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Methods;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\MethodPrototypeReflection;
use PHPStan\Reflection\Native\NativeMethodReflection;
use PHPStan\Reflection\Php\PhpClassReflectionExtension;
use PHPStan\Reflection\Php\PhpMethodReflection;
use function is_bool;
use function strtolower;
#[AutowiredService]
final class MethodPrototypeFinder
{
public function __construct(
private PhpVersion $phpVersion,
private PhpClassReflectionExtension $phpClassReflectionExtension,
)
{
}
/**
* Finds the prototype method that a class method should be validated against.
* Returns two prototypes with different purposes:
* - Signature prototype: Used for validating method signature (parameters, return type, ...).
* - Inheritance prototype: Used for validating inheritance rules (final keyword, override attribute, ...).
* Also, return a bool to precise if the visibility of the prototype needs to be respected.
*
* @return array{ExtendedMethodReflection, ClassReflection, bool, ExtendedMethodReflection, ClassReflection}|null
*/
public function findPrototype(ClassReflection $classReflection, string $methodName): ?array
{
foreach ($classReflection->getImmediateInterfaces() as $immediateInterface) {
if ($immediateInterface->hasNativeMethod($methodName)) {
$method = $immediateInterface->getNativeMethod($methodName);
return [$method, $method->getDeclaringClass(), true, $method, $method->getDeclaringClass()];
}
}
if ($this->phpVersion->supportsAbstractTraitMethods()) {
foreach ($classReflection->getTraits(true) as $trait) {
$nativeTraitReflection = $trait->getNativeReflection();
if (!$nativeTraitReflection->hasMethod($methodName)) {
continue;
}
$methodReflection = $nativeTraitReflection->getMethod($methodName);
$isAbstract = $methodReflection->isAbstract();
if ($isAbstract) {
$declaringTrait = $trait->getNativeMethod($methodName)->getDeclaringClass();
$prototype = $this->phpClassReflectionExtension->createUserlandMethodReflection(
$trait,
$classReflection,
$methodReflection,
$declaringTrait->getName(),
);
return [
$prototype,
$declaringTrait,
false,
$prototype,
$declaringTrait,
];
}
}
}
$parentClass = $classReflection->getParentClass();
if ($parentClass === null) {
return null;
}
if (!$parentClass->hasNativeMethod($methodName)) {
return null;
}
$method = $parentClass->getNativeMethod($methodName);
if ($method->isPrivate()) {
return null;
}
$declaringClass = $method->getDeclaringClass();
if ($declaringClass->hasConstructor()) {
if ($method->getName() === $declaringClass->getConstructor()->getName()) {
$prototype = $method->getPrototype();
if ($prototype instanceof PhpMethodReflection || $prototype instanceof MethodPrototypeReflection || $prototype instanceof NativeMethodReflection) {
$abstract = $prototype->isAbstract();
if (is_bool($abstract)) {
if (!$abstract) {
return null;
}
} elseif (!$abstract->yes()) {
return null;
}
}
} elseif (strtolower($methodName) === '__construct') {
return null;
}
}
$prototype = $method;
if (strtolower($method->getName()) === '__construct') {
foreach ($parentClass->getInterfaces() as $interface) {
if ($interface->hasNativeMethod($method->getName())) {
$prototype = $interface->getNativeMethod($method->getName());
break;
}
}
}
return [
$prototype,
$prototype->getDeclaringClass(),
true,
$method,
$declaringClass,
];
}
}