|
6 | 6 |
|
7 | 7 | use Phauthentic\CognitiveCodeAnalysis\Business\Halstead\HalsteadMetrics; |
8 | 8 | use Phauthentic\CognitiveCodeAnalysis\Business\Cyclomatic\CyclomaticMetrics; |
| 9 | +use Phauthentic\CognitiveCodeAnalysis\Business\Understandability\UnderstandabilityMetrics; |
9 | 10 | use InvalidArgumentException; |
10 | 11 | use JsonSerializable; |
11 | 12 |
|
@@ -69,6 +70,7 @@ class CognitiveMetrics implements JsonSerializable |
69 | 70 |
|
70 | 71 | private ?HalsteadMetrics $halstead = null; |
71 | 72 | private ?CyclomaticMetrics $cyclomatic = null; |
| 73 | + private ?UnderstandabilityMetrics $understandability = null; |
72 | 74 | private ?float $coverage = null; |
73 | 75 |
|
74 | 76 | /** |
@@ -105,25 +107,29 @@ public function __construct(array $metrics) |
105 | 107 | ]); |
106 | 108 | } |
107 | 109 |
|
108 | | - if (!isset($metrics['cyclomatic_complexity'])) { |
109 | | - // Handle baseline format with individual cyclomatic fields |
110 | | - if (isset($metrics['cyclomaticComplexity']) && !isset($metrics['cyclomatic_complexity'])) { |
111 | | - $riskLevel = $metrics['cyclomaticRiskLevel'] ?? 'unknown'; |
112 | | - $this->cyclomatic = new CyclomaticMetrics([ |
113 | | - 'complexity' => $this->resolveIntValue($metrics['cyclomaticComplexity'], 1), |
114 | | - 'riskLevel' => is_string($riskLevel) ? $riskLevel : 'unknown', |
115 | | - ]); |
116 | | - } |
117 | | - return; |
| 110 | + if (isset($metrics['cyclomatic_complexity']) && is_array($metrics['cyclomatic_complexity'])) { |
| 111 | + /** @var array<string, mixed> $cyclomaticData */ |
| 112 | + $cyclomaticData = $metrics['cyclomatic_complexity']; |
| 113 | + $this->cyclomatic = new CyclomaticMetrics($cyclomaticData); |
| 114 | + } elseif (isset($metrics['cyclomaticComplexity'])) { |
| 115 | + $riskLevel = $metrics['cyclomaticRiskLevel'] ?? 'unknown'; |
| 116 | + $this->cyclomatic = new CyclomaticMetrics([ |
| 117 | + 'complexity' => $this->resolveIntValue($metrics['cyclomaticComplexity'], 1), |
| 118 | + 'riskLevel' => is_string($riskLevel) ? $riskLevel : 'unknown', |
| 119 | + ]); |
118 | 120 | } |
119 | 121 |
|
120 | | - if (!is_array($metrics['cyclomatic_complexity'])) { |
121 | | - return; |
| 122 | + if (isset($metrics['understandability']) && is_array($metrics['understandability'])) { |
| 123 | + /** @var array<string, mixed> $understandabilityData */ |
| 124 | + $understandabilityData = $metrics['understandability']; |
| 125 | + $this->understandability = new UnderstandabilityMetrics($understandabilityData); |
| 126 | + } elseif (isset($metrics['understandabilityComplexity'])) { |
| 127 | + $riskLevel = $metrics['understandabilityRiskLevel'] ?? 'unknown'; |
| 128 | + $this->understandability = new UnderstandabilityMetrics([ |
| 129 | + 'complexity' => $this->resolveIntValue($metrics['understandabilityComplexity'], 0), |
| 130 | + 'riskLevel' => is_string($riskLevel) ? $riskLevel : 'unknown', |
| 131 | + ]); |
122 | 132 | } |
123 | | - |
124 | | - /** @var array<string, mixed> $cyclomaticData */ |
125 | | - $cyclomaticData = $metrics['cyclomatic_complexity']; |
126 | | - $this->cyclomatic = new CyclomaticMetrics($cyclomaticData); |
127 | 133 | } |
128 | 134 |
|
129 | 135 | /** |
@@ -537,6 +543,11 @@ public function getCyclomatic(): ?CyclomaticMetrics |
537 | 543 | return $this->cyclomatic; |
538 | 544 | } |
539 | 545 |
|
| 546 | + public function getUnderstandability(): ?UnderstandabilityMetrics |
| 547 | + { |
| 548 | + return $this->understandability; |
| 549 | + } |
| 550 | + |
540 | 551 | private function resolveStringValue(mixed $value): string |
541 | 552 | { |
542 | 553 | if (!is_string($value)) { |
|
0 commit comments