-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnalysisResult.php
More file actions
42 lines (37 loc) · 1.3 KB
/
AnalysisResult.php
File metadata and controls
42 lines (37 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
declare(strict_types=1);
namespace Smeghead\PhpVariableHardUsage\Analyze;
final class AnalysisResult
{
public readonly int $maxVariableHardUsage;
public readonly float $avarageVariableHardUsage;
/**
* @param list<Scope> $scopes
*/
public function __construct(
public readonly string $filename,
public readonly array $scopes
)
{
$this->maxVariableHardUsage = max(array_map(fn(Scope $scope) => $scope->getVariableHardUsage(), $scopes));
if (count($scopes) === 0) {
$this->avarageVariableHardUsage = 0;
} else {
$this->avarageVariableHardUsage = array_sum(array_map(fn(Scope $scope) => $scope->getVariableHardUsage(), $scopes)) / count($scopes);
}
}
public function format(): string
{
$output = [
'filename' => $this->filename,
'maxVariableHardUsage' => $this->maxVariableHardUsage,
'avarageVariableHardUsage' => $this->avarageVariableHardUsage,
];
$output['scopes'] = array_map(fn(Scope $scope) => [
'namespace' => $scope->namespace,
'name' => $scope->name,
'variableHardUsage' => $scope->getVariableHardUsage()
], $this->scopes);
return json_encode($output, JSON_PRETTY_PRINT) . PHP_EOL;
}
}