1.2.747. Multiple Type Cases In Switch¶
This reports switch() instructions, which have several types in cases.
This might generate compatibility errors, as the comparison may succeed in different ways, depending on PHP versions. This is particularly the case for PHP 8.0, and values such as ‘0’, ‘’, 0, null <https://www.php.net/`null>`_, and false. This situation doesn’t affect match(), as it uses a strict type comparison, unlike switch().
<?php
switch($a) {
case 1:
break;
case 'a':
break;
}
?>
1.2.747.1. Connex PHP features¶
1.2.747.1.1. Suggestions¶
Make all the types identical in the cases.
Switch to match() call, to include a type check
1.2.747.1.2. Specs¶
Short name |
Structures/MultipleTypeCasesInSwitch |
Rulesets |
|
Exakat since |
2.5.1 |
PHP Version |
With PHP 8.0 and more recent |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
High |
Available in |