DuplicateMatchCondition
warning native on by default quick-fix PHP ≥ 8.0Group: Probable bugs
Detect a later match arm condition statically strictly identical to an earlier condition, including resolved references to the same enum case.
Example
<?php
$v=match($input) {4=>'first',4=>'second',default=>'other'};<?php
$v=match($input) {4=>'first',default=>'other'};Reported:
- line 2: Remove the repeated match condition.
Detection
- Report a later match arm condition statically strictly identical to an earlier condition, including resolved references to the same enum case.
- Resolve builtin calls and named arguments before checking the contract. Project-local callees are considered only when their bodies or immutable summaries establish the required facts. The default is enabled, with positive proof required.
- For flow-dependent conditions, require a reachable source-to-use path with no intervening invalidation. Unknown calls, ambiguous aliases or recursion cannot establish definite facts. When the proof is inconclusive, nothing is reported.
- A variable condition takes part only when it is a local of a braced function, method or closure body bound by exactly one plain
=assignment of the variable itself, the assigned expression reads no variable, and the resolved value of that assignment is known. Two conditions repeated inside the same arm are reported, but they do not make the arm unreachable.
Exceptions (no report)
- Unknown values, different strict types and effectful condition expressions are excluded.
- A variable condition is ignored when the variable has any other binding in its scope:
++/--, a compound, by-reference or destructuring assignment, a second assignment, acatch,foreach,global,staticorunsetbinding, or a by-reference closure capture. It is ignored as well when its single assignment copies or computes from another variable ($a = $b), because that variable may itself have changed. It is also ignored in top-level code and arrow functions, and when the scope contains a variable-variable,extract(),evalor an include, since those can rebind a local by name. - Unresolved targets, incomplete required facts, suppressed findings and PHP versions lacking the referenced API or language feature produce no report. User declarations shadowing builtin names are not builtin contracts.
Fix
Remove the arm, with one adjacent comma, only when the whole arm is unreachable: every one of its conditions has a known value already listed by an earlier arm. A condition repeated only within its own arm, or sharing its arm with a new or unknown condition, gets no fix. An arm with several conditions carries the removal on its first condition only. No fix is offered when the removed text contains a comment.
A fix is offered only when the concrete prerequisites above are proven. Edits use exact source byte ranges, preserve comments and argument evaluation order, and introduce syntax supported by the configured PHP version. Never invent error-handling policy.
PHP versions
Available from PHP 8.0. Builtin behavior and argument contracts follow the configured PHP version. Extension-specific calls require resolved extension API symbols; do not treat same-named application functions as extension calls.
Options
None. Enable or disable this rule through normal rule configuration.
Configure
In custos.json:
{
"rules": {
"DuplicateMatchCondition": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
// @custos-ignore DuplicateMatchCondition