1.2.739. 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, 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.739.1. Suggestions

  • Make all the types identical in the cases.

  • Switch to match() call, to include a type check

1.2.739.2. Specs

Short name

Structures/MultipleTypeCasesInSwitch

Rulesets

All, CompatibilityPHP80

Exakat since

2.5.1

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

switch

Available in

Entreprise Edition, Exakat Cloud