Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# CHANGELOG

## v0.0.1 (2025-03-18)

### Features

* Add support for anonymous function. by @hirokinoue
* Add namespace to scope output.

### Bug fix

* Refactor how to find nodes. by @hirokinoue

6 changes: 5 additions & 1 deletion src/Analyze/AnalysisResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public function format(): string
'maxVariableHardUsage' => $this->maxVariableHardUsage,
'avarageVariableHardUsage' => $this->avarageVariableHardUsage,
];
$output['scopes'] = array_map(fn(Scope $scope) => ['name' => $scope->getName(), 'variableHardUsage' => $scope->getVariableHardUsage()], $this->scopes);
$output['scopes'] = array_map(fn(Scope $scope) => [
'namespace' => $scope->getNamespace(),
'name' => $scope->getName(),
'variableHardUsage' => $scope->getVariableHardUsage()
], $this->scopes);
return json_encode($output, JSON_PRETTY_PRINT) . PHP_EOL;
}
}
7 changes: 6 additions & 1 deletion src/Analyze/FunctionScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ final class FunctionScope implements Scope
/**
* @param list<AnalyzedVariable> $analyzedVariables
*/
public function __construct(private string $name, array $analyzedVariables)
public function __construct(private ?string $namespace, private string $name, array $analyzedVariables)
{
$this->analyzedVariables = $analyzedVariables;
}

public function getNamespace(): ?string
{
return $this->namespace;
}

public function getName(): string
{
return $this->name;
Expand Down
1 change: 1 addition & 0 deletions src/Analyze/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

interface Scope
{
public function getNamespace(): ?string;
public function getName(): string;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Analyze/VariableAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private function analyzeFunction(Func $function): Scope
$analyzedVars[] = new AnalyzedVariable($variableName, $variableHardUsage);
}

return new FunctionScope($function->name, $analyzedVars);
return new FunctionScope($function->namespace, $function->name, $analyzedVars);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Parse/Func.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ final class Func
/** @var list<VarReference> */
private array $variables;

public function __construct(public readonly string $name)
public function __construct(
public readonly ?string $namespace,
public readonly string $name
)
{
$this->variables = [];
}
Expand Down
18 changes: 16 additions & 2 deletions src/Parse/VariableParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\FunctionLike;
use PhpParser\NodeFinder;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\Parser;
use Smeghead\PhpVariableHardUsage\Parse\Exception\ParseFailedException;
use Smeghead\PhpVariableHardUsage\Parse\Visitor\FunctionLikeFindingVisitor;
Expand All @@ -24,12 +25,25 @@ public function __construct()
$this->nodeFinder = new NodeFinder();
}

/**
* @param array<\PhpParser\Node\Stmt> $stmts
* @return array<\PhpParser\Node\Stmt>
*/
private function resolveNames(array $stmts): array
{
$nameResolver = new NameResolver();
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor($nameResolver);
return $nodeTraverser->traverse($stmts);
}

public function parse(string $content): ParseResult
{
$stmts = $this->parser->parse($content);
if ($stmts === null) {
throw new ParseFailedException();
}
$stmts = $this->resolveNames($stmts);

$traverser = new NodeTraverser();
$visitor = new FunctionLikeFindingVisitor(fn($node) => $node instanceof FunctionLike);
Expand All @@ -49,15 +63,15 @@ public function parse(string $content): ParseResult
private function collectParseResultPerFunctionLike(array $functionLikes): array
{
return array_map(function (FunctionLike $function) {
$namespace = $function->getAttribute('namespace'); // Get the namespace name set in FunctionLikeFindingVisitor
$className = $function->getAttribute('className'); // Get the class name set in FunctionLikeFindingVisitor
$functionName = $function->name->name ?? $function->getType() . '@' . $function->getStartLine();
$functionIdentifier = sprintf(
'%s%s',
isset($className) ? $className . '::' : '',
$functionName
);

$func = new Func($functionIdentifier);
$func = new Func($namespace, $functionIdentifier);

$variables = $this->nodeFinder->findInstanceOf($function, Variable::class);
foreach ($variables as $variable) {
Expand Down
11 changes: 10 additions & 1 deletion src/Parse/Visitor/FunctionLikeFindingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,34 @@
namespace Smeghead\PhpVariableHardUsage\Parse\Visitor;

use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\NodeVisitor\FindingVisitor;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Namespace_;

/**
* FindingVisitor that searches for elements of FunctionLike
*
* For ClassMethod, set the class name to ClassName as Attribute.
*/
class FunctionLikeFindingVisitor extends FindingVisitor {
protected array $foundNodes = [];
private ?string $currentClass = null;
private ?string $currentNamespace = null;

public function enterNode(Node $node) {
if ($node instanceof Class_) {
// Record class name
$this->currentClass = $node->name ? $node->name->name : null;
}
if ($node instanceof Namespace_) {
// Record class name
$this->currentNamespace = $node->name ? $node->name->name : null;
}

if ($node instanceof FunctionLike) {
$node->setAttribute('namespace', $this->currentNamespace);
}
if ($node instanceof ClassMethod) {
$node->setAttribute('className', $this->currentClass);
}
Expand Down
4 changes: 2 additions & 2 deletions test/VariableAnalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class VariableAnalizerTest extends TestCase
{
public function testAnalyzeFunctionSimple(): void
{
$func = new Func('testFunction');
$func = new Func(null, 'testFunction');
$func->addVariable(new VarReference('a', 1));
$func->addVariable(new VarReference('a', 2));
$func->addVariable(new VarReference('a', 3));
Expand All @@ -27,7 +27,7 @@ public function testAnalyzeFunctionSimple(): void

public function testAnalyzeFunctionLong(): void
{
$func = new Func('testFunction');
$func = new Func(null, 'testFunction');
$func->addVariable(new VarReference('a', 1));
$func->addVariable(new VarReference('a', 2));
$func->addVariable(new VarReference('a', 100));
Expand Down
35 changes: 28 additions & 7 deletions test/VariableParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public function testParseFunction(): void
$this->assertInstanceOf(ParseResult::class, $result);
$functions = $result->functions;
$this->assertCount(1, $functions);
$this->assertEquals('smallFunction', $functions[0]->name);
$this->assertSame('smallFunction', $functions[0]->name);
$this->assertSame(null, $functions[0]->namespace);
$this->assertCount(2, $functions[0]->getVariables());

$vars = $functions[0]->getVariables();
Expand All @@ -38,18 +39,19 @@ public function testParseClass(): void
$this->assertInstanceOf(ParseResult::class, $result);
$functions = $result->functions;
$this->assertCount(1, $functions);
$this->assertEquals('Clazz::bigFunction', $functions[0]->name);
$this->assertSame('Smeghead\PhpVariableHardUsage\Fixtures', $functions[0]->namespace);
$this->assertSame('Clazz::bigFunction', $functions[0]->name);
$this->assertCount(4, $functions[0]->getVariables());

$vars = $functions[0]->getVariables();
$this->assertSame('num', $vars[0]->name);
$this->assertSame(9, $vars[0]->lineNumber, 'first $num (9)');
$this->assertSame(11, $vars[0]->lineNumber, 'first $num (11)');
$this->assertSame('num', $vars[1]->name);
$this->assertSame(11, $vars[1]->lineNumber, 'second $num (11)');
$this->assertSame(13, $vars[1]->lineNumber, 'second $num (13)');
$this->assertSame('num', $vars[2]->name);
$this->assertSame(12, $vars[2]->lineNumber, 'second $num (12)');
$this->assertSame(14, $vars[2]->lineNumber, 'second $num (14)');
$this->assertSame('num', $vars[3]->name);
$this->assertSame(15, $vars[3]->lineNumber, 'second $num (15)');
$this->assertSame(17, $vars[3]->lineNumber, 'second $num (17)');
}

public function testParseAnonymousFunction(): void
Expand All @@ -61,7 +63,7 @@ public function testParseAnonymousFunction(): void
$this->assertInstanceOf(ParseResult::class, $result);
$functions = $result->functions;
$this->assertCount(1, $functions);
$this->assertEquals('Expr_Closure@4', $functions[0]->name);
$this->assertSame('Expr_Closure@4', $functions[0]->name);
$this->assertCount(4, $functions[0]->getVariables());

$vars = $functions[0]->getVariables();
Expand All @@ -74,4 +76,23 @@ public function testParseAnonymousFunction(): void
$this->assertSame('b', $vars[3]->name);
$this->assertSame(5, $vars[3]->lineNumber, 'second $b (5)');
}

public function testParseNamespace(): void
{
$parser = new VariableParser();
$content = file_get_contents($this->fixtureDir . '/namespace.php');
$result = $parser->parse($content);

$this->assertInstanceOf(ParseResult::class, $result);
$functions = $result->functions;
$this->assertCount(2, $functions);
$this->assertSame('SomeNamespace', $functions[0]->namespace);
$this->assertSame('someFunction', $functions[0]->name);
$this->assertCount(0, $functions[0]->getVariables());

$this->assertSame('OtherNamespace', $functions[1]->namespace);
$this->assertSame('otherFunction', $functions[1]->name);
$this->assertCount(0, $functions[1]->getVariables());
}

}
2 changes: 2 additions & 0 deletions test/fixtures/Clazz.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

namespace Smeghead\PhpVariableHardUsage\Fixtures;

class Clazz
{
public function bigFunction(): void
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/brace_namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace AnotherNamespace {
function anotherFunction(): void {}
}
8 changes: 8 additions & 0 deletions test/fixtures/namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace SomeNamespace;

function someFunction(): void {}

namespace OtherNamespace;

function otherFunction(): void {}