-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathNetteObjectClassReflectionExtension.php
More file actions
107 lines (87 loc) · 3.24 KB
/
NetteObjectClassReflectionExtension.php
File metadata and controls
107 lines (87 loc) · 3.24 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
<?php declare(strict_types = 1);
namespace PHPStan\Reflection\Nette;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Type\CallableType;
class NetteObjectClassReflectionExtension implements MethodsClassReflectionExtension, PropertiesClassReflectionExtension
{
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
{
if (!$this->inheritsFromNetteObject($classReflection->getNativeReflection())) {
return false;
}
if ($classReflection->hasNativeMethod($propertyName)) {
return true;
}
$getterMethod = $this->getMethodByProperty($classReflection, $propertyName);
if ($getterMethod === null) {
return false;
}
if ($getterMethod->isStatic()) {
return false;
}
return $getterMethod->isPublic();
}
/**
* @param \PHPStan\Reflection\ClassReflection $classReflection
* @param string $propertyName
* @return \PHPStan\Reflection\MethodReflection|null
*/
private function getMethodByProperty(ClassReflection $classReflection, string $propertyName)
{
$getterMethodName = sprintf('get%s', ucfirst($propertyName));
if (!$classReflection->hasNativeMethod($getterMethodName)) {
return null;
}
return $classReflection->getNativeMethod($getterMethodName);
}
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
if ($classReflection->hasNativeMethod($propertyName)) {
return new NetteObjectPropertyReflection($classReflection, new CallableType());
}
/** @var \PHPStan\Reflection\MethodReflection $getterMethod */
$getterMethod = $this->getMethodByProperty($classReflection, $propertyName);
return new NetteObjectPropertyReflection($classReflection, $getterMethod->getReturnType());
}
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
$traitNames = $this->getTraitNames($classReflection->getNativeReflection());
if (!in_array(\Nette\SmartObject::class, $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 $class
* @return string[]
*/
private function getTraitNames(\ReflectionClass $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;
}
private function inheritsFromNetteObject(\ReflectionClass $class): bool
{
while (($class = $class->getParentClass()) !== false) {
if ($class->getName() === 'Nette\Object') {
return true;
}
}
return false;
}
}