Skip to content

Commit 66e94a6

Browse files
authored
Merge pull request #8 from smeghead/feature/add-filename
feat: Add filename to report json.
2 parents c616e92 + 76495cb commit 66e94a6

File tree

5 files changed

+17
-14
lines changed

5 files changed

+17
-14
lines changed

CHANGELOG.md

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

55
* The base of deviation of local variable abuse has been changed from the average number of rows to the first number of rows.
66
* AssignOp has been added to the assignment decision in addition to Assign.
7-
7+
* Add filename to report json.
88

99
## v0.0.2 (2025-03-19)
1010

src/Analyze/AnalysisResult.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ final class AnalysisResult
1313
* @param list<Scope> $scopes
1414
*/
1515
public function __construct(
16+
public readonly string $filename,
1617
public readonly array $scopes
1718
)
1819
{
@@ -27,6 +28,7 @@ public function __construct(
2728
public function format(): string
2829
{
2930
$output = [
31+
'filename' => $this->filename,
3032
'maxVariableHardUsage' => $this->maxVariableHardUsage,
3133
'avarageVariableHardUsage' => $this->avarageVariableHardUsage,
3234
];

src/Analyze/VariableAnalyzer.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,19 @@ final class VariableAnalyzer
1111
{
1212
private const ASSIGNED_VARIABLE_COEFFICIENT = 2;
1313

14-
/**
15-
* @var list<Func>
16-
*/
17-
private array $functions;
18-
1914
/**
2015
* @param list<Func> $functions
2116
*/
22-
public function __construct(array $functions)
17+
public function __construct(
18+
public readonly string $filename,
19+
public readonly array $functions
20+
)
2321
{
24-
$this->functions = $functions;
2522
}
2623

2724
public function analyze(): AnalysisResult
2825
{
29-
return new AnalysisResult(array_map(fn($f) => $this->analyzeFunction($f), $this->functions));
26+
return new AnalysisResult($this->filename, array_map(fn($f) => $this->analyzeFunction($f), $this->functions));
3027
}
3128

3229
private function analyzeFunction(Func $function): Scope

src/Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function run(array $argv): void
3232
return;
3333
}
3434
$parseResult = $parser->parse($content);
35-
$analyzer = new VariableAnalyzer($parseResult->functions);
35+
$analyzer = new VariableAnalyzer($filePath, $parseResult->functions);
3636
$result = $analyzer->analyze();
3737
echo $result->format();
3838
}

test/VariableAnalizerTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ public function testAnalyzeFunctionSimple(): void
1616
$func->addVariable(new VarReference('a', 2));
1717
$func->addVariable(new VarReference('a', 3));
1818

19-
$sut = new VariableAnalyzer([$func]);
19+
$sut = new VariableAnalyzer('target.php', [$func]);
2020
$result = $sut->analyze();
21+
$this->assertSame('target.php', $result->filename);
2122
$scopes = $result->scopes;
2223

2324
$this->assertCount(1, $scopes);
@@ -32,8 +33,9 @@ public function testAnalyzeFunctionLong(): void
3233
$func->addVariable(new VarReference('a', 2));
3334
$func->addVariable(new VarReference('a', 100));
3435

35-
$sut = new VariableAnalyzer([$func]);
36+
$sut = new VariableAnalyzer('target.php', [$func]);
3637
$result = $sut->analyze();
38+
$this->assertSame('target.php', $result->filename);
3739
$scopes = $result->scopes;
3840

3941
$this->assertCount(1, $scopes);
@@ -48,8 +50,9 @@ public function testAnalyzeFunctionLongAssignedVariable(): void
4850
$func->addVariable(new VarReference('a', 2));
4951
$func->addVariable(new VarReference('a', 100));
5052

51-
$sut = new VariableAnalyzer([$func]);
53+
$sut = new VariableAnalyzer('target.php', [$func]);
5254
$result = $sut->analyze();
55+
$this->assertSame('target.php', $result->filename);
5356
$scopes = $result->scopes;
5457

5558
$this->assertCount(1, $scopes);
@@ -64,8 +67,9 @@ public function testAnalyzeFunctionLongMultipleAssignedVariable(): void
6467
$func->addVariable(new VarReference('a', 2));
6568
$func->addVariable(new VarReference('a', 100, true));
6669

67-
$sut = new VariableAnalyzer([$func]);
70+
$sut = new VariableAnalyzer('target.php', [$func]);
6871
$result = $sut->analyze();
72+
$this->assertSame('target.php', $result->filename);
6973
$scopes = $result->scopes;
7074

7175
$this->assertCount(1, $scopes);

0 commit comments

Comments
 (0)