diff --git a/CHANGELOG.md b/CHANGELOG.md index b1b6248..da2fad7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ * The base of deviation of local variable abuse has been changed from the average number of rows to the first number of rows. * AssignOp has been added to the assignment decision in addition to Assign. - + * Add filename to report json. ## v0.0.2 (2025-03-19) diff --git a/src/Analyze/AnalysisResult.php b/src/Analyze/AnalysisResult.php index 2c9c169..7efa880 100644 --- a/src/Analyze/AnalysisResult.php +++ b/src/Analyze/AnalysisResult.php @@ -13,6 +13,7 @@ final class AnalysisResult * @param list $scopes */ public function __construct( + public readonly string $filename, public readonly array $scopes ) { @@ -27,6 +28,7 @@ public function __construct( public function format(): string { $output = [ + 'filename' => $this->filename, 'maxVariableHardUsage' => $this->maxVariableHardUsage, 'avarageVariableHardUsage' => $this->avarageVariableHardUsage, ]; diff --git a/src/Analyze/VariableAnalyzer.php b/src/Analyze/VariableAnalyzer.php index 2ff387e..fbcd641 100644 --- a/src/Analyze/VariableAnalyzer.php +++ b/src/Analyze/VariableAnalyzer.php @@ -11,22 +11,19 @@ final class VariableAnalyzer { private const ASSIGNED_VARIABLE_COEFFICIENT = 2; - /** - * @var list - */ - private array $functions; - /** * @param list $functions */ - public function __construct(array $functions) + public function __construct( + public readonly string $filename, + public readonly array $functions + ) { - $this->functions = $functions; } public function analyze(): AnalysisResult { - return new AnalysisResult(array_map(fn($f) => $this->analyzeFunction($f), $this->functions)); + return new AnalysisResult($this->filename, array_map(fn($f) => $this->analyzeFunction($f), $this->functions)); } private function analyzeFunction(Func $function): Scope diff --git a/src/Command.php b/src/Command.php index 66c5119..82c0a4d 100644 --- a/src/Command.php +++ b/src/Command.php @@ -32,7 +32,7 @@ public function run(array $argv): void return; } $parseResult = $parser->parse($content); - $analyzer = new VariableAnalyzer($parseResult->functions); + $analyzer = new VariableAnalyzer($filePath, $parseResult->functions); $result = $analyzer->analyze(); echo $result->format(); } diff --git a/test/VariableAnalizerTest.php b/test/VariableAnalizerTest.php index 77ba6d2..56c9690 100644 --- a/test/VariableAnalizerTest.php +++ b/test/VariableAnalizerTest.php @@ -16,8 +16,9 @@ public function testAnalyzeFunctionSimple(): void $func->addVariable(new VarReference('a', 2)); $func->addVariable(new VarReference('a', 3)); - $sut = new VariableAnalyzer([$func]); + $sut = new VariableAnalyzer('target.php', [$func]); $result = $sut->analyze(); + $this->assertSame('target.php', $result->filename); $scopes = $result->scopes; $this->assertCount(1, $scopes); @@ -32,8 +33,9 @@ public function testAnalyzeFunctionLong(): void $func->addVariable(new VarReference('a', 2)); $func->addVariable(new VarReference('a', 100)); - $sut = new VariableAnalyzer([$func]); + $sut = new VariableAnalyzer('target.php', [$func]); $result = $sut->analyze(); + $this->assertSame('target.php', $result->filename); $scopes = $result->scopes; $this->assertCount(1, $scopes); @@ -48,8 +50,9 @@ public function testAnalyzeFunctionLongAssignedVariable(): void $func->addVariable(new VarReference('a', 2)); $func->addVariable(new VarReference('a', 100)); - $sut = new VariableAnalyzer([$func]); + $sut = new VariableAnalyzer('target.php', [$func]); $result = $sut->analyze(); + $this->assertSame('target.php', $result->filename); $scopes = $result->scopes; $this->assertCount(1, $scopes); @@ -64,8 +67,9 @@ public function testAnalyzeFunctionLongMultipleAssignedVariable(): void $func->addVariable(new VarReference('a', 2)); $func->addVariable(new VarReference('a', 100, true)); - $sut = new VariableAnalyzer([$func]); + $sut = new VariableAnalyzer('target.php', [$func]); $result = $sut->analyze(); + $this->assertSame('target.php', $result->filename); $scopes = $result->scopes; $this->assertCount(1, $scopes);