-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUnderstandabilityCalculator.php
More file actions
70 lines (59 loc) · 1.97 KB
/
Copy pathUnderstandabilityCalculator.php
File metadata and controls
70 lines (59 loc) · 1.97 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
namespace Phauthentic\CognitiveCodeAnalysis\Business\Understandability;
class UnderstandabilityCalculator implements UnderstandabilityCalculatorInterface
{
/**
* @param array<string, int> $incrementCounts
*/
public function calculateComplexity(array $incrementCounts): int
{
return $incrementCounts['total'] ?? 0;
}
/**
* @param array<string, int> $incrementCounts
* @return array<string, int>
*/
public function createBreakdown(array $incrementCounts, int $totalComplexity): array
{
return array_merge(['total' => $totalComplexity], $incrementCounts);
}
public function getRiskLevel(int $complexity): string
{
return match (true) {
$complexity <= 5 => 'low',
$complexity <= 10 => 'medium',
$complexity <= 15 => 'high',
default => 'very_high',
};
}
/**
* @param array<string, int> $methodComplexities
* @param array<string, array<string, int>> $methodBreakdowns
* @return array<string, mixed>
*/
public function createSummary(array $methodComplexities, array $methodBreakdowns): array
{
$summary = [
'methods' => [],
'high_risk_methods' => [],
'very_high_risk_methods' => [],
];
foreach ($methodComplexities as $methodKey => $complexity) {
$riskLevel = $this->getRiskLevel($complexity);
$summary['methods'][$methodKey] = [
'complexity' => $complexity,
'risk_level' => $riskLevel,
'breakdown' => $methodBreakdowns[$methodKey] ?? [],
];
if ($complexity >= 10) {
$summary['high_risk_methods'][$methodKey] = $complexity;
}
if ($complexity < 15) {
continue;
}
$summary['very_high_risk_methods'][$methodKey] = $complexity;
}
return $summary;
}
}