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);
$a = [3, 1, 2];
sort($a, SORT_NUMERIC | SORT_STRING);
Why is it reported? #
The constants SORT_NUMERIC and SORT_STRING belong to the same exclusive group — they represent alternative sorting strategies. Combining them with | is not meaningful because the function can only sort one way at a time. The resulting bitmask value produces undefined behavior.
Some function parameters accept bitmask flags, but certain subsets of those flags are mutually exclusive. For example, htmlspecialchars() has two exclusive groups within its $flags parameter: one for quote handling (ENT_QUOTES, ENT_NOQUOTES, ENT_COMPAT) and one for document type (ENT_HTML401, ENT_HTML5, ENT_XHTML, ENT_XML1). Combining flags from different groups is fine, but combining flags within the same group is an error.
This check is only performed on PHP 8.0+.
How to fix it #
Use only one constant from the exclusive group:
$a = [3, 1, 2];
-sort($a, SORT_NUMERIC | SORT_STRING);
+sort($a, SORT_NUMERIC);
Non-exclusive modifiers can still be combined with the chosen constant:
$a = [3, 1, 2];
-sort($a, SORT_NUMERIC | SORT_STRING);
+sort($a, SORT_STRING | SORT_FLAG_CASE);
How to ignore this error #
You can use the identifier argument.exclusiveConstants to ignore this error using a comment:
// @phpstan-ignore argument.exclusiveConstants
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: argument.exclusiveConstants
Rules that report this error #
- PHPStan\Rules\Classes\ClassAttributesRule [1]
- PHPStan\Rules\Classes\ClassConstantAttributesRule [1]
- PHPStan\Rules\Classes\InstantiationRule [1]
- PHPStan\Rules\Constants\ConstantAttributesRule [1]
- PHPStan\Rules\EnumCases\EnumCaseAttributesRule [1]
- PHPStan\Rules\Functions\ArrowFunctionAttributesRule [1]
- PHPStan\Rules\Functions\CallCallablesRule [1]
- PHPStan\Rules\Functions\CallToFunctionParametersRule [1]
- PHPStan\Rules\Functions\CallUserFuncRule [1]
- PHPStan\Rules\Functions\ClosureAttributesRule [1]
- PHPStan\Rules\Functions\FunctionAttributesRule [1]
- PHPStan\Rules\Functions\ParamAttributesRule [1]
- PHPStan\Rules\Methods\CallMethodsRule [1]
- PHPStan\Rules\Methods\CallStaticMethodsRule [1]
- PHPStan\Rules\Methods\MethodAttributesRule [1]
- PHPStan\Rules\Properties\PropertyAttributesRule [1]
- PHPStan\Rules\Properties\PropertyHookAttributesRule [1]
- PHPStan\Rules\Traits\TraitAttributesRule [1]