1.2.295. Could Use Match¶
The switch() syntax use may be replaced by a match() call.
The simplest case for such refactoring is when each of the switch’s case (including default), assign one value to the same variable. See this below : Match() was introduced in PHP 8. It is not valid with older PHP versions.
<?php
switch($a) {
case 1:
$b = '1';
break;
case 2:
$b = '3';
break;
default:
$b = '0';
break;
}
/*
$b = match($a) {
1 => '1',
2 => '3',
default => '0'
};
*/
?>
See also Match().
1.2.295.1. Connex PHP features¶
1.2.295.1.1. Suggestions¶
Replace switch() with match()
1.2.295.1.2. Specs¶
Short name |
Structures/CouldUseMatch |
Rulesets |
|
Exakat since |
2.2.2 |
PHP Version |
With PHP 8.0 and more recent |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
High |
Available in |