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
|
|
|
* @copyright 2010-2018 Mike van Riel<[email protected]> |
11
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
12
|
|
|
* @link http://phpdoc.org |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace phpDocumentor\Reflection\Php\Factory; |
16
|
|
|
|
17
|
|
|
use phpDocumentor\Reflection\DocBlock as DocBlockInstance; |
18
|
|
|
use phpDocumentor\Reflection\File as FileSystemFile; |
19
|
|
|
use phpDocumentor\Reflection\Middleware\ChainFactory; |
20
|
|
|
use phpDocumentor\Reflection\Middleware\Middleware; |
21
|
|
|
use phpDocumentor\Reflection\Php\Factory\File\CreateCommand; |
22
|
|
|
use phpDocumentor\Reflection\Php\File as FileElement; |
23
|
|
|
use phpDocumentor\Reflection\Php\NodesFactory; |
24
|
|
|
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy; |
25
|
|
|
use phpDocumentor\Reflection\Php\StrategyContainer; |
26
|
|
|
use phpDocumentor\Reflection\Types\Context; |
27
|
|
|
use phpDocumentor\Reflection\Types\NamespaceNodeToContext; |
28
|
|
|
use PhpParser\Comment\Doc; |
29
|
|
|
use PhpParser\Node; |
30
|
|
|
use PhpParser\Node\Stmt\Class_ as ClassNode; |
31
|
|
|
use PhpParser\Node\Stmt\Const_ as ConstantNode; |
32
|
|
|
use PhpParser\Node\Stmt\Function_ as FunctionNode; |
33
|
|
|
use PhpParser\Node\Stmt\Interface_ as InterfaceNode; |
34
|
|
|
use PhpParser\Node\Stmt\Namespace_ as NamespaceNode; |
35
|
|
|
use PhpParser\Node\Stmt\Trait_ as TraitNode; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Strategy to create File element from the provided filename. |
39
|
|
|
* This class supports extra middle wares to add extra steps to the creation process. |
40
|
|
|
*/ |
41
|
|
|
final class File extends AbstractFactory implements ProjectFactoryStrategy |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var NodesFactory |
45
|
|
|
*/ |
46
|
|
|
private $nodesFactory; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var callable |
50
|
|
|
*/ |
51
|
|
|
private $middlewareChain; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Initializes the object. |
55
|
|
|
* |
56
|
|
|
* @param Middleware[] $middleware |
57
|
|
|
*/ |
58
|
13 |
|
public function __construct(NodesFactory $nodesFactory, $middleware = []) |
59
|
|
|
{ |
60
|
13 |
|
$this->nodesFactory = $nodesFactory; |
61
|
|
|
|
62
|
13 |
|
$lastCallable = function ($command) { |
63
|
9 |
|
return $this->createFile($command); |
64
|
13 |
|
}; |
65
|
|
|
|
66
|
13 |
|
$this->middlewareChain = ChainFactory::createExecutionChain($middleware, $lastCallable); |
67
|
13 |
|
} |
68
|
|
|
|
69
|
1 |
|
public function matches($file) : bool |
70
|
|
|
{ |
71
|
1 |
|
return $file instanceof FileSystemFile; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Creates an File out of the given object. |
76
|
|
|
* |
77
|
|
|
* Since an object might contain other objects that need to be converted the $factory is passed so it can be |
78
|
|
|
* used to create nested Elements. |
79
|
|
|
* |
80
|
|
|
* @param FileSystemFile $object path to the file to convert to an File object. |
81
|
|
|
* @param StrategyContainer $strategies used to convert nested objects. |
82
|
|
|
* @return File |
83
|
|
|
*/ |
84
|
10 |
|
protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null) |
85
|
|
|
{ |
86
|
10 |
|
$command = new CreateCommand($object, $strategies); |
87
|
10 |
|
$middlewareChain = $this->middlewareChain; |
88
|
|
|
|
89
|
10 |
|
return $middlewareChain($command); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return FileElement |
94
|
|
|
*/ |
95
|
9 |
|
private function createFile(CreateCommand $command) |
96
|
|
|
{ |
97
|
9 |
|
$file = $command->getFile(); |
98
|
9 |
|
$code = $file->getContents(); |
99
|
9 |
|
$nodes = $this->nodesFactory->create($code); |
100
|
|
|
|
101
|
9 |
|
$docBlock = $this->createFileDocBlock(null, $command->getStrategies(), null, $nodes); |
102
|
|
|
|
103
|
9 |
|
$result = new FileElement( |
104
|
9 |
|
$file->md5(), |
105
|
9 |
|
$file->path(), |
106
|
9 |
|
$code, |
107
|
9 |
|
$docBlock |
108
|
|
|
); |
109
|
|
|
|
110
|
9 |
|
$this->createElements($nodes, $result, $command->getStrategies(), null); |
111
|
|
|
|
112
|
9 |
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param Node[] $nodes |
117
|
|
|
*/ |
118
|
9 |
|
private function createElements( |
119
|
|
|
array $nodes, |
120
|
|
|
FileElement $file, |
121
|
|
|
StrategyContainer $strategies, |
122
|
|
|
?Context $context |
123
|
|
|
) : void { |
124
|
9 |
|
foreach ($nodes as $node) { |
125
|
9 |
|
switch (get_class($node)) { |
126
|
9 |
|
case ClassNode::class: |
127
|
2 |
|
$strategy = $strategies->findMatching($node); |
128
|
2 |
|
$class = $strategy->create($node, $strategies, $context); |
129
|
2 |
|
$file->addClass($class); |
|
|
|
|
130
|
2 |
|
break; |
131
|
7 |
|
case ConstantNode::class: |
132
|
1 |
|
$constants = new GlobalConstantIterator($node); |
|
|
|
|
133
|
1 |
|
foreach ($constants as $constant) { |
134
|
1 |
|
$strategy = $strategies->findMatching($constant); |
135
|
1 |
|
$constant = $strategy->create($constant, $strategies, $context); |
136
|
1 |
|
$file->addConstant($constant); |
|
|
|
|
137
|
|
|
} |
138
|
1 |
|
break; |
139
|
6 |
|
case FunctionNode::class: |
140
|
1 |
|
$strategy = $strategies->findMatching($node); |
141
|
1 |
|
$function = $strategy->create($node, $strategies, $context); |
142
|
1 |
|
$file->addFunction($function); |
|
|
|
|
143
|
1 |
|
break; |
144
|
5 |
|
case InterfaceNode::class: |
145
|
1 |
|
$strategy = $strategies->findMatching($node); |
146
|
1 |
|
$interface = $strategy->create($node, $strategies, $context); |
147
|
1 |
|
$file->addInterface($interface); |
|
|
|
|
148
|
1 |
|
break; |
149
|
4 |
|
case NamespaceNode::class: |
150
|
3 |
|
$context = (new NamespaceNodeToContext())($node); |
151
|
3 |
|
$file->addNamespace($node->fqsen); |
|
|
|
|
152
|
3 |
|
$this->createElements($node->stmts, $file, $strategies, $context); |
|
|
|
|
153
|
3 |
|
break; |
154
|
1 |
|
case TraitNode::class: |
155
|
1 |
|
$strategy = $strategies->findMatching($node); |
156
|
1 |
|
$trait = $strategy->create($node, $strategies, $context); |
157
|
1 |
|
$file->addTrait($trait); |
|
|
|
|
158
|
9 |
|
break; |
159
|
|
|
} |
160
|
|
|
} |
161
|
9 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param Node[] $nodes |
165
|
|
|
*/ |
166
|
9 |
|
protected function createFileDocBlock( |
167
|
|
|
?Doc $docBlock = null, |
|
|
|
|
168
|
|
|
?StrategyContainer $strategies = null, |
169
|
|
|
?Context $context = null, |
170
|
|
|
array $nodes = [] |
171
|
|
|
) : ?DocBlockInstance { |
172
|
9 |
|
$node = current($nodes); |
173
|
9 |
|
if (!$node instanceof Node) { |
174
|
|
|
return null; |
175
|
|
|
} |
176
|
|
|
|
177
|
9 |
|
$comments = $node->getAttribute('comments'); |
178
|
9 |
|
if (!is_array($comments) || empty($comments)) { |
179
|
6 |
|
return null; |
180
|
|
|
} |
181
|
|
|
|
182
|
3 |
|
$found = 0; |
183
|
3 |
|
$firstDocBlock = null; |
184
|
3 |
|
foreach ($comments as $comment) { |
185
|
3 |
|
if (!$comment instanceof Doc) { |
186
|
1 |
|
continue; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// If current node cannot have a docblock return the first comment as docblock for the file. |
190
|
|
|
if (!( |
191
|
3 |
|
$node instanceof ConstantNode || |
192
|
3 |
|
$node instanceof ClassNode || |
193
|
2 |
|
$node instanceof FunctionNode || |
194
|
2 |
|
$node instanceof InterfaceNode || |
195
|
3 |
|
$node instanceof TraitNode |
196
|
|
|
)) { |
197
|
2 |
|
return $this->createDocBlock($strategies, $comment, $context); |
198
|
|
|
} |
199
|
|
|
|
200
|
1 |
|
++$found; |
201
|
1 |
|
if ($firstDocBlock === null) { |
202
|
1 |
|
$firstDocBlock = $comment; |
203
|
1 |
|
} elseif ($found > 2) { |
204
|
1 |
|
break; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
1 |
|
if ($found === 2) { |
209
|
1 |
|
return $this->createDocBlock($strategies, $firstDocBlock, $context); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return null; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.