CyclomaticComplexity (CC)

メソッドに分岐(if、else、switch、for、while、catchなど)が多すぎる場合に発生します。

<?php
public function process(string $type, array $data): array
{
    if ($type === 'article') {
        if (isset($data['draft'])) {
            if ($data['draft'] === true) {
                // ...
            } else {
                // ...
            }
        } elseif (isset($data['scheduled'])) {
            // ...
        }
    } elseif ($type === 'page') {
        if ($data['template'] === 'full') {
            // ...
        } elseif ($data['template'] === 'sidebar') {
            // ...
        }
    } elseif ($type === 'widget') {
        // ...
    }
    // CC = 15
}

なぜ問題か

修正方法

Strategyパターンで条件分岐をポリモーフィズムに置き換える:

<?php
public function process(string $type, array $data): array
{
    $processor = $this->processorFactory->create($type);

    return $processor->process($data);
}

各プロセッサは自分のロジックだけを処理する:

<?php
class ArticleProcessor implements ProcessorInterface
{
    public function process(array $data): array
    {
        // article固有のロジックのみ
    }
}

しきい値

レベル CC値
🍝 Piccolo ≤10
🍝🍝 Medio 11-15
🍝🍝🍝 Grande 16-20
🍝🍝🍝🍝 Mamma Mia! 21+