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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* @see https://github.com/zendframework/zend-server for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-server/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Server\Reflection;
/**
* Method Reflection
*/
class ReflectionMethod extends AbstractFunction
{
/**
* Doc block inherit tag for search
*/
const INHERIT_TAG = '{@inheritdoc}';
/**
* Parent class name
* @var string
*/
protected $class;
/**
* Parent class reflection
* @var ReflectionClass|\ReflectionClass
*/
protected $classReflection;
/**
* Constructor
*
* @param ReflectionClass $class
* @param \ReflectionMethod $r
* @param string $namespace
* @param array $argv
*/
public function __construct(ReflectionClass $class, \ReflectionMethod $r, $namespace = null, $argv = [])
{
$this->classReflection = $class;
$this->reflection = $r;
$classNamespace = $class->getNamespace();
// Determine namespace
if (! empty($namespace)) {
$this->setNamespace($namespace);
} elseif (! empty($classNamespace)) {
$this->setNamespace($classNamespace);
}
// Determine arguments
if (is_array($argv)) {
$this->argv = $argv;
}
// If method call, need to store some info on the class
$this->class = $class->getName();
// Perform some introspection
$this->reflect();
}
/**
* Return the reflection for the class that defines this method
*
* @return \Zend\Server\Reflection\ReflectionClass
*/
public function getDeclaringClass()
{
return $this->classReflection;
}
/**
* Wakeup from serialization
*
* Reflection needs explicit instantiation to work correctly. Re-instantiate
* reflection object on wakeup.
*
* @return void
*/
public function __wakeup()
{
$this->classReflection = new ReflectionClass(
new \ReflectionClass($this->class),
$this->getNamespace(),
$this->getInvokeArguments()
);
$this->reflection = new \ReflectionMethod($this->classReflection->getName(), $this->getName());
}
/**
* {@inheritdoc}
*/
protected function reflect()
{
$docComment = $this->reflection->getDocComment();
if (strpos($docComment, self::INHERIT_TAG) !== false) {
$this->docComment = $this->fetchRecursiveDocComment();
}
parent::reflect();
}
/**
* Fetch all doc comments for inherit values
*
* @return string
*/
private function fetchRecursiveDocComment()
{
$currentMethodName = $this->reflection->getName();
$docCommentList[] = $this->reflection->getDocComment();
// fetch all doc blocks for method from parent classes
$docCommentFetched = $this->fetchRecursiveDocBlockFromParent($this->classReflection, $currentMethodName);
if ($docCommentFetched) {
$docCommentList = array_merge($docCommentList, $docCommentFetched);
}
// fetch doc blocks from interfaces
$interfaceReflectionList = $this->classReflection->getInterfaces();
foreach ($interfaceReflectionList as $interfaceReflection) {
if (! $interfaceReflection->hasMethod($currentMethodName)) {
continue;
}
$docCommentList[] = $interfaceReflection->getMethod($currentMethodName)->getDocComment();
}
$normalizedDocCommentList = array_map(
function ($docComment) {
$docComment = str_replace('/**', '', $docComment);
$docComment = str_replace('*/', '', $docComment);
return $docComment;
},
$docCommentList
);
$docComment = '/**' . implode(PHP_EOL, $normalizedDocCommentList) . '*/';
return $docComment;
}
/**
* Fetch recursive doc blocks from parent classes
*
* @param \ReflectionClass $reflectionClass
* @param string $methodName
*
* @return array|void
*/
private function fetchRecursiveDocBlockFromParent($reflectionClass, $methodName)
{
$docComment = [];
$parentReflectionClass = $reflectionClass->getParentClass();
if (! $parentReflectionClass) {
return;
}
if (! $parentReflectionClass->hasMethod($methodName)) {
return;
}
$methodReflection = $parentReflectionClass->getMethod($methodName);
$docCommentLast = $methodReflection->getDocComment();
$docComment[] = $docCommentLast;
if ($this->isInherit($docCommentLast)) {
if ($docCommentFetched = $this->fetchRecursiveDocBlockFromParent($parentReflectionClass, $methodName)) {
$docComment = array_merge($docComment, $docCommentFetched);
}
}
return $docComment;
}
/**
* Return true if doc block inherit from parent or interface
*
* @param string $docComment
*
* @return bool
*/
private function isInherit($docComment)
{
$isInherit = strpos($docComment, self::INHERIT_TAG) !== false;
return $isInherit;
}
}