1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wingu\OctopusCore\Reflection; |
4
|
|
|
|
5
|
|
|
use Wingu\OctopusCore\Reflection\Exceptions\RuntimeException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Reflection about a method. |
9
|
|
|
*/ |
10
|
|
|
class ReflectionMethod extends \ReflectionMethod |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
use ReflectionDocCommentTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Gets declaring class. |
17
|
|
|
* |
18
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionClass |
19
|
|
|
*/ |
20
|
33 |
|
public function getDeclaringClass() |
21
|
|
|
{ |
22
|
33 |
|
return new ReflectionClass(parent::getDeclaringClass()->getName()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Gets prototype. |
27
|
|
|
* |
28
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionMethod |
29
|
|
|
*/ |
30
|
3 |
|
public function getPrototype() |
31
|
|
|
{ |
32
|
3 |
|
$prototype = parent::getPrototype(); |
33
|
|
|
|
34
|
3 |
|
return new static($prototype->getDeclaringClass()->getName(), $prototype->getName()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the body of the method. |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
* @throws \Wingu\OctopusCore\Reflection\Exceptions\RuntimeException If the method belongs to an internal class or is abstract. |
42
|
|
|
*/ |
43
|
27 |
|
public function getBody() |
44
|
|
|
{ |
45
|
27 |
|
if ($this->isAbstract() === true) { |
46
|
6 |
|
throw new RuntimeException('Can not get body of an abstract method'); |
47
|
|
|
} |
48
|
|
|
|
49
|
21 |
|
$fileName = $this->getDeclaringClass()->getFileName(); |
50
|
21 |
|
if ($fileName === false) { |
51
|
3 |
|
throw new RuntimeException('Can not get body of a method belonging to an internal class.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
18 |
|
$lines = file($fileName, FILE_IGNORE_NEW_LINES); |
55
|
18 |
|
$lines = array_slice($lines, $this->getStartLine() - 1, ($this->getEndLine() - $this->getStartLine() + 1), |
56
|
18 |
|
true); |
57
|
18 |
|
$lines = implode("\n", $lines); |
58
|
|
|
|
59
|
18 |
|
$firstBracketPos = strpos($lines, '{'); |
60
|
18 |
|
$lastBracketPost = strrpos($lines, '}'); |
61
|
18 |
|
$body = substr($lines, $firstBracketPos + 1, $lastBracketPost - $firstBracketPos - 1); |
62
|
|
|
|
63
|
18 |
|
return trim(rtrim($body), "\n\r"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Gets parameters. |
68
|
|
|
* |
69
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionParameter[] |
70
|
|
|
*/ |
71
|
6 |
|
public function getParameters() |
72
|
|
|
{ |
73
|
6 |
|
$function = array($this->getDeclaringClass()->getName(), $this->getName()); |
74
|
6 |
|
$res = parent::getParameters(); |
75
|
|
|
|
76
|
6 |
|
foreach ($res as $key => $val) { |
77
|
3 |
|
$res[$key] = new ReflectionParameter($function, $val->getName()); |
|
|
|
|
78
|
2 |
|
} |
79
|
|
|
|
80
|
6 |
|
return $res; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Gets extension info. |
85
|
|
|
* |
86
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionExtension |
87
|
|
|
*/ |
88
|
6 |
|
public function getExtension() |
89
|
|
|
{ |
90
|
6 |
|
$extensionName = $this->getExtensionName(); |
91
|
6 |
|
if ($extensionName !== false) { |
92
|
3 |
|
return new ReflectionExtension($extensionName); |
93
|
|
|
} else { |
94
|
3 |
|
return null; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: