Menu

← Back to switch.*

Error Identifier: switch.type

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

$value = 1;

switch ($value) {
	case 1:
		break;
	case 'test':
		break;
}

Why is it reported? #

This error is reported by the phpstan-strict-rules package.

A switch case condition has a type that is incompatible with the switch expression type. PHP’s switch statement uses loose comparison (==) to match case values. When the case value type does not overlap with the switch expression type, the case can never match through strict typing logic, even if PHP’s loose comparison might coerce the values.

In the example above, $value is int but the case 'test' is a string. These types are incompatible and the case will never match the intended value.

How to fix it #

Ensure all case conditions match the type of the switch expression:

 $value = 1;

 switch ($value) {
 	case 1:
 		break;
-	case 'test':
+	case 2:
 		break;
 }

If the switch expression can have multiple types, adjust its type declaration to reflect that:

-$value = 1;
+/** @var int|string $value */

 switch ($value) {
 	case 1:
 		break;
 	case 'test':
 		break;
 }

How to ignore this error #

You can use the identifier switch.type to ignore this error using a comment:

// @phpstan-ignore switch.type
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: switch.type

Rules that report this error #

  • PHPStan\Rules\SwitchConditions\MatchingTypeInSwitchCaseConditionRule [1] phpstan/phpstan-strict-rules

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.