-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathMixinPropertiesClassReflectionExtension.php
90 lines (70 loc) · 2.44 KB
/
MixinPropertiesClassReflectionExtension.php
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
<?php declare(strict_types = 1);
namespace PHPStan\Reflection\Mixin;
use PHPStan\Analyser\OutOfClassScope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\VerbosityLevel;
use function array_intersect;
use function count;
final class MixinPropertiesClassReflectionExtension implements PropertiesClassReflectionExtension
{
/** @var array<string, array<string, true>> */
private array $inProcess = [];
/**
* @param string[] $mixinExcludeClasses
*/
public function __construct(private array $mixinExcludeClasses)
{
}
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
{
return $this->findProperty($classReflection, $propertyName) !== null;
}
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
$property = $this->findProperty($classReflection, $propertyName);
if ($property === null) {
throw new ShouldNotHappenException();
}
return $property;
}
private function findProperty(ClassReflection $classReflection, string $propertyName): ?PropertyReflection
{
$mixinTypes = $classReflection->getResolvedMixinTypes();
foreach ($mixinTypes as $type) {
if (count(array_intersect($type->getObjectClassNames(), $this->mixinExcludeClasses)) > 0) {
continue;
}
$typeDescription = $type->describe(VerbosityLevel::typeOnly());
if (isset($this->inProcess[$typeDescription][$propertyName])) {
continue;
}
$this->inProcess[$typeDescription][$propertyName] = true;
if (!$type->hasInstanceProperty($propertyName)->yes()) {
unset($this->inProcess[$typeDescription][$propertyName]);
continue;
}
$property = $type->getInstanceProperty($propertyName, new OutOfClassScope());
unset($this->inProcess[$typeDescription][$propertyName]);
return $property;
}
foreach ($classReflection->getTraits() as $traitClass) {
$methodWithDeclaringClass = $this->findProperty($traitClass, $propertyName);
if ($methodWithDeclaringClass === null) {
continue;
}
return $methodWithDeclaringClass;
}
$parentClass = $classReflection->getParentClass();
while ($parentClass !== null) {
$property = $this->findProperty($parentClass, $propertyName);
if ($property !== null) {
return $property;
}
$parentClass = $parentClass->getParentClass();
}
return null;
}
}