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\File as FileSystemFile; |
18
|
|
|
use phpDocumentor\Reflection\Fqsen; |
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\ContextFactory; |
28
|
|
|
use PhpParser\Comment\Doc; |
29
|
|
|
use PhpParser\Node; |
30
|
|
|
use PhpParser\Node\Stmt\Class_ as ClassNode; |
31
|
|
|
use PhpParser\Node\Stmt\Function_ as FunctionNode; |
32
|
|
|
use PhpParser\Node\Stmt\Interface_ as InterfaceNode; |
33
|
|
|
use PhpParser\Node\Stmt\Namespace_ as NamespaceNode; |
34
|
|
|
use PhpParser\Node\Stmt\Trait_ as TraitNode; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Strategy to create File element from the provided filename. |
38
|
|
|
* This class supports extra middle wares to add extra steps to the creation process. |
39
|
|
|
*/ |
40
|
|
|
final class File extends AbstractFactory implements ProjectFactoryStrategy |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var NodesFactory |
44
|
|
|
*/ |
45
|
|
|
private $nodesFactory; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initializes the object. |
49
|
|
|
* |
50
|
|
|
* @param Middleware[] $middleware |
51
|
|
|
*/ |
52
|
12 |
|
public function __construct(NodesFactory $nodesFactory, $middleware = []) |
53
|
|
|
{ |
54
|
12 |
|
$this->nodesFactory = $nodesFactory; |
55
|
|
|
|
56
|
12 |
|
$lastCallable = function ($command) { |
57
|
8 |
|
return $this->createFile($command); |
58
|
12 |
|
}; |
59
|
|
|
|
60
|
12 |
|
$this->middlewareChain = ChainFactory::createExecutionChain($middleware, $lastCallable); |
|
|
|
|
61
|
12 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns true when the strategy is able to handle the object. |
65
|
|
|
* |
66
|
|
|
* |
67
|
|
|
* @param mixed $file path to check. |
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
|
9 |
|
protected function doCreate($object, StrategyContainer $strategies, Context $context = null) |
85
|
|
|
{ |
86
|
9 |
|
$command = new CreateCommand($object, $strategies); |
87
|
9 |
|
$middlewareChain = $this->middlewareChain; |
88
|
|
|
|
89
|
9 |
|
return $middlewareChain($command); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return FileElement |
94
|
|
|
*/ |
95
|
8 |
|
private function createFile(CreateCommand $command) |
96
|
|
|
{ |
97
|
8 |
|
$file = $command->getFile(); |
98
|
8 |
|
$code = $file->getContents(); |
99
|
8 |
|
$nodes = $this->nodesFactory->create($code); |
100
|
|
|
|
101
|
8 |
|
$contextFactory = new ContextFactory(); |
102
|
8 |
|
$context = $contextFactory->createForNamespace('\\', $code); |
103
|
|
|
|
104
|
8 |
|
$docBlock = $this->createFileDocBlock(null, $command->getStrategies(), $context, $nodes); |
105
|
|
|
|
106
|
8 |
|
$result = new FileElement( |
107
|
8 |
|
$file->md5(), |
108
|
8 |
|
$file->path(), |
109
|
8 |
|
$code, |
110
|
8 |
|
$docBlock |
|
|
|
|
111
|
|
|
); |
112
|
|
|
|
113
|
8 |
|
$this->createElements(new Fqsen('\\'), $nodes, $result, $command->getStrategies()); |
114
|
|
|
|
115
|
8 |
|
return $result; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Node[] $nodes |
120
|
|
|
*/ |
121
|
8 |
|
private function createElements(Fqsen $namespace, $nodes, FileElement $file, StrategyContainer $strategies) |
122
|
|
|
{ |
123
|
8 |
|
$contextFactory = new ContextFactory(); |
124
|
8 |
|
$context = $contextFactory->createForNamespace((string) $namespace, $file->getSource()); |
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 |
|
$file->addNamespace($node->fqsen); |
|
|
|
|
144
|
3 |
|
$this->createElements($node->fqsen, $node->stmts, $file, $strategies); |
|
|
|
|
145
|
3 |
|
break; |
146
|
1 |
|
case TraitNode::class: |
147
|
1 |
|
$strategy = $strategies->findMatching($node); |
148
|
1 |
|
$trait = $strategy->create($node, $strategies, $context); |
149
|
1 |
|
$file->addTrait($trait); |
|
|
|
|
150
|
8 |
|
break; |
151
|
|
|
} |
152
|
|
|
} |
153
|
8 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param Node[] $nodes |
157
|
|
|
* @return null|\phpDocumentor\Reflection\DocBlock |
158
|
|
|
*/ |
159
|
8 |
|
protected function createFileDocBlock( |
160
|
|
|
Doc $docBlock = null, |
|
|
|
|
161
|
|
|
StrategyContainer $strategies = null, |
162
|
|
|
Context $context = null, |
163
|
|
|
$nodes = [] |
164
|
|
|
) { |
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
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: