|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace PHPSA\Definition; |
|
7
|
|
|
|
|
8
|
|
|
use Exception; |
|
9
|
|
|
use PHPSA\AliasManager; |
|
10
|
|
|
use PHPSA\Compiler; |
|
11
|
|
|
use PHPSA\Context; |
|
12
|
|
|
use PhpParser\Node; |
|
13
|
|
|
use RuntimeException; |
|
14
|
|
|
|
|
15
|
|
|
class FileParser |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \PhpParser\Parser |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $parser; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \PhpParser\NodeTraverser |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $nodeTraverser; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Compiler |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $compiler; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param \PhpParser\Parser $parser |
|
34
|
|
|
* @param Compiler $compiler |
|
35
|
|
|
*/ |
|
36
|
25 |
|
public function __construct(\PhpParser\Parser $parser, Compiler $compiler) |
|
37
|
|
|
{ |
|
38
|
25 |
|
$this->nodeTraverser = new \PhpParser\NodeTraverser(); |
|
39
|
25 |
|
$this->nodeTraverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver); |
|
40
|
|
|
|
|
41
|
25 |
|
$this->parser = $parser; |
|
42
|
25 |
|
$this->compiler = $compiler; |
|
43
|
25 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $filepath |
|
47
|
|
|
* @param Context $context |
|
48
|
|
|
* @throws RuntimeException when filepath is not readable |
|
49
|
|
|
*/ |
|
50
|
25 |
|
public function parserFile($filepath, Context $context) |
|
51
|
|
|
{ |
|
52
|
25 |
|
$context->setFilepath($filepath); |
|
53
|
|
|
|
|
54
|
|
|
try { |
|
55
|
25 |
|
if (!is_readable($filepath)) { |
|
56
|
|
|
throw new RuntimeException('File ' . $filepath . ' is not readable'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
25 |
|
$context->debug('<comment>Precompile: ' . $filepath . '.</comment>'); |
|
60
|
|
|
|
|
61
|
25 |
|
$code = file_get_contents($filepath); |
|
62
|
25 |
|
$astTree = $this->parser->parse($code); |
|
63
|
|
|
|
|
64
|
25 |
|
$this->nodeTraverser->traverse($astTree); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
25 |
|
$context->aliasManager = new AliasManager(); |
|
67
|
25 |
|
$namespace = null; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Step 1 Precompile |
|
71
|
|
|
*/ |
|
72
|
25 |
|
foreach ($astTree as $topStatement) { |
|
|
|
|
|
|
73
|
25 |
|
if ($topStatement instanceof Node\Stmt\Namespace_) { |
|
74
|
|
|
/** |
|
75
|
|
|
* Namespace block can be created without NS name |
|
76
|
|
|
*/ |
|
77
|
25 |
|
if ($topStatement->name) { |
|
78
|
25 |
|
$namespace = $topStatement->name->toString(); |
|
79
|
25 |
|
$context->aliasManager->setNamespace($namespace); |
|
80
|
25 |
|
} |
|
81
|
|
|
|
|
82
|
25 |
|
if ($topStatement->stmts) { |
|
|
|
|
|
|
83
|
25 |
|
$this->parseTopDefinitions($topStatement->stmts, $context->aliasManager, $filepath); |
|
|
|
|
|
|
84
|
25 |
|
} |
|
85
|
25 |
|
} else { |
|
86
|
|
|
if (is_array($topStatement)) { |
|
87
|
|
|
$this->parseTopDefinitions($topStatement, $context->aliasManager, $filepath); |
|
88
|
|
|
} else { |
|
89
|
|
|
$this->parseTopDefinitions($astTree, $context->aliasManager, $filepath); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
25 |
|
} |
|
93
|
|
|
|
|
94
|
25 |
|
$context->clear(); |
|
95
|
25 |
|
} catch (\PhpParser\Error $e) { |
|
96
|
|
|
$context->syntaxError($e, $filepath); |
|
97
|
|
|
} catch (Exception $e) { |
|
98
|
|
|
$context->output->writeln("<error>{$e->getMessage()}</error>"); |
|
99
|
|
|
} |
|
100
|
25 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param Node\Stmt|Node\Stmt[] $topStatement |
|
104
|
|
|
* @param AliasManager $aliasManager |
|
105
|
|
|
* @param string $filepath |
|
106
|
|
|
*/ |
|
107
|
25 |
|
protected function parseTopDefinitions($topStatement, AliasManager $aliasManager, $filepath) |
|
108
|
|
|
{ |
|
109
|
25 |
|
foreach ($topStatement as $statement) { |
|
|
|
|
|
|
110
|
25 |
|
if ($statement instanceof Node\Stmt\Use_) { |
|
111
|
|
|
if (count($statement->uses) > 0) { |
|
112
|
|
|
foreach ($statement->uses as $use) { |
|
113
|
|
|
$aliasManager->add($use->name->parts); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
25 |
|
} elseif ($statement instanceof Node\Stmt\GroupUse) { |
|
117
|
|
|
if (count($statement->uses) > 0) { |
|
118
|
|
|
foreach ($statement->uses as $use) { |
|
119
|
|
|
$aliasManager->add($statement->prefix . $use->name->parts); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
25 |
|
} elseif ($statement instanceof Node\Stmt\Trait_) { |
|
123
|
|
|
$definition = new TraitDefinition($statement->name, $statement); |
|
124
|
|
|
$definition->setFilepath($filepath); |
|
125
|
|
|
$definition->setNamespace($aliasManager->getNamespace()); |
|
126
|
|
|
$definition->precompile(); |
|
127
|
|
|
|
|
128
|
|
|
$this->compiler->addTrait($definition); |
|
129
|
25 |
|
} elseif ($statement instanceof Node\Stmt\Class_) { |
|
130
|
25 |
|
$definition = new ClassDefinition($statement->name, $statement, $statement->type); |
|
131
|
25 |
|
$definition->setFilepath($filepath); |
|
132
|
25 |
|
$definition->setNamespace($aliasManager->getNamespace()); |
|
133
|
|
|
|
|
134
|
25 |
|
if ($statement->extends) { |
|
135
|
|
|
$definition->setExtendsClass($statement->extends->toString()); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
25 |
|
foreach ($statement->implements as $interface) { |
|
139
|
|
|
$definition->addInterface($interface->toString()); |
|
140
|
25 |
|
} |
|
141
|
|
|
|
|
142
|
25 |
|
foreach ($statement->stmts as $stmt) { |
|
143
|
25 |
|
if ($stmt instanceof Node\Stmt\ClassMethod) { |
|
144
|
24 |
|
$definition->addMethod( |
|
145
|
24 |
|
new ClassMethod($stmt->name, $stmt, $stmt->type) |
|
146
|
24 |
|
); |
|
147
|
25 |
|
} elseif ($stmt instanceof Node\Stmt\Property) { |
|
148
|
3 |
|
$definition->addProperty($stmt); |
|
149
|
4 |
|
} elseif ($stmt instanceof Node\Stmt\TraitUse) { |
|
150
|
|
|
foreach ($stmt->traits as $traitPart) { |
|
151
|
|
|
$traitDefinition = $this->compiler->getTrait($traitPart->toString()); |
|
|
|
|
|
|
152
|
|
|
if ($traitDefinition) { |
|
153
|
|
|
$definition->mergeTrait($traitDefinition); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
1 |
|
} elseif ($stmt instanceof Node\Stmt\ClassConst) { |
|
157
|
1 |
|
$definition->addConst($stmt); |
|
158
|
1 |
|
} |
|
159
|
25 |
|
} |
|
160
|
|
|
|
|
161
|
25 |
|
$this->compiler->addClass($definition); |
|
162
|
25 |
|
} elseif ($statement instanceof Node\Stmt\Function_) { |
|
163
|
1 |
|
$definition = new FunctionDefinition($statement->name, $statement); |
|
164
|
1 |
|
$definition->setFilepath($filepath); |
|
165
|
1 |
|
$definition->setNamespace($aliasManager->getNamespace()); |
|
166
|
|
|
|
|
167
|
1 |
|
$this->compiler->addFunction($definition); |
|
168
|
25 |
|
} elseif ($statement instanceof Node\Stmt\Interface_) { |
|
169
|
|
|
$definition = new InterfaceDefinition($statement->name, $statement); |
|
170
|
|
|
$definition->setFilepath($filepath); |
|
171
|
|
|
$definition->setNamespace($aliasManager->getNamespace()); |
|
172
|
|
|
|
|
173
|
|
|
foreach ($statement->extends as $interface) { |
|
|
|
|
|
|
174
|
|
|
$definition->addExtendsInterface($interface->toString()); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
foreach ($statement->stmts as $stmt) { |
|
178
|
|
|
if ($stmt instanceof Node\Stmt\ClassMethod) { |
|
179
|
|
|
$method = new ClassMethod($stmt->name, $stmt, $stmt->type | Node\Stmt\Class_::MODIFIER_ABSTRACT); |
|
180
|
|
|
|
|
181
|
|
|
$definition->addMethod($method); |
|
182
|
|
|
} elseif ($stmt instanceof Node\Stmt\ClassConst) { |
|
183
|
|
|
$definition->addConst($stmt); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
$this->compiler->addInterface($definition); |
|
188
|
|
|
} |
|
189
|
25 |
|
} |
|
190
|
25 |
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.