Code Readability Metrics
debt(d5/e5/b3/t5)
Closest to 'specialist tool catches' (d5), since PHPMD/phploc/SonarQube/CodeClimate are dedicated metrics tools that flag CC/LOC/params thresholds — not default linters.
Closest to 'touches multiple files / significant refactor in one component' (e5), because remediating poor readability metrics typically means refactoring multiple functions/classes flagged by the tool, not a one-line swap.
Closest to 'localised tax' (b3), since adopting metrics rules in CI imposes ongoing review/refactor pressure but doesn't shape system architecture; applies_to spans web/cli/queue but only at the policy layer.
Closest to 'notable trap' (t5), matching the misconception that low metrics guarantee readability — a documented gotcha (Goodhart's law on metrics) that teams learn through experience of gaming CC.
TL;DR
Explanation
Key metrics: Cyclomatic Complexity (CC) — control flow paths. Cognitive Complexity (CC2, SonarQube) — mental effort to understand, penalises nesting more than CC. Lines of Code per function (LOC) — under 20 is ideal. Parameter count — under 4 parameters. Nesting depth — under 3 levels. Halstead metrics — volume, difficulty, effort. Maintainability Index (0–100, VS Code, Visual Studio) — composite metric. Tools: PHPStan (complexity), PHPMD, SonarQube, CodeClimate, phploc (PHP Lines of Code). These are guides, not absolute rules — context matters.
Common Misconception
Why It Matters
Common Mistakes
- Optimising for metrics rather than readability — gaming CC by moving code into helper functions that don't improve clarity.
- Treating metrics as hard limits rather than signals for review.
- Not setting baseline metrics for a codebase and tracking change over time.
Code Examples
// High CC, high nesting, short but unreadable:
function p($u,$a){if($u&&$a){if($u->r>0){if($a<100){return $u->r*$a;}}}return 0;}
function calculateDiscount(User $user, float $orderAmount): float {
if (!$user || $orderAmount <= 0) return 0.0;
if (!$user->hasActiveDiscount()) return 0.0;
return $user->discountRate * $orderAmount;
}