22
33declare (strict_types=1 );
44
5- namespace Smeghead \PhpVariableHardUsage \Core ;
5+ namespace Smeghead \PhpVariableHardUsage \Parse ;
66
77use PhpParser \Node \Expr \Variable ;
88use PhpParser \Node \Stmt ;
9+ use PhpParser \Node \Stmt \ClassMethod ;
910use PhpParser \Node \Stmt \Function_ ;
1011use PhpParser \NodeDumper ;
1112use PhpParser \NodeVisitor \FindingVisitor ;
1213use PhpParser \Parser ;
13- use Smeghead \PhpVariableHardUsage \Core \Exception \ParseFailedException ;
14+ use Smeghead \PhpVariableHardUsage \Parse \Exception \ParseFailedException ;
1415
1516final class VariableParser
1617{
@@ -37,7 +38,7 @@ private function getFunctions(array $stmt): array
3738 return $ functionVisitor ->getFoundNodes ();
3839 }
3940
40- private function getVariables (Function_ $ function ): array
41+ private function getVariables (Function_ | ClassMethod $ function ): array
4142 {
4243 $ variableVisitor = new FindingVisitor (function ($ node ) {
4344 return $ node instanceof Variable;
@@ -49,13 +50,8 @@ private function getVariables(Function_ $function): array
4950 return $ variableVisitor ->getFoundNodes ();
5051 }
5152
52- public function parse ( string $ content ): ParseResult
53+ private function parseFunctions ( array $ stmts ): array
5354 {
54- $ stmts = $ this ->parser ->parse ($ content );
55- if ($ stmts === null ) {
56- throw new ParseFailedException ();
57- }
58-
5955 $ foundFunctions = $ this ->getFunctions ($ stmts );
6056
6157 $ functions = [];
@@ -67,6 +63,50 @@ public function parse(string $content): ParseResult
6763 }
6864 $ functions [] = $ func ;
6965 }
66+ return $ functions ;
67+ }
68+
69+ private function getClasses (array $ stmt ): array
70+ {
71+ $ classVisitor = new FindingVisitor (function ($ node ) {
72+ return $ node instanceof \PhpParser \Node \Stmt \Class_;
73+ });
74+ $ traverser = new \PhpParser \NodeTraverser ();
75+ $ traverser ->addVisitor ($ classVisitor );
76+ $ traverser ->traverse ($ stmt );
77+
78+ return $ classVisitor ->getFoundNodes ();
79+ }
80+
81+ private function parseClasses (array $ stmts ): array
82+ {
83+ $ foundClasses = $ this ->getClasses ($ stmts );
84+
85+ $ methods = [];
86+ foreach ($ foundClasses as $ foundClass ) {
87+ foreach ($ foundClass ->getMethods () as $ method ) {
88+ $ variables = $ this ->getVariables ($ method );
89+ $ func = new Func (sprintf ('%s::%s ' , $ foundClass ->name , $ method ->name ->name ));
90+ foreach ($ variables as $ variable ) {
91+ $ func ->addVariable (new VarReference ($ variable ->name , $ variable ->getLine ()));
92+ }
93+ $ methods [] = $ func ;
94+ }
95+ }
96+ return $ methods ;
97+ }
98+
99+ public function parse (string $ content ): ParseResult
100+ {
101+ $ stmts = $ this ->parser ->parse ($ content );
102+ if ($ stmts === null ) {
103+ throw new ParseFailedException ();
104+ }
105+ $ dumper = new NodeDumper ();
106+ // echo $dumper->dump($stmts) . PHP_EOL;
107+
108+ $ functions = $ this ->parseFunctions ($ stmts ) + $ this ->parseClasses ($ stmts );
109+
70110 return new ParseResult ($ functions );
71111 }
72112}
0 commit comments