Skip to content

Commit 276c66e

Browse files
committed
parse Classes.
1 parent 3f85b12 commit 276c66e

File tree

7 files changed

+96
-16
lines changed

7 files changed

+96
-16
lines changed

src/Core/Exception/ParseFailedException.php renamed to src/Parse/Exception/ParseFailedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Smeghead\PhpVariableHardUsage\Core\Exception;
5+
namespace Smeghead\PhpVariableHardUsage\Parse\Exception;
66

77
use Exception;
88

src/Core/Func.php renamed to src/Parse/Func.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Smeghead\PhpVariableHardUsage\Core;
5+
namespace Smeghead\PhpVariableHardUsage\Parse;
66

77
final class Func
88
{

src/Core/ParseResult.php renamed to src/Parse/ParseResult.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Smeghead\PhpVariableHardUsage\Core;
5+
namespace Smeghead\PhpVariableHardUsage\Parse;
66

77
final class ParseResult
88
{

src/Core/VarReference.php renamed to src/Parse/VarReference.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Smeghead\PhpVariableHardUsage\Core;
5+
namespace Smeghead\PhpVariableHardUsage\Parse;
66

77
final class VarReference
88
{

src/Core/VariableParser.php renamed to src/Parse/VariableParser.php

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
declare(strict_types=1);
44

5-
namespace Smeghead\PhpVariableHardUsage\Core;
5+
namespace Smeghead\PhpVariableHardUsage\Parse;
66

77
use PhpParser\Node\Expr\Variable;
88
use PhpParser\Node\Stmt;
9+
use PhpParser\Node\Stmt\ClassMethod;
910
use PhpParser\Node\Stmt\Function_;
1011
use PhpParser\NodeDumper;
1112
use PhpParser\NodeVisitor\FindingVisitor;
1213
use PhpParser\Parser;
13-
use Smeghead\PhpVariableHardUsage\Core\Exception\ParseFailedException;
14+
use Smeghead\PhpVariableHardUsage\Parse\Exception\ParseFailedException;
1415

1516
final 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
}

test/VariableParserTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
declare(strict_types=1);
44

55
use PHPUnit\Framework\TestCase;
6-
use Smeghead\PhpVariableHardUsage\Core\ParseResult;
7-
use Smeghead\PhpVariableHardUsage\Core\VariableParser;
6+
use Smeghead\PhpVariableHardUsage\Parse\ParseResult;
7+
use Smeghead\PhpVariableHardUsage\Parse\VariableParser;
88

99
class VariableParserTest extends TestCase
1010
{
1111
private string $fixtureDir = __DIR__ . '/fixtures';
1212

13-
public function testParse(): void
13+
public function testParseFunction(): void
1414
{
1515
$parser = new VariableParser();
1616
$content = file_get_contents($this->fixtureDir . '/function.php');
@@ -28,4 +28,27 @@ public function testParse(): void
2828
$this->assertSame('num', $vars[1]->getName());
2929
$this->assertSame(10, $vars[1]->getLineNumber(), 'second $num (10)');
3030
}
31+
32+
public function testParseClass(): void
33+
{
34+
$parser = new VariableParser();
35+
$content = file_get_contents($this->fixtureDir . '/Clazz.php');
36+
$result = $parser->parse($content);
37+
38+
$this->assertInstanceOf(ParseResult::class, $result);
39+
$fuctions = $result->getfunctions();
40+
$this->assertCount(1, $fuctions);
41+
$this->assertEquals('Clazz::bigFunction', $fuctions[0]->getName());
42+
$this->assertCount(4, $fuctions[0]->getVariables());
43+
44+
$vars = $fuctions[0]->getVariables();
45+
$this->assertSame('num', $vars[0]->getName());
46+
$this->assertSame(9, $vars[0]->getLineNumber(), 'first $num (9)');
47+
$this->assertSame('num', $vars[1]->getName());
48+
$this->assertSame(11, $vars[1]->getLineNumber(), 'second $num (11)');
49+
$this->assertSame('num', $vars[2]->getName());
50+
$this->assertSame(12, $vars[2]->getLineNumber(), 'second $num (12)');
51+
$this->assertSame('num', $vars[3]->getName());
52+
$this->assertSame(15, $vars[3]->getLineNumber(), 'second $num (15)');
53+
}
3154
}

test/fixtures/Clazz.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class Clazz
6+
{
7+
public function bigFunction(): void
8+
{
9+
$num = 1; // 5行目
10+
11+
if ($num === 1) {
12+
$num = 2; // 9行目
13+
}
14+
15+
echo $num; // 12行目
16+
}
17+
}

0 commit comments

Comments
 (0)