Simplifying Complex Conditional Logic
debt(d5/e3/b3/t5)
Closest to 'specialist tool catches it' (d5). The detection_hints list phpmd and eslint — phpmd is a specialist static analysis tool for PHP complexity metrics, and eslint requires configuration for complexity rules. These tools can flag deeply nested or overly complex conditionals but won't catch all cases (e.g., semantically confusing but syntactically simple ternary chains), placing this between default linter (d3) and specialist tool (d5), landing at d5.
Closest to 'simple parameterised fix' (e3). The quick_fix describes applying De Morgan's laws, extracting boolean expressions to named predicates, and using guard clauses — these are pattern-based refactors within a single function or component, not single-line patches but not cross-cutting changes either. This aligns with e3.
Closest to 'localised tax' (b3). The applies_to covers web, cli, and queue-worker broadly, but the burden of complex conditionals is localised to the specific function or module where they appear. Other parts of the codebase are largely unaffected unless the conditional is in a shared utility. Tags (style, readability, refactoring) confirm this is a localised maintainability concern, not an architectural one.
Closest to 'notable trap' (t5). The misconception explicitly states that developers believe shorter conditionals are always simpler — a one-line ternary chain looks cleaner but may be far harder to understand. This is a documented gotcha (conflating brevity with clarity) that many developers eventually learn, matching the t5 anchor. It's not catastrophic or contradicting another language's semantics, so t7 is too high.
TL;DR
Explanation
Techniques: (1) De Morgan's laws: !($a && $b) === (!$a || !$b), useful for inverting. (2) Consolidate duplicate fragments from if/else branches into pre/post logic. (3) Decompose conditions into named predicate methods: isEligibleForDiscount(). (4) Replace nested ternaries with match or if/else. (5) Use guard clauses (early return) to flatten nesting. (6) Consolidate conditions: if ($a) { x(); } if ($b) { x(); } → if ($a || $b) { x(); }. (7) Remove negation: !$flag → $flag with inverted branch order. Each technique reduces cognitive load for the reader.
Common Misconception
Why It Matters
Common Mistakes
- Deeply nested ternaries — always prefer if/else or match for more than 2 levels.
- Negative conditions without good reason — affirmative conditions read more naturally.
- Duplicated code in both if and else branches that could be extracted.
Code Examples
if (!(!$user->isActive() || !$user->hasPermission())) {
doSomething();
}
// De Morgan's: !(!A || !B) === (A && B)
if ($user->isActive() && $user->hasPermission()) {
doSomething();
}
// Or extract to named predicate:
if ($user->canPerformAction()) {
doSomething();
}