Skip to content

Maintainability Rules

This document details the rules available in the Maintainability category.

Available Rules

RuleCode
Cyclomatic Complexitycyclomatic-complexity
Excessive Nestingexcessive-nesting
Excessive Parameter Listexcessive-parameter-list
Halsteadhalstead
Kan Defectkan-defect
No Boolean Flag Parameterno-boolean-flag-parameter
No Else Clauseno-else-clause
No Gotono-goto
Too Many Enum Casestoo-many-enum-cases
Too Many Methodstoo-many-methods
Too Many Propertiestoo-many-properties

cyclomatic-complexity

Checks the cyclomatic complexity of classes, traits, enums, interfaces, functions, and closures.

Cyclomatic complexity is a measure of the number of linearly independent paths through a program's source code.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"error"
thresholdinteger20

excessive-nesting

Checks if the nesting level in any block exceeds a configurable threshold.

Deeply nested code is harder to read, understand, and maintain. Consider refactoring to use early returns, helper methods, or clearer control flow.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"
thresholdinteger7

Examples

Correct Code

php
<?php

if ($condition) {
    while ($otherCondition) {
        echo "Hello"; // nesting depth = 2
    }
}

Incorrect Code

php
<?php

if ($a) {
    if ($b) {
        if ($c) {
            if ($d) {
                if ($e) {
                    if ($f) {
                        if ($g) {
                            if ($h) {
                                echo "Too deeply nested!";
                            }
                        }
                    }
                }
            }
        }
    }
}

excessive-parameter-list

Detects functions, closures, and methods with too many parameters.

If the number of parameters exceeds a configurable threshold, an issue is reported.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"error"
thresholdinteger5

halstead

Computes Halstead metrics (volume, difficulty, effort) and reports if they exceed configured thresholds.

Halstead metrics are calculated by counting operators and operands in the analyzed code. For more info: https://en.wikipedia.org/wiki/Halstead_complexity_measures

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"
volume-thresholddouble1000
difficulty-thresholddouble12
effort-thresholddouble7000

kan-defect

Detects classes, traits, interfaces, functions, and closures with high kan defect.

The "Kan Defect" metric is a heuristic for estimating defect proneness in a class or similar structure. It counts control-flow statements (while, do, foreach, if, and switch) and sums them using a formula loosely based on the work of Stephen H. Kan.

References:

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"error"
thresholddouble1.9

no-boolean-flag-parameter

Flags function-like parameters that use a boolean type.

Boolean flag parameters can indicate a violation of the Single Responsibility Principle (SRP). Refactor by extracting the flag logic into its own class or method.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

function get_difference(string $a, string $b): string {
    // ...
}

function get_difference_case_insensitive(string $a, string $b): string {
    // ...
}

Incorrect Code

php
<?php

function get_difference(string $a, string $b, bool $ignore_case): string {
    // ...
}

no-else-clause

Flags if statements that include an else or elseif branch.

Using else or elseif can lead to deeply nested code and complex control flow. This can often be simplified by using early returns (guard clauses), which makes the code easier to read and maintain by reducing its cyclomatic complexity.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

function process($user) {
    if (!$user->isVerified()) {
        return; // Early return
    }

    // "Happy path" continues here
    $user->login();
}

Incorrect Code

php
<?php

function process($user) {
    if ($user->isVerified()) {
        // "Happy path" is nested
        $user->login();
    } else {
        // Logic is split across branches
        return;
    }
}

no-goto

Detects the use of goto statements and labels. The goto statement can make code harder to read, understand, and maintain. It can lead to "spaghetti code" and make it difficult to follow the flow of execution.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"note"

Examples

Correct Code

php
<?php

$i = 0;
while ($i < 10) {
    if ($i === 5) {
        break; // Structured control flow.
    }
    $i++;
}

Incorrect Code

php
<?php

$i = 0;
loop:
if ($i >= 10) {
    goto end;
}

$i++;
goto loop;
end:

too-many-enum-cases

Detects enums with too many cases.

This rule checks the number of cases in enums. If the number of cases exceeds a configurable threshold, an issue is reported.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"error"
thresholdinteger30

Examples

Correct Code

php
enum SimpleEnum {
    case A;
    case B;
    case C;
}

Incorrect Code

php
enum LargeEnum {
    case A;
    case B;
    case C;
    case D;
    case E;
    case F;
    case G;
    case H;
    case I;
    case J;
    case K;
    case L;
    case M;
    case N;
    case O;
    case P;
    case Q;
    case R;
    case S;
    case T;
    case U;
}

too-many-methods

Detects class-like structures with too many methods.

This rule checks the number of methods in classes, traits, enums, and interfaces. If the number of methods exceeds a configurable threshold, an issue is reported.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"error"
thresholdinteger10
count-hooksbooleanfalse
count-setters-and-gettersbooleanfalse

Examples

Correct Code

php
class SimpleClass {
    public function a() {}

    public function b() {}
}

Incorrect Code

php
class ComplexClass {
    public function a() {}
    public function b() {}
    public function c() {}
    public function d() {}
    public function e() {}
    public function f() {}
    public function g() {}
    public function h() {}
    public function i() {}
    public function j() {}
    public function k() {}
    public function l() {}
    public function m() {}
    public function n() {}
    public function o() {}
    public function p() {}
    public function q() {}
    public function r() {}
    public function s() {}
    public function t() {}
    public function u() {}
}

too-many-properties

Detects class-like structures with too many properties.

This rule checks the number of properties in classes, traits, and interfaces. If the number of properties exceeds a configurable threshold, an issue is reported.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"error"
thresholdinteger10

Examples

Correct Code

php
class SimpleClass {
    public $a;
    public $b;
}

Incorrect Code

php
class ComplexClass {
    public $a; public $b; public $c; public $d; public $e;
    public $f; public $g; public $h; public $i; public $j; public $k;
}