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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

### Bug fix

* Refactor remove interface Scope

## v0.0.1 (2025-03-18)

### Features
Expand Down
4 changes: 2 additions & 2 deletions src/Analyze/AnalysisResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function format(): string
'avarageVariableHardUsage' => $this->avarageVariableHardUsage,
];
$output['scopes'] = array_map(fn(Scope $scope) => [
'namespace' => $scope->getNamespace(),
'name' => $scope->getName(),
'namespace' => $scope->namespace,
'name' => $scope->name,
'variableHardUsage' => $scope->getVariableHardUsage()
], $this->scopes);
return json_encode($output, JSON_PRETTY_PRINT) . PHP_EOL;
Expand Down
42 changes: 0 additions & 42 deletions src/Analyze/FunctionScope.php

This file was deleted.

28 changes: 23 additions & 5 deletions src/Analyze/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,33 @@

namespace Smeghead\PhpVariableHardUsage\Analyze;

interface Scope
final class Scope
{
public function getNamespace(): ?string;
public function getName(): string;
/** @var list<AnalyzedVariable> */
private array $analyzedVariables;

/**
* @param list<AnalyzedVariable> $analyzedVariables
*/
public function __construct(
public readonly ?string $namespace,
public readonly string $name,
array $analyzedVariables
)
{
$this->analyzedVariables = $analyzedVariables;
}

/**
* @return list<AnalyzedVariable>
*/
public function getAnalyzedVariables(): array;
public function getAnalyzedVariables(): array
{
return $this->analyzedVariables;
}

public function getVariableHardUsage(): int;
public function getVariableHardUsage(): int
{
return array_sum(array_map(fn(AnalyzedVariable $variable) => $variable->variableHardUsage, $this->analyzedVariables));
}
}
2 changes: 1 addition & 1 deletion src/Analyze/VariableAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private function analyzeFunction(Func $function): Scope
$analyzedVars[] = new AnalyzedVariable($variableName, $variableHardUsage);
}

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

/**
Expand Down
4 changes: 2 additions & 2 deletions test/VariableAnalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testAnalyzeFunctionSimple(): void
$scopes = $result->scopes;

$this->assertCount(1, $scopes);
$this->assertSame('testFunction', $scopes[0]->getName());
$this->assertSame('testFunction', $scopes[0]->name);
$this->assertSame(2, $scopes[0]->getAnalyzedVariables()[0]->variableHardUsage);
}

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

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