Skip to content

Commit 5fd3856

Browse files
committed
FunctionLikeを分析対象にする
1 parent b38a11e commit 5fd3856

File tree

1 file changed

+14
-98
lines changed

1 file changed

+14
-98
lines changed

src/Parse/VariableParser.php

Lines changed: 14 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@
55
namespace Smeghead\PhpVariableHardUsage\Parse;
66

77
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;
1410
use PhpParser\Parser;
1511
use Smeghead\PhpVariableHardUsage\Parse\Exception\ParseFailedException;
16-
1712
final class VariableParser
1813
{
1914
private Parser $parser;
@@ -23,104 +18,25 @@ public function __construct()
2318
$this->parser = (new \PhpParser\ParserFactory())->createForNewestSupportedVersion();
2419
}
2520

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-
11621
public function parse(string $content): ParseResult
11722
{
11823
$stmts = $this->parser->parse($content);
11924
if ($stmts === null) {
12025
throw new ParseFailedException();
12126
}
12227

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);
12440

12541
return new ParseResult($functions);
12642
}

0 commit comments

Comments
 (0)