-
Notifications
You must be signed in to change notification settings - Fork 34
/
NetteObjectClassReflectionExtension.php
112 lines (94 loc) · 3.4 KB
/
NetteObjectClassReflectionExtension.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php declare(strict_types = 1);
namespace PHPStan\Reflection\Nette;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnum;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use function array_merge;
use function array_unique;
use function array_values;
use function in_array;
use function sprintf;
use function strlen;
use function substr;
use function ucfirst;
class NetteObjectClassReflectionExtension implements MethodsClassReflectionExtension, PropertiesClassReflectionExtension
{
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
{
if (!$this->inheritsFromNetteObject($classReflection->getNativeReflection())) {
return false;
}
$getterMethod = $this->getMethodByProperty($classReflection, $propertyName);
if ($getterMethod === null) {
return false;
}
if ($getterMethod->isStatic()) {
return false;
}
return $getterMethod->isPublic();
}
private function getMethodByProperty(ClassReflection $classReflection, string $propertyName): ?MethodReflection
{
$getterMethodName = sprintf('get%s', ucfirst($propertyName));
if (!$classReflection->hasNativeMethod($getterMethodName)) {
return null;
}
return $classReflection->getNativeMethod($getterMethodName);
}
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
/** @var MethodReflection $getterMethod */
$getterMethod = $this->getMethodByProperty($classReflection, $propertyName);
return new NetteObjectPropertyReflection($classReflection, ParametersAcceptorSelector::selectSingle($getterMethod->getVariants())->getReturnType());
}
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
$traitNames = $this->getTraitNames($classReflection->getNativeReflection());
if (!in_array('Nette\SmartObject', $traitNames, true) && !$this->inheritsFromNetteObject($classReflection->getNativeReflection())) {
return false;
}
if (substr($methodName, 0, 2) !== 'on' || strlen($methodName) <= 2) {
return false;
}
return $classReflection->hasNativeProperty($methodName) && $classReflection->getNativeProperty($methodName)->isPublic();
}
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
{
return new NetteObjectEventListenerMethodReflection($methodName, $classReflection);
}
/**
* @param ReflectionClass|ReflectionEnum $class
* @return string[]
*/
private function getTraitNames($class): array
{
$traitNames = $class->getTraitNames();
while ($class->getParentClass() !== false) {
$traitNames = array_values(array_unique(array_merge($traitNames, $class->getParentClass()->getTraitNames())));
$class = $class->getParentClass();
}
return $traitNames;
}
/**
* @param ReflectionClass|ReflectionEnum $class
*/
private function inheritsFromNetteObject($class): bool
{
$class = $class->getParentClass();
while ($class !== false) {
if (in_array($class->getName(), [ // @phpstan-ignore-line
'Nette\Object',
'Nette\LegacyObject',
], true)) {
return true;
}
$class = $class->getParentClass();
}
return false;
}
}