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\Fqsen; |
20
|
|
|
use phpDocumentor\Reflection\Middleware\ChainFactory; |
21
|
|
|
use phpDocumentor\Reflection\Middleware\Middleware; |
22
|
|
|
use phpDocumentor\Reflection\Php\Factory\File\CreateCommand; |
23
|
|
|
use phpDocumentor\Reflection\Php\File as FileElement; |
24
|
|
|
use phpDocumentor\Reflection\Php\NodesFactory; |
25
|
|
|
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy; |
26
|
|
|
use phpDocumentor\Reflection\Php\StrategyContainer; |
27
|
|
|
use phpDocumentor\Reflection\Types\Context; |
28
|
|
|
use phpDocumentor\Reflection\Types\ContextFactory; |
29
|
|
|
use phpDocumentor\Reflection\Types\NamespaceNodeToContext; |
30
|
|
|
use PhpParser\Comment\Doc; |
31
|
|
|
use PhpParser\Node; |
32
|
|
|
use PhpParser\Node\Stmt\Class_ as ClassNode; |
33
|
|
|
use PhpParser\Node\Stmt\Function_ as FunctionNode; |
34
|
|
|
use PhpParser\Node\Stmt\Interface_ as InterfaceNode; |
35
|
|
|
use PhpParser\Node\Stmt\Namespace_ as NamespaceNode; |
36
|
|
|
use PhpParser\Node\Stmt\Trait_ as TraitNode; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Strategy to create File element from the provided filename. |
40
|
|
|
* This class supports extra middle wares to add extra steps to the creation process. |
41
|
|
|
*/ |
42
|
|
|
final class File extends AbstractFactory implements ProjectFactoryStrategy |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* @var NodesFactory |
46
|
|
|
*/ |
47
|
|
|
private $nodesFactory; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var callable |
51
|
|
|
*/ |
52
|
|
|
private $middlewareChain; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Initializes the object. |
56
|
|
|
* |
57
|
|
|
* @param Middleware[] $middleware |
58
|
|
|
*/ |
59
|
12 |
|
public function __construct(NodesFactory $nodesFactory, $middleware = []) |
60
|
|
|
{ |
61
|
12 |
|
$this->nodesFactory = $nodesFactory; |
62
|
|
|
|
63
|
12 |
|
$lastCallable = function ($command) { |
64
|
8 |
|
return $this->createFile($command); |
65
|
12 |
|
}; |
66
|
|
|
|
67
|
12 |
|
$this->middlewareChain = ChainFactory::createExecutionChain($middleware, $lastCallable); |
68
|
12 |
|
} |
69
|
|
|
|
70
|
1 |
|
public function matches($file): bool |
71
|
|
|
{ |
72
|
1 |
|
return $file instanceof FileSystemFile; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Creates an File out of the given object. |
77
|
|
|
* |
78
|
|
|
* Since an object might contain other objects that need to be converted the $factory is passed so it can be |
79
|
|
|
* used to create nested Elements. |
80
|
|
|
* |
81
|
|
|
* @param FileSystemFile $object path to the file to convert to an File object. |
82
|
|
|
* @param StrategyContainer $strategies used to convert nested objects. |
83
|
|
|
* @return File |
84
|
|
|
*/ |
85
|
9 |
|
protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null) |
86
|
|
|
{ |
87
|
9 |
|
$command = new CreateCommand($object, $strategies); |
88
|
9 |
|
$middlewareChain = $this->middlewareChain; |
89
|
|
|
|
90
|
9 |
|
return $middlewareChain($command); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return FileElement |
95
|
|
|
*/ |
96
|
8 |
|
private function createFile(CreateCommand $command) |
97
|
|
|
{ |
98
|
8 |
|
$file = $command->getFile(); |
99
|
8 |
|
$code = $file->getContents(); |
100
|
8 |
|
$nodes = $this->nodesFactory->create($code); |
101
|
|
|
|
102
|
8 |
|
$docBlock = $this->createFileDocBlock(null, $command->getStrategies(), null, $nodes); |
103
|
|
|
|
104
|
8 |
|
$result = new FileElement( |
105
|
8 |
|
$file->md5(), |
106
|
8 |
|
$file->path(), |
107
|
8 |
|
$code, |
108
|
8 |
|
$docBlock |
109
|
|
|
); |
110
|
|
|
|
111
|
8 |
|
$this->createElements($nodes, $result, $command->getStrategies(), null); |
112
|
|
|
|
113
|
8 |
|
return $result; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param Node[] $nodes |
118
|
|
|
*/ |
119
|
8 |
|
private function createElements( |
120
|
|
|
array $nodes, |
121
|
|
|
FileElement $file, |
122
|
|
|
StrategyContainer $strategies, |
123
|
|
|
?Context $context |
124
|
|
|
): void { |
125
|
8 |
|
foreach ($nodes as $node) { |
126
|
8 |
|
switch (get_class($node)) { |
127
|
8 |
|
case ClassNode::class: |
|
|
|
|
128
|
2 |
|
$strategy = $strategies->findMatching($node); |
129
|
2 |
|
$class = $strategy->create($node, $strategies, $context); |
130
|
2 |
|
$file->addClass($class); |
|
|
|
|
131
|
2 |
|
break; |
132
|
6 |
|
case FunctionNode::class: |
133
|
1 |
|
$strategy = $strategies->findMatching($node); |
134
|
1 |
|
$function = $strategy->create($node, $strategies, $context); |
135
|
1 |
|
$file->addFunction($function); |
|
|
|
|
136
|
1 |
|
break; |
137
|
5 |
|
case InterfaceNode::class: |
138
|
1 |
|
$strategy = $strategies->findMatching($node); |
139
|
1 |
|
$interface = $strategy->create($node, $strategies, $context); |
140
|
1 |
|
$file->addInterface($interface); |
|
|
|
|
141
|
1 |
|
break; |
142
|
4 |
|
case NamespaceNode::class: |
143
|
3 |
|
$context = (new NamespaceNodeToContext())($node); |
144
|
3 |
|
$file->addNamespace($node->fqsen); |
|
|
|
|
145
|
3 |
|
$this->createElements($node->stmts, $file, $strategies, $context); |
|
|
|
|
146
|
3 |
|
break; |
147
|
1 |
|
case TraitNode::class: |
148
|
1 |
|
$strategy = $strategies->findMatching($node); |
149
|
1 |
|
$trait = $strategy->create($node, $strategies, $context); |
150
|
1 |
|
$file->addTrait($trait); |
|
|
|
|
151
|
8 |
|
break; |
152
|
|
|
} |
153
|
|
|
} |
154
|
8 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param Node[] $nodes |
158
|
|
|
*/ |
159
|
8 |
|
protected function createFileDocBlock( |
160
|
|
|
?Doc $docBlock = null, |
|
|
|
|
161
|
|
|
?StrategyContainer $strategies = null, |
162
|
|
|
?Context $context = null, |
163
|
|
|
array $nodes = [] |
164
|
|
|
): ?DocBlockInstance { |
165
|
8 |
|
$node = current($nodes); |
166
|
8 |
|
if (!$node instanceof Node) { |
167
|
|
|
return null; |
168
|
|
|
} |
169
|
|
|
|
170
|
8 |
|
$comments = $node->getAttribute('comments'); |
171
|
8 |
|
if (!is_array($comments) || empty($comments)) { |
172
|
5 |
|
return null; |
173
|
|
|
} |
174
|
|
|
|
175
|
3 |
|
$found = 0; |
176
|
3 |
|
$firstDocBlock = null; |
177
|
3 |
|
foreach ($comments as $comment) { |
178
|
3 |
|
if (!$comment instanceof Doc) { |
179
|
1 |
|
continue; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
//If current node cannot have a docblock return the first comment as docblock for the file. |
183
|
|
|
if (!( |
184
|
3 |
|
$node instanceof ClassNode || |
185
|
2 |
|
$node instanceof FunctionNode || |
186
|
2 |
|
$node instanceof InterfaceNode || |
187
|
3 |
|
$node instanceof TraitNode |
188
|
|
|
)) { |
189
|
2 |
|
return $this->createDocBlock($strategies, $comment, $context); |
190
|
|
|
} |
191
|
|
|
|
192
|
1 |
|
++$found; |
193
|
1 |
|
if ($firstDocBlock === null) { |
194
|
1 |
|
$firstDocBlock = $comment; |
195
|
1 |
|
} elseif ($found > 2) { |
196
|
1 |
|
break; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
1 |
|
if ($found === 2) { |
201
|
1 |
|
return $this->createDocBlock($strategies, $firstDocBlock, $context); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return null; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.