-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathClassChildAnalyzer.php
More file actions
124 lines (104 loc) · 4.13 KB
/
ClassChildAnalyzer.php
File metadata and controls
124 lines (104 loc) · 4.13 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
<?php
declare(strict_types=1);
namespace Rector\FamilyTree\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\Php\PhpMethodReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Rector\PhpParser\AstResolver;
final readonly class ClassChildAnalyzer
{
public function __construct(
private AstResolver $astResolver
) {
}
/**
* Look both parent class and interface, yes, all PHP interface methods are abstract
*/
public function hasAbstractParentClassMethod(ClassReflection $classReflection, string $methodName): bool
{
$parentClassMethods = $this->resolveParentClassMethods($classReflection, $methodName);
if ($parentClassMethods === []) {
return false;
}
foreach ($parentClassMethods as $parentClassMethod) {
if ($parentClassMethod->isAbstract()) {
return true;
}
}
return false;
}
/**
* @api downgrade
*/
public function resolveParentClassMethodReturnType(ClassReflection $classReflection, string $methodName): Type
{
$parentClassMethods = $this->resolveParentClassMethods($classReflection, $methodName);
if ($parentClassMethods === []) {
return new MixedType();
}
foreach ($parentClassMethods as $parentClassMethod) {
// for downgrade purpose on __toString
// @see https://3v4l.org/kdcEh#v7.4.33
// @see https://github.com/phpstan/phpstan-src/commit/3854cbc5748a7cb51ee0b86ceffe29bd0564bc98
if ($parentClassMethod->getDeclaringClass()->isBuiltIn() || $methodName !== '__toString') {
$nativeReturnType = $this->resolveNativeType($parentClassMethod);
} else {
$nativeReturnType = $this->resolveToStringNativeTypeFromAstResolver($parentClassMethod);
}
if (! $nativeReturnType instanceof MixedType) {
return $nativeReturnType;
}
}
return new MixedType();
}
private function resolveNativeType(PhpMethodReflection $phpMethodReflection): Type
{
$extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors($phpMethodReflection->getVariants());
return $extendedParametersAcceptor->getNativeReturnType();
}
private function resolveToStringNativeTypeFromAstResolver(PhpMethodReflection $phpMethodReflection): Type
{
$classReflection = $phpMethodReflection->getDeclaringClass();
$class = $this->astResolver->resolveClassFromClassReflection($classReflection);
if ($class instanceof ClassLike) {
$classMethod = $class->getMethod($phpMethodReflection->getName());
if ($classMethod instanceof ClassMethod && !$classMethod->returnType instanceof Node) {
return new MixedType();
}
}
return new StringType();
}
/**
* @return PhpMethodReflection[]
*/
private function resolveParentClassMethods(ClassReflection $classReflection, string $methodName): array
{
if ($classReflection->hasNativeMethod($methodName) && $classReflection->getNativeMethod(
$methodName
)->isPrivate()) {
return [];
}
$parentClassMethods = [];
$parents = [...$classReflection->getParents(), ...$classReflection->getInterfaces()];
foreach ($parents as $parent) {
if (! $parent->hasNativeMethod($methodName)) {
continue;
}
$methodReflection = $parent->getNativeMethod($methodName);
if (! $methodReflection instanceof PhpMethodReflection) {
continue;
}
$methodDeclaringMethodClass = $methodReflection->getDeclaringClass();
if ($methodDeclaringMethodClass->getName() === $parent->getName()) {
$parentClassMethods[] = $methodReflection;
}
}
return $parentClassMethods;
}
}