-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phauthentic\CodeQualityMetrics\Business\Cognitive; | ||
|
||
use RuntimeException; | ||
|
||
/** | ||
* | ||
*/ | ||
class BaselineService | ||
{ | ||
/** | ||
* @param CognitiveMetricsCollection $metricsCollection | ||
* @param array<string, array<string, mixed>> $baseline | ||
*/ | ||
public function calculateDeltas(CognitiveMetricsCollection $metricsCollection, array $baseline): void | ||
{ | ||
foreach ($baseline as $class => $data) { | ||
foreach ($data['methods'] as $methodName => $methodData) { | ||
$metrics = $metricsCollection->getClassWithMethod($class, $methodName); | ||
if (!$metrics) { | ||
continue; | ||
} | ||
|
||
$previousMetrics = CognitiveMetrics::fromArray($methodData); | ||
$metrics->calculateDeltas($previousMetrics); | ||
} | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Loads the baseline file and returns the data as an array. | ||
* | ||
* @param string $baselineFile | ||
* @return array<string, array<string, mixed>> $baseline | ||
* @throws \JsonException | ||
*/ | ||
public function loadBaseline(string $baselineFile): array | ||
{ | ||
if (!file_exists($baselineFile)) { | ||
throw new RuntimeException('Baseline file does not exist.'); | ||
} | ||
|
||
$baseline = file_get_contents($baselineFile); | ||
if ($baseline === false) { | ||
throw new RuntimeException('Failed to read baseline file.'); | ||
} | ||
|
||
return json_decode($baseline, true, 512, JSON_THROW_ON_ERROR); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,23 @@ | |
*/ | ||
class CognitiveMetrics implements JsonSerializable | ||
{ | ||
private string $class = ''; | ||
private string $method = ''; | ||
/** | ||
* @var array<int, string> | ||
*/ | ||
private array $metrics = [ | ||
'lineCount', | ||
'argCount', | ||
'returnCount', | ||
'variableCount', | ||
'propertyCallCount', | ||
'ifCount', | ||
'ifNestingLevel', | ||
'elseCount' | ||
]; | ||
|
||
private string $class; | ||
private string $method; | ||
|
||
private int $lineCount = 0; | ||
private int $argCount = 0; | ||
private int $returnCount = 0; | ||
|
@@ -49,25 +64,37 @@ public function __construct(array $metrics) | |
{ | ||
$this->assertArrayKeyIsPresent($metrics, 'class'); | ||
Check warning on line 65 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
Check warning on line 65 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
|
||
$this->assertArrayKeyIsPresent($metrics, 'method'); | ||
Check warning on line 66 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
Check warning on line 66 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
|
||
$this->assertArrayKeyIsPresent($metrics, 'lineCount'); | ||
$this->assertArrayKeyIsPresent($metrics, 'argCount'); | ||
$this->assertArrayKeyIsPresent($metrics, 'returnCount'); | ||
$this->assertArrayKeyIsPresent($metrics, 'variableCount'); | ||
$this->assertArrayKeyIsPresent($metrics, 'propertyCallCount'); | ||
$this->assertArrayKeyIsPresent($metrics, 'ifCount'); | ||
$this->assertArrayKeyIsPresent($metrics, 'ifNestingLevel'); | ||
$this->assertArrayKeyIsPresent($metrics, 'elseCount'); | ||
|
||
$this->class = $metrics['class']; | ||
$this->method = $metrics['method']; | ||
$this->lineCount = $metrics['lineCount']; | ||
$this->argCount = $metrics['argCount']; | ||
$this->returnCount = $metrics['returnCount']; | ||
$this->variableCount = $metrics['variableCount']; | ||
$this->propertyCallCount = $metrics['propertyCallCount']; | ||
$this->ifCount = $metrics['ifCount']; | ||
$this->ifNestingLevel = $metrics['ifNestingLevel']; | ||
$this->elseCount = $metrics['elseCount']; | ||
$this->class = $metrics['class']; | ||
|
||
$this->setRequiredMetricProperties($metrics); | ||
$this->setOptionalMetricProperties($metrics); | ||
Check warning on line 71 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
Check warning on line 71 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
|
||
} | ||
|
||
/** | ||
* @param array<string, mixed> $metrics | ||
* @return void | ||
*/ | ||
private function setRequiredMetricProperties(array $metrics): void | ||
{ | ||
foreach ($this->metrics as $metricName) { | ||
$this->assertArrayKeyIsPresent($metrics, $metricName); | ||
Check warning on line 81 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
Check warning on line 81 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
|
||
$this->$metricName = $metrics[$metricName]; | ||
} | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $metrics | ||
* @return void | ||
*/ | ||
private function setOptionalMetricProperties(array $metrics): void | ||
{ | ||
foreach ($this->metrics as $metricName) { | ||
Check warning on line 92 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
Check warning on line 92 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
|
||
$property = $metricName . 'Weight'; | ||
Check warning on line 93 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
Check warning on line 93 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
Check warning on line 93 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
Check warning on line 93 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
Check warning on line 93 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
Check warning on line 93 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
|
||
if (array_key_exists($property, $metrics)) { | ||
$this->$property = $metrics[$property]; | ||
} | ||
} | ||
} | ||
|
||
private function assertSame(self $other): void | ||
|
@@ -93,17 +120,16 @@ public function calculateDeltas(self $other): void | |
{ | ||
$this->assertSame($other); | ||
Check warning on line 121 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
Check warning on line 121 in src/Business/Cognitive/CognitiveMetrics.php GitHub Actions / Coding Standard & Static Analysis
|
||
|
||
$this->lineCountWeightDelta = new Delta($this->lineCountWeight, $other->getLineCountWeight()); | ||
$this->argCountWeightDelta = new Delta($this->argCountWeight, $other->getArgCountWeight()); | ||
$this->returnCountWeightDelta = new Delta($this->returnCountWeight, $other->getReturnCountWeight()); | ||
$this->variableCountWeightDelta = new Delta($this->variableCountWeight, $other->getVariableCountWeight()); | ||
$this->propertyCallCountWeightDelta = new Delta($this->propertyCallCountWeight, $other->getPropertyCallCountWeight()); | ||
$this->ifCountWeightDelta = new Delta($this->ifCountWeight, $other->getIfCountWeight()); | ||
$this->ifNestingLevelWeightDelta = new Delta($this->ifNestingLevelWeight, $other->getIfNestingLevelWeight()); | ||
$this->elseCountWeightDelta = new Delta($this->elseCountWeight, $other->getElseCountWeight()); | ||
$this->lineCountWeightDelta = new Delta($other->getLineCountWeight(), $this->lineCountWeight); | ||
$this->argCountWeightDelta = new Delta($other->getArgCountWeight(), $this->argCountWeight); | ||
$this->returnCountWeightDelta = new Delta($other->getReturnCountWeight(), $this->returnCountWeight); | ||
$this->variableCountWeightDelta = new Delta($other->getVariableCountWeight(), $this->variableCountWeight); | ||
$this->propertyCallCountWeightDelta = new Delta($other->getPropertyCallCountWeight(), $this->propertyCallCountWeight); | ||
$this->ifCountWeightDelta = new Delta($other->getIfCountWeight(), $this->ifCountWeight); | ||
$this->ifNestingLevelWeightDelta = new Delta($other->getIfNestingLevelWeight(), $this->ifNestingLevelWeight); | ||
$this->elseCountWeightDelta = new Delta($other->getElseCountWeight(), $this->elseCountWeight); | ||
} | ||
|
||
|
||
/** | ||
* @param array<string, mixed> $metrics | ||
* @return self | ||
|