Skip to content

Commit 9becb56

Browse files
authored
Merge pull request #3 from smeghead/fix/remove-scope-interface
remove Scope interface.
2 parents 087dde2 + 9892369 commit 9becb56

File tree

6 files changed

+32
-52
lines changed

6 files changed

+32
-52
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
### Bug fix
4+
5+
* Refactor remove interface Scope
6+
37
## v0.0.1 (2025-03-18)
48

59
### Features

src/Analyze/AnalysisResult.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public function format(): string
3131
'avarageVariableHardUsage' => $this->avarageVariableHardUsage,
3232
];
3333
$output['scopes'] = array_map(fn(Scope $scope) => [
34-
'namespace' => $scope->getNamespace(),
35-
'name' => $scope->getName(),
34+
'namespace' => $scope->namespace,
35+
'name' => $scope->name,
3636
'variableHardUsage' => $scope->getVariableHardUsage()
3737
], $this->scopes);
3838
return json_encode($output, JSON_PRETTY_PRINT) . PHP_EOL;

src/Analyze/FunctionScope.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/Analyze/Scope.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,33 @@
44

55
namespace Smeghead\PhpVariableHardUsage\Analyze;
66

7-
interface Scope
7+
final class Scope
88
{
9-
public function getNamespace(): ?string;
10-
public function getName(): string;
9+
/** @var list<AnalyzedVariable> */
10+
private array $analyzedVariables;
11+
12+
/**
13+
* @param list<AnalyzedVariable> $analyzedVariables
14+
*/
15+
public function __construct(
16+
public readonly ?string $namespace,
17+
public readonly string $name,
18+
array $analyzedVariables
19+
)
20+
{
21+
$this->analyzedVariables = $analyzedVariables;
22+
}
1123

1224
/**
1325
* @return list<AnalyzedVariable>
1426
*/
15-
public function getAnalyzedVariables(): array;
27+
public function getAnalyzedVariables(): array
28+
{
29+
return $this->analyzedVariables;
30+
}
1631

17-
public function getVariableHardUsage(): int;
32+
public function getVariableHardUsage(): int
33+
{
34+
return array_sum(array_map(fn(AnalyzedVariable $variable) => $variable->variableHardUsage, $this->analyzedVariables));
35+
}
1836
}

src/Analyze/VariableAnalyzer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private function analyzeFunction(Func $function): Scope
4040
$analyzedVars[] = new AnalyzedVariable($variableName, $variableHardUsage);
4141
}
4242

43-
return new FunctionScope($function->namespace, $function->name, $analyzedVars);
43+
return new Scope($function->namespace, $function->name, $analyzedVars);
4444
}
4545

4646
/**

test/VariableAnalizerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testAnalyzeFunctionSimple(): void
2121
$scopes = $result->scopes;
2222

2323
$this->assertCount(1, $scopes);
24-
$this->assertSame('testFunction', $scopes[0]->getName());
24+
$this->assertSame('testFunction', $scopes[0]->name);
2525
$this->assertSame(2, $scopes[0]->getAnalyzedVariables()[0]->variableHardUsage);
2626
}
2727

@@ -37,7 +37,7 @@ public function testAnalyzeFunctionLong(): void
3737
$scopes = $result->scopes;
3838

3939
$this->assertCount(1, $scopes);
40-
$this->assertSame('testFunction', $scopes[0]->getName());
40+
$this->assertSame('testFunction', $scopes[0]->name);
4141
// (1 + 2 + 100) / 3 = 34
4242
// abs(34 - 1) + abs(34 - 2) + abs(34 - 100) = 33 + 32 + 66 = 131
4343
$this->assertSame(131, $scopes[0]->getAnalyzedVariables()[0]->variableHardUsage);

0 commit comments

Comments
 (0)