-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added method and class method average cyclomatic complexity ins…
…ights
- Loading branch information
1 parent
f476219
commit 56d61bd
Showing
11 changed files
with
567 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/Domain/Insights/ClassMethodAverageCyclomaticComplexityIsHigh.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\PhpInsights\Domain\Insights; | ||
|
||
use NunoMaduro\PhpInsights\Domain\Contracts\GlobalInsight; | ||
use NunoMaduro\PhpInsights\Domain\Contracts\HasDetails; | ||
use NunoMaduro\PhpInsights\Domain\Details; | ||
|
||
/** | ||
* @see \Tests\Domain\Insights\ClassMethodAverageCyclomaticComplexityIsHighTest | ||
*/ | ||
final class ClassMethodAverageCyclomaticComplexityIsHigh extends Insight implements HasDetails, GlobalInsight | ||
{ | ||
/** | ||
* @var array<Details> | ||
*/ | ||
private array $details = []; | ||
|
||
public function hasIssue(): bool | ||
{ | ||
return $this->details !== []; | ||
} | ||
|
||
public function getTitle(): string | ||
{ | ||
return sprintf( | ||
'Having `classes` with average method cyclomatic complexity more than %s is prohibited - Consider refactoring', | ||
$this->getMaxComplexity() | ||
); | ||
} | ||
|
||
/** | ||
* @return array<int, Details> | ||
*/ | ||
public function getDetails(): array | ||
{ | ||
return $this->details; | ||
} | ||
|
||
public function process(): void | ||
{ | ||
// Exclude in collector all excluded files | ||
if ($this->excludedFiles !== []) { | ||
$this->collector->excludeComplexityFiles($this->excludedFiles); | ||
} | ||
|
||
$averageClassComplexity = $this->getAverageClassComplexity(); | ||
|
||
// Exclude the ones which didn't pass the threshold | ||
$complexityLimit = $this->getMaxComplexity(); | ||
$averageClassComplexity = array_filter( | ||
$averageClassComplexity, | ||
static fn ($complexity): bool => $complexity > $complexityLimit | ||
); | ||
|
||
$this->details = array_map( | ||
static fn ($class, $complexity): Details => Details::make() | ||
->setFile($class) | ||
->setMessage(sprintf('%.2f cyclomatic complexity', $complexity)), | ||
array_keys($averageClassComplexity), | ||
$averageClassComplexity | ||
); | ||
} | ||
|
||
private function getMaxComplexity(): float | ||
{ | ||
return (float) ($this->config['maxClassMethodAverageComplexity'] ?? 5.0); | ||
} | ||
|
||
private function getFile(string $classMethod): string | ||
{ | ||
$colonPosition = strpos($classMethod, ':'); | ||
|
||
if ($colonPosition !== false) { | ||
return substr($classMethod, 0, $colonPosition); | ||
} | ||
|
||
return $classMethod; | ||
} | ||
|
||
/** | ||
* @return array<string, float> | ||
*/ | ||
private function getAverageClassComplexity(): array | ||
{ | ||
// Group method complexities by files | ||
$classComplexities = []; | ||
|
||
foreach ($this->collector->getMethodComplexity() as $classMethod => $complexity) { | ||
$classComplexities[$this->getFile($classMethod)][] = $complexity; | ||
} | ||
|
||
// Calculate average complexity of each file | ||
$averageClassComplexity = []; | ||
|
||
foreach ($classComplexities as $file => $complexities) { | ||
$averageClassComplexity[$file] = array_sum($complexities) / count($complexities); | ||
} | ||
|
||
return $averageClassComplexity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\PhpInsights\Domain\Insights; | ||
|
||
use NunoMaduro\PhpInsights\Domain\Contracts\GlobalInsight; | ||
use NunoMaduro\PhpInsights\Domain\Contracts\HasDetails; | ||
use NunoMaduro\PhpInsights\Domain\Details; | ||
|
||
/** | ||
* @see \Tests\Domain\Insights\MethodCyclomaticComplexityIsHighTest | ||
*/ | ||
final class MethodCyclomaticComplexityIsHigh extends Insight implements HasDetails, GlobalInsight | ||
{ | ||
/** | ||
* @var array<Details> | ||
*/ | ||
private array $details = []; | ||
|
||
public function hasIssue(): bool | ||
{ | ||
return $this->details !== []; | ||
} | ||
|
||
public function getTitle(): string | ||
{ | ||
return sprintf( | ||
'Having `methods` with cyclomatic complexity more than %s is prohibited - Consider refactoring', | ||
$this->getMaxComplexity() | ||
); | ||
} | ||
|
||
/** | ||
* @return array<int, Details> | ||
*/ | ||
public function getDetails(): array | ||
{ | ||
return $this->details; | ||
} | ||
|
||
public function process(): void | ||
{ | ||
// Exclude in collector all excluded files | ||
if ($this->excludedFiles !== []) { | ||
$this->collector->excludeComplexityFiles($this->excludedFiles); | ||
} | ||
$complexityLimit = $this->getMaxComplexity(); | ||
|
||
$methodComplexity = array_filter( | ||
$this->collector->getMethodComplexity(), | ||
static fn ($complexity): bool => $complexity > $complexityLimit | ||
); | ||
|
||
$this->details = array_map( | ||
fn ($class, $complexity): Details => $this->getDetailsForClassMethod($class, $complexity), | ||
array_keys($methodComplexity), | ||
$methodComplexity | ||
); | ||
} | ||
|
||
private function getMaxComplexity(): int | ||
{ | ||
return (int) ($this->config['maxMethodComplexity'] ?? 5); | ||
} | ||
|
||
private function getDetailsForClassMethod(string $class, int $complexity): Details | ||
{ | ||
$file = $class; | ||
$function = null; | ||
$colonPosition = strpos($class, ':'); | ||
|
||
if ($colonPosition !== false) { | ||
$file = substr($class, 0, $colonPosition); | ||
$function = substr($class, $colonPosition + 1); | ||
} | ||
|
||
$details = Details::make() | ||
->setFile($file) | ||
->setMessage(sprintf('%d cyclomatic complexity', $complexity)); | ||
|
||
if ($function !== null) { | ||
$details->setFunction($function); | ||
} | ||
|
||
return $details; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.