1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace gossi\codegen\parser; |
5
|
|
|
|
6
|
|
|
use gossi\codegen\parser\visitor\ParserVisitorInterface; |
7
|
|
|
use phootwork\collection\Set; |
8
|
|
|
use phootwork\file\exception\FileNotFoundException; |
9
|
|
|
use phootwork\file\File; |
10
|
|
|
use PhpParser\Node; |
11
|
|
|
use PhpParser\NodeTraverser; |
12
|
|
|
use PhpParser\NodeVisitorAbstract; |
13
|
|
|
use PhpParser\Parser; |
14
|
|
|
|
15
|
|
|
class FileParser extends NodeVisitorAbstract { |
16
|
|
|
|
17
|
|
|
private $visitors; |
18
|
|
|
private $filename; |
19
|
|
|
|
20
|
17 |
|
public function __construct($filename) { |
21
|
17 |
|
$this->filename = $filename; |
22
|
17 |
|
$this->visitors = new Set(); |
23
|
17 |
|
} |
24
|
|
|
|
25
|
16 |
|
public function addVisitor(ParserVisitorInterface $visitor) { |
26
|
16 |
|
$this->visitors->add($visitor); |
27
|
16 |
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
public function removeVisitor(ParserVisitorInterface $visitor) { |
31
|
1 |
|
$this->visitors->remove($visitor); |
32
|
1 |
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
public function hasVisitor(ParserVisitorInterface $visitor): bool { |
36
|
1 |
|
return $this->visitors->contains($visitor); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws FileNotFoundException |
41
|
|
|
*/ |
42
|
16 |
|
public function parse() { |
43
|
16 |
|
$file = new File($this->filename); |
44
|
|
|
|
45
|
16 |
|
if (!$file->exists()) { |
46
|
1 |
|
throw new FileNotFoundException(sprintf('File (%s) does not exist.', $this->filename)); |
47
|
|
|
} |
48
|
|
|
|
49
|
15 |
|
$parser = $this->getParser(); |
50
|
15 |
|
$traverser = new NodeTraverser(); |
51
|
15 |
|
$traverser->addVisitor($this); |
52
|
15 |
|
$traverser->traverse($parser->parse($file->read())); |
|
|
|
|
53
|
15 |
|
} |
54
|
|
|
|
55
|
15 |
|
private function getParser(): Parser { |
56
|
15 |
|
$factory = new \PhpParser\ParserFactory(); |
57
|
15 |
|
return $factory->create(\PhpParser\ParserFactory::PREFER_PHP7); |
58
|
|
|
} |
59
|
|
|
|
60
|
15 |
|
public function enterNode(Node $node) { |
61
|
15 |
|
foreach ($this->visitors as $visitor) { |
62
|
15 |
|
switch ($node->getType()) { |
63
|
15 |
|
case 'Stmt_Namespace': |
64
|
9 |
|
$visitor->visitNamespace($node); |
65
|
9 |
|
break; |
66
|
|
|
|
67
|
15 |
|
case 'Stmt_UseUse': |
68
|
1 |
|
$visitor->visitUseStatement($node); |
69
|
1 |
|
break; |
70
|
|
|
|
71
|
15 |
|
case 'Stmt_Class': |
72
|
12 |
|
$visitor->visitStruct($node); |
73
|
12 |
|
$visitor->visitClass($node); |
74
|
12 |
|
break; |
75
|
|
|
|
76
|
15 |
|
case 'Stmt_Interface': |
77
|
2 |
|
$visitor->visitStruct($node); |
78
|
2 |
|
$visitor->visitInterface($node); |
79
|
2 |
|
break; |
80
|
|
|
|
81
|
15 |
|
case 'Stmt_Trait': |
82
|
1 |
|
$visitor->visitStruct($node); |
83
|
1 |
|
$visitor->visitTrait($node); |
84
|
1 |
|
break; |
85
|
|
|
|
86
|
15 |
|
case 'Stmt_TraitUse': |
87
|
2 |
|
$visitor->visitTraitUse($node); |
88
|
2 |
|
break; |
89
|
|
|
|
90
|
15 |
|
case 'Stmt_ClassConst': |
91
|
3 |
|
$visitor->visitConstants($node); |
92
|
3 |
|
break; |
93
|
|
|
|
94
|
15 |
|
case 'Stmt_Property': |
95
|
5 |
|
$visitor->visitProperty($node); |
96
|
5 |
|
break; |
97
|
|
|
|
98
|
15 |
|
case 'Stmt_ClassMethod': |
99
|
8 |
|
$visitor->visitMethod($node); |
100
|
8 |
|
break; |
101
|
|
|
} |
102
|
|
|
} |
103
|
15 |
|
} |
104
|
|
|
} |
105
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.