5
5
namespace Smeghead \PhpVariableHardUsage \Parse ;
6
6
7
7
use PhpParser \Node \Expr \Variable ;
8
- use PhpParser \Node \Stmt ;
9
- use PhpParser \Node \Stmt \ClassMethod ;
10
- use PhpParser \Node \Stmt \Class_ ;
11
- use PhpParser \Node \Stmt \Function_ ;
12
- use PhpParser \NodeDumper ;
13
- use PhpParser \NodeVisitor \FindingVisitor ;
8
+ use PhpParser \Node \FunctionLike ;
9
+ use PhpParser \NodeFinder ;
14
10
use PhpParser \Parser ;
15
11
use Smeghead \PhpVariableHardUsage \Parse \Exception \ParseFailedException ;
16
-
17
12
final class VariableParser
18
13
{
19
14
private Parser $ parser ;
@@ -23,104 +18,25 @@ public function __construct()
23
18
$ this ->parser = (new \PhpParser \ParserFactory ())->createForNewestSupportedVersion ();
24
19
}
25
20
26
- /**
27
- * @param list<Stmt> $stmt
28
- * @return list<Function_>
29
- */
30
- private function getFunctions (array $ stmt ): array
31
- {
32
- $ functionVisitor = new FindingVisitor (function ($ node ) {
33
- return $ node instanceof Function_;
34
- });
35
- $ traverser = new \PhpParser \NodeTraverser ();
36
- $ traverser ->addVisitor ($ functionVisitor );
37
- $ traverser ->traverse ($ stmt );
38
-
39
- return $ functionVisitor ->getFoundNodes (); // @phpstan-ignore-line
40
- }
41
-
42
- /**
43
- * @param Function_|ClassMethod $function
44
- * @return list<Variable>
45
- */
46
- private function getVariables (Function_ |ClassMethod $ function ): array
47
- {
48
- $ variableVisitor = new FindingVisitor (function ($ node ) {
49
- return $ node instanceof Variable;
50
- });
51
- $ traverser = new \PhpParser \NodeTraverser ();
52
- $ traverser ->addVisitor ($ variableVisitor );
53
- $ traverser ->traverse ([$ function ]);
54
-
55
- return $ variableVisitor ->getFoundNodes (); // @phpstan-ignore-line
56
- }
57
-
58
- /**
59
- * @param list<Stmt> $stmts
60
- * @return list<Func>
61
- */
62
- private function parseFunctions (array $ stmts ): array
63
- {
64
- $ foundFunctions = $ this ->getFunctions ($ stmts );
65
-
66
- $ functions = [];
67
- foreach ($ foundFunctions as $ foundFunction ) {
68
- $ variables = $ this ->getVariables ($ foundFunction );
69
- $ func = new Func ($ foundFunction ->name ->name );
70
- foreach ($ variables as $ variable ) {
71
- $ func ->addVariable (new VarReference ($ variable ->name , $ variable ->getLine ())); // @phpstan-ignore-line
72
- }
73
- $ functions [] = $ func ;
74
- }
75
- return $ functions ;
76
- }
77
-
78
- /**
79
- * @param list<Stmt> $stmt
80
- * @return list<Class_>
81
- */
82
- private function getClasses (array $ stmt ): array
83
- {
84
- $ classVisitor = new FindingVisitor (function ($ node ) {
85
- return $ node instanceof \PhpParser \Node \Stmt \Class_;
86
- });
87
- $ traverser = new \PhpParser \NodeTraverser ();
88
- $ traverser ->addVisitor ($ classVisitor );
89
- $ traverser ->traverse ($ stmt );
90
-
91
- return $ classVisitor ->getFoundNodes (); // @phpstan-ignore-line
92
- }
93
-
94
- /**
95
- * @param list<Stmt> $stmts
96
- * @return list<Func>
97
- */
98
- private function parseClasses (array $ stmts ): array
99
- {
100
- $ foundClasses = $ this ->getClasses ($ stmts );
101
-
102
- $ methods = [];
103
- foreach ($ foundClasses as $ foundClass ) {
104
- foreach ($ foundClass ->getMethods () as $ method ) {
105
- $ variables = $ this ->getVariables ($ method );
106
- $ func = new Func (sprintf ('%s::%s ' , $ foundClass ->name , $ method ->name ->name ));
107
- foreach ($ variables as $ variable ) {
108
- $ func ->addVariable (new VarReference ($ variable ->name , $ variable ->getLine ())); // @phpstan-ignore-line
109
- }
110
- $ methods [] = $ func ;
111
- }
112
- }
113
- return $ methods ;
114
- }
115
-
116
21
public function parse (string $ content ): ParseResult
117
22
{
118
23
$ stmts = $ this ->parser ->parse ($ content );
119
24
if ($ stmts === null ) {
120
25
throw new ParseFailedException ();
121
26
}
122
27
123
- $ functions = $ this ->parseFunctions ($ stmts ) + $ this ->parseClasses ($ stmts );
28
+ $ nodeFinder = new NodeFinder ();
29
+
30
+ $ functionLikes = $ nodeFinder ->findInstanceOf ($ stmts , FunctionLike::class);
31
+
32
+ $ functions = array_map (function (FunctionLike $ function ) use ($ nodeFinder ) {
33
+ $ func = new Func ($ function ->name ->name );
34
+ $ variables = $ nodeFinder ->findInstanceOf ($ function , Variable::class);
35
+ foreach ($ variables as $ variable ) {
36
+ $ func ->addVariable (new VarReference ($ variable ->name , $ variable ->getLine ()));
37
+ }
38
+ return $ func ;
39
+ }, $ functionLikes );
124
40
125
41
return new ParseResult ($ functions );
126
42
}
0 commit comments