1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Benoth\StaticReflection\Reflection; |
4
|
|
|
|
5
|
|
|
class ReflectionMethod extends ReflectionFunctionAbstract |
6
|
|
|
{ |
7
|
|
|
use Parts\VisibilityTrait; |
8
|
|
|
use Parts\StaticTrait; |
9
|
|
|
use Parts\AbstractTrait; |
10
|
|
|
use Parts\FinalTrait; |
11
|
|
|
use Parts\DeclaringClassLikeTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Checks if the method is a constructor. |
15
|
|
|
* |
16
|
|
|
* @return bool |
17
|
|
|
*/ |
18
|
|
|
public function isConstructor() |
19
|
|
|
{ |
20
|
|
|
return $this->getName() === '__construct'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Checks if the method is a destructor. |
25
|
|
|
* |
26
|
|
|
* @return bool |
27
|
|
|
*/ |
28
|
|
|
public function isDestructor() |
29
|
|
|
{ |
30
|
|
|
return $this->getName() === '__destruct'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns the methods prototype. |
35
|
|
|
* |
36
|
|
|
* @throws \ReflectionException If the method does not have a prototype. |
37
|
|
|
* |
38
|
|
|
* @return ReflectionMethod |
39
|
|
|
*/ |
40
|
|
|
public function getPrototype() |
41
|
|
|
{ |
42
|
|
|
$parent = $this->getDeclaringClass(); |
43
|
|
|
$methodName = $this->getName(); |
44
|
|
|
|
45
|
|
|
while (!is_null($parent)) { |
46
|
|
|
if ($parent instanceof ReflectionClass || $parent instanceof ReflectionInterface) { |
47
|
|
|
foreach ($parent->getSelfInterfaces() as $interface) { |
48
|
|
|
if ($interface->hasMethod($methodName)) { |
49
|
|
|
return $interface->getMethod($methodName); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!$parent instanceof ReflectionClass) { |
55
|
|
|
$parent = null; |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$parent = $parent->getParentClass(); |
60
|
|
|
if (is_null($parent)) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($parent->hasMethod($methodName) && $parent->getMethod($methodName)->isAbstract()) { |
65
|
|
|
return $parent->getMethod($methodName); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new \ReflectionException('Method '.$this->getDeclaringClass()->getName().'::'.$this->getName().' does not have a prototype'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getParent() |
73
|
|
|
{ |
74
|
|
|
$class = $this->getDeclaringClass(); |
75
|
|
|
|
76
|
|
|
$methods = []; |
77
|
|
|
foreach ($class->getSelfMethods() as $methodName => $method) { |
78
|
|
|
if ($methodName === $this->getName()) { |
79
|
|
|
$methods[] = $method; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($class instanceof ReflectionClass) { |
84
|
|
|
foreach ($class->getTraitsMethods() as $methodName => $method) { |
85
|
|
|
if ($methodName === $this->getName()) { |
|
|
|
|
86
|
|
|
$methods[] = $method; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (($class instanceof ReflectionClass || $class instanceof \ReflectionClass) && ($class->getParentClass() instanceof ReflectionClass || $class->getParentClass() instanceof \ReflectionClass)) { |
92
|
|
|
foreach ($class->getParentClass()->getMethods() as $methodName => $method) { |
93
|
|
|
if (is_int($methodName)) { |
94
|
|
|
$methodName = $method->getName(); |
95
|
|
|
} |
96
|
|
|
if ($methodName === $this->getName()) { |
97
|
|
|
$methods[] = $method; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($class instanceof ReflectionClass || $class instanceof \ReflectionClass) { |
103
|
|
View Code Duplication |
foreach ($class->getInterfaces() as $interfaceName => $interface) { |
|
|
|
|
104
|
|
|
foreach ($interface->getMethods() as $methodName => $method) { |
105
|
|
|
if ($methodName === $this->getName()) { |
106
|
|
|
$methods[] = $method; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (count($methods) >= 2) { |
113
|
|
|
// $methods[0] is the current method |
114
|
|
|
return $methods[1]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Sets a method to be accessible. |
122
|
|
|
* |
123
|
|
|
* |
124
|
|
|
* @param bool $accessible |
125
|
|
|
* |
126
|
|
|
* @throws \ReflectionException Always... Can't be implemented. |
127
|
|
|
*/ |
128
|
|
|
public function setAccessible($accessible) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
throw new \ReflectionException('StaticReflection can\'t change a method accessibility'); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|