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
|
20 |
|
public function __construct(\PhpParser\Parser $parser, Compiler $compiler) |
33
|
|
|
{ |
34
|
20 |
|
$this->nodeTraverser = new \PhpParser\NodeTraverser(); |
35
|
20 |
|
$this->nodeTraverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver); |
36
|
|
|
|
37
|
20 |
|
$this->parser = $parser; |
38
|
20 |
|
$this->compiler = $compiler; |
39
|
20 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $filepath |
43
|
|
|
* @param Context $context |
44
|
|
|
*/ |
45
|
20 |
|
public function parserFile($filepath, Context $context) |
46
|
|
|
{ |
47
|
20 |
|
$context->setFilepath($filepath); |
48
|
|
|
|
49
|
|
|
try { |
50
|
20 |
|
if (!is_readable($filepath)) { |
51
|
|
|
throw new RuntimeException('File ' . $filepath . ' is not readable'); |
52
|
|
|
} |
53
|
|
|
|
54
|
20 |
|
$context->debug('<comment>Precompile: ' . $filepath . '.</comment>'); |
55
|
|
|
|
56
|
20 |
|
$code = file_get_contents($filepath); |
57
|
20 |
|
$astTree = $this->parser->parse($code); |
58
|
|
|
|
59
|
20 |
|
$this->nodeTraverser->traverse($astTree); |
|
|
|
|
60
|
|
|
|
61
|
20 |
|
$context->aliasManager = new AliasManager(); |
62
|
20 |
|
$namespace = null; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Step 1 Precompile |
66
|
|
|
*/ |
67
|
20 |
|
foreach ($astTree as $topStatement) { |
|
|
|
|
68
|
20 |
|
if ($topStatement instanceof Node\Stmt\Namespace_) { |
69
|
|
|
/** |
70
|
|
|
* Namespace block can be created without NS name |
71
|
|
|
*/ |
72
|
20 |
|
if ($topStatement->name) { |
73
|
20 |
|
$namespace = $topStatement->name->toString(); |
74
|
20 |
|
$context->aliasManager->setNamespace($namespace); |
75
|
20 |
|
} |
76
|
|
|
|
77
|
20 |
|
if ($topStatement->stmts) { |
|
|
|
|
78
|
20 |
|
$this->parseTopDefinitions($topStatement->stmts, $context->aliasManager, $filepath); |
|
|
|
|
79
|
20 |
|
} |
80
|
20 |
|
} else { |
81
|
|
|
if (is_array($topStatement)) { |
82
|
|
|
$this->parseTopDefinitions($topStatement, $context->aliasManager, $filepath); |
|
|
|
|
83
|
|
|
} else { |
84
|
|
|
$this->parseTopDefinitions($astTree, $context->aliasManager, $filepath); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
} |
87
|
20 |
|
} |
88
|
|
|
|
89
|
20 |
|
$context->clear(); |
90
|
20 |
|
} catch (\PhpParser\Error $e) { |
91
|
|
|
$context->syntaxError($e, $filepath); |
92
|
|
|
} catch (Exception $e) { |
93
|
|
|
$context->output->writeln("<error>{$e->getMessage()}</error>"); |
94
|
|
|
} |
95
|
20 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param Node\Stmt $topStatement |
99
|
|
|
* @param AliasManager $aliasManager |
100
|
|
|
* @param string $filepath |
101
|
|
|
*/ |
102
|
20 |
|
protected function parseTopDefinitions($topStatement, AliasManager $aliasManager, $filepath) |
103
|
|
|
{ |
104
|
20 |
|
foreach ($topStatement as $statement) { |
|
|
|
|
105
|
20 |
|
if ($statement instanceof Node\Stmt\Use_) { |
106
|
|
|
if (count($statement->uses) > 0) { |
107
|
|
|
foreach ($statement->uses as $use) { |
108
|
|
|
$aliasManager->add($use->name->parts); |
109
|
|
|
} |
110
|
|
|
} |
111
|
20 |
|
} elseif ($statement instanceof Node\Stmt\Class_) { |
112
|
20 |
|
$definition = new ClassDefinition($statement->name, $statement, $statement->type); |
113
|
20 |
|
$definition->setFilepath($filepath); |
114
|
20 |
|
$definition->setNamespace($aliasManager->getNamespace()); |
115
|
|
|
|
116
|
20 |
|
if ($statement->extends) { |
117
|
|
|
$definition->setExtendsClass($statement->extends->toString()); |
118
|
|
|
} |
119
|
|
|
|
120
|
20 |
|
if ($statement->implements) { |
121
|
|
|
foreach ($statement->implements as $interface) { |
122
|
|
|
$definition->addInterface($interface->toString()); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
20 |
|
foreach ($statement->stmts as $stmt) { |
127
|
20 |
|
if ($stmt instanceof Node\Stmt\ClassMethod) { |
128
|
19 |
|
$method = new ClassMethod($stmt->name, $stmt, $stmt->type); |
129
|
|
|
|
130
|
19 |
|
$definition->addMethod($method); |
131
|
20 |
|
} elseif ($stmt instanceof Node\Stmt\Property) { |
132
|
1 |
|
$definition->addProperty($stmt); |
133
|
2 |
|
} elseif ($stmt instanceof Node\Stmt\ClassConst) { |
134
|
1 |
|
$definition->addConst($stmt); |
135
|
1 |
|
} |
136
|
20 |
|
} |
137
|
|
|
|
138
|
20 |
|
$this->compiler->addClass($definition); |
139
|
20 |
|
} elseif ($statement instanceof Node\Stmt\Function_) { |
140
|
1 |
|
$definition = new FunctionDefinition($statement->name, $statement); |
141
|
1 |
|
$definition->setFilepath($filepath); |
142
|
1 |
|
$definition->setNamespace($aliasManager->getNamespace()); |
143
|
|
|
|
144
|
1 |
|
$this->compiler->addFunction($definition); |
145
|
1 |
|
} |
146
|
20 |
|
} |
147
|
20 |
|
} |
148
|
|
|
} |
149
|
|
|
|
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.