1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of phpDocumentor. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @author Mike van Riel <[email protected]> |
11
|
|
|
* @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com) |
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
13
|
|
|
* @link http://phpdoc.org |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace phpDocumentor\Descriptor\Builder\Reflector; |
17
|
|
|
|
18
|
|
|
use phpDocumentor\Descriptor\ArgumentDescriptor; |
19
|
|
|
use phpDocumentor\Descriptor\MethodDescriptor; |
20
|
|
|
use phpDocumentor\Reflection\DocBlock\Tags\Param; |
21
|
|
|
use phpDocumentor\Reflection\Php\Argument; |
22
|
|
|
use phpDocumentor\Reflection\Php\Method; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Assembles a MethodDescriptor from a MethodReflector. |
26
|
|
|
*/ |
27
|
|
|
class MethodAssembler extends AssemblerAbstract |
28
|
|
|
{ |
29
|
|
|
/** @var ArgumentAssembler */ |
30
|
|
|
protected $argumentAssembler; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initializes this assembler with its dependencies. |
34
|
|
|
*/ |
35
|
3 |
|
public function __construct(ArgumentAssembler $argumentAssembler) |
36
|
|
|
{ |
37
|
3 |
|
$this->argumentAssembler = $argumentAssembler; |
38
|
3 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Creates a Descriptor from the provided data. |
42
|
|
|
* |
43
|
|
|
* @param Method $data |
44
|
|
|
* |
45
|
|
|
* @return MethodDescriptor |
46
|
|
|
*/ |
47
|
3 |
|
public function create($data) |
48
|
|
|
{ |
49
|
3 |
|
$methodDescriptor = new MethodDescriptor(); |
50
|
3 |
|
$methodDescriptor->setNamespace( |
51
|
3 |
|
substr( |
52
|
|
|
(string) $data->getFqsen(), |
53
|
3 |
|
0, |
54
|
3 |
|
-strlen($data->getName()) - 4 |
55
|
3 |
|
) |
56
|
|
|
); |
57
|
3 |
|
$this->mapReflectorToDescriptor($data, $methodDescriptor); |
58
|
|
|
|
59
|
|
|
$this->assembleDocBlock($data->getDocBlock(), $methodDescriptor); |
60
|
|
|
$this->addArguments($data, $methodDescriptor); |
61
|
|
|
$this->addVariadicArgument($data, $methodDescriptor); |
62
|
|
|
|
63
|
3 |
|
return $methodDescriptor; |
64
|
|
|
} |
65
|
3 |
|
|
66
|
3 |
|
/** |
67
|
3 |
|
* Maps the fields to the reflector to the descriptor. |
68
|
3 |
|
*/ |
69
|
3 |
|
protected function mapReflectorToDescriptor(Method $reflector, MethodDescriptor $descriptor): void |
70
|
3 |
|
{ |
71
|
3 |
|
$descriptor->setFullyQualifiedStructuralElementName($reflector->getFqsen()); |
72
|
3 |
|
$descriptor->setName($reflector->getName()); |
73
|
3 |
|
$descriptor->setVisibility((string) $reflector->getVisibility() ?: 'public'); |
74
|
|
|
$descriptor->setFinal($reflector->isFinal()); |
75
|
|
|
$descriptor->setAbstract($reflector->isAbstract()); |
76
|
|
|
$descriptor->setStatic($reflector->isStatic()); |
77
|
|
|
$descriptor->setLine($reflector->getLocation()->getLineNumber()); |
78
|
3 |
|
$descriptor->setReturnType($reflector->getReturnType()); |
79
|
|
|
} |
80
|
3 |
|
|
81
|
3 |
|
/** |
82
|
|
|
* Adds the reflected Arguments to the Descriptor. |
83
|
3 |
|
*/ |
84
|
|
|
protected function addArguments(Method $reflector, MethodDescriptor $descriptor): void |
85
|
|
|
{ |
86
|
|
|
foreach ($reflector->getArguments() as $argument) { |
87
|
|
|
$this->addArgument($argument, $descriptor); |
88
|
3 |
|
} |
89
|
|
|
} |
90
|
3 |
|
|
91
|
|
|
/** |
92
|
3 |
|
* Adds a single reflected Argument to the Method Descriptor. |
93
|
3 |
|
*/ |
94
|
|
|
protected function addArgument(Argument $argument, MethodDescriptor $descriptor): void |
95
|
|
|
{ |
96
|
3 |
|
$params = $descriptor->getTags()->get('param', []); |
97
|
|
|
|
98
|
3 |
|
if (!$this->argumentAssembler->getBuilder()) { |
99
|
3 |
|
$this->argumentAssembler->setBuilder($this->builder); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$argumentDescriptor = $this->argumentAssembler->create($argument, $params); |
103
|
|
|
|
104
|
|
|
$descriptor->addArgument($argumentDescriptor->getName(), $argumentDescriptor); |
105
|
3 |
|
} |
106
|
|
|
|
107
|
3 |
|
/** |
108
|
1 |
|
* Checks if there is a variadic argument in the `@param` tags and adds it to the list of Arguments in |
109
|
|
|
* the Descriptor unless there is already one present. |
110
|
|
|
*/ |
111
|
2 |
|
protected function addVariadicArgument(Method $data, MethodDescriptor $methodDescriptor): void |
112
|
|
|
{ |
113
|
|
|
if (!$data->getDocBlock()) { |
114
|
2 |
|
return; |
115
|
2 |
|
} |
116
|
1 |
|
|
117
|
|
|
$paramTags = $data->getDocBlock()->getTagsByName('param'); |
118
|
|
|
|
119
|
1 |
|
/** @var Param $lastParamTag */ |
120
|
1 |
|
$lastParamTag = end($paramTags); |
121
|
|
|
if (!$lastParamTag) { |
122
|
1 |
|
return; |
123
|
|
|
} |
124
|
1 |
|
|
125
|
1 |
|
if ($lastParamTag->isVariadic() |
126
|
1 |
|
&& array_key_exists($lastParamTag->getVariableName(), $methodDescriptor->getArguments()->getAll()) |
127
|
1 |
|
) { |
128
|
1 |
|
$types = $lastParamTag->getType(); |
129
|
1 |
|
|
130
|
|
|
$argument = new ArgumentDescriptor(); |
131
|
1 |
|
$argument->setName($lastParamTag->getVariableName()); |
132
|
|
|
$argument->setType($types); |
133
|
1 |
|
$argument->setDescription($lastParamTag->getDescription()); |
|
|
|
|
134
|
|
|
$argument->setLine($methodDescriptor->getLine()); |
135
|
|
|
$argument->setVariadic(true); |
136
|
|
|
|
137
|
|
|
$methodDescriptor->getArguments()->set($argument->getName(), $argument); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: