diff --git a/CHANGELOG.md b/CHANGELOG.md index c88fa6e..4256316 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Fixed command execution to properly return error codes when failures occur * Improved error handling in all command implementations + * fix empty scope error. #11 ## v0.0.4 (2025-03-20) diff --git a/src/Analyze/AnalysisResult.php b/src/Analyze/AnalysisResult.php index 7efa880..5f28167 100644 --- a/src/Analyze/AnalysisResult.php +++ b/src/Analyze/AnalysisResult.php @@ -17,10 +17,11 @@ public function __construct( public readonly array $scopes ) { - $this->maxVariableHardUsage = max(array_map(fn(Scope $scope) => $scope->getVariableHardUsage(), $scopes)); if (count($scopes) === 0) { + $this->maxVariableHardUsage = 0; $this->avarageVariableHardUsage = 0; } else { + $this->maxVariableHardUsage = max(array_map(fn(Scope $scope) => $scope->getVariableHardUsage(), $scopes)); $this->avarageVariableHardUsage = array_sum(array_map(fn(Scope $scope) => $scope->getVariableHardUsage(), $scopes)) / count($scopes); } } diff --git a/test/VariableAnalizerTest.php b/test/VariableAnalizerTest.php index 56c9690..933fcbe 100644 --- a/test/VariableAnalizerTest.php +++ b/test/VariableAnalizerTest.php @@ -9,6 +9,16 @@ class VariableAnalizerTest extends TestCase { + public function testAnalyzeEmpty(): void + { + $sut = new VariableAnalyzer('target.php', []); + $result = $sut->analyze(); + $this->assertSame('target.php', $result->filename); + $scopes = $result->scopes; + + $this->assertCount(0, $scopes); + } + public function testAnalyzeFunctionSimple(): void { $func = new Func(null, 'testFunction');