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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions src/Analyze/AnalysisResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class AnalysisResult
* @param list<Scope> $scopes
*/
public function __construct(
public readonly string $filename,
public readonly array $scopes
)
{
Expand All @@ -27,6 +28,7 @@ public function __construct(
public function format(): string
{
$output = [
'filename' => $this->filename,
'maxVariableHardUsage' => $this->maxVariableHardUsage,
'avarageVariableHardUsage' => $this->avarageVariableHardUsage,
];
Expand Down
13 changes: 5 additions & 8 deletions src/Analyze/VariableAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@ final class VariableAnalyzer
{
private const ASSIGNED_VARIABLE_COEFFICIENT = 2;

/**
* @var list<Func>
*/
private array $functions;

/**
* @param list<Func> $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
Expand Down
2 changes: 1 addition & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
12 changes: 8 additions & 4 deletions test/VariableAnalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down