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);
json_encode([], SORT_REGULAR);
Why is it reported? #
The SORT_REGULAR constant is not a valid flag for the $flags parameter of json_encode(). Each PHP function that accepts predefined constants only works correctly with a specific set of them. Passing an unrelated constant results in unexpected behavior because the function interprets the underlying integer value differently than intended.
PHPStan maintains a map of allowed constants for parameters of built-in PHP functions and reports when a constant from a different domain is used.
This check is only performed on PHP 8.0+ because it relies on named argument support to accurately map arguments to parameters.
How to fix it #
Use a constant that belongs to the correct function:
-json_encode([], SORT_REGULAR);
+json_encode([], JSON_PRETTY_PRINT);
Multiple valid constants can be combined with the | operator when the parameter accepts a bitmask:
-json_encode([], SORT_REGULAR);
+json_encode([], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
How to ignore this error #
You can use the identifier argument.invalidConstant to ignore this error using a comment:
// @phpstan-ignore argument.invalidConstant
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.invalidConstant
Rules that report this error #
- PHPStan\Rules\Classes\ClassAttributesRule [1] [2]
- PHPStan\Rules\Classes\ClassConstantAttributesRule [1] [2]
- PHPStan\Rules\Classes\InstantiationRule [1] [2]
- PHPStan\Rules\Constants\ConstantAttributesRule [1] [2]
- PHPStan\Rules\EnumCases\EnumCaseAttributesRule [1] [2]
- PHPStan\Rules\Functions\ArrowFunctionAttributesRule [1] [2]
- PHPStan\Rules\Functions\CallCallablesRule [1] [2]
- PHPStan\Rules\Functions\CallToFunctionParametersRule [1] [2]
- PHPStan\Rules\Functions\CallUserFuncRule [1] [2]
- PHPStan\Rules\Functions\ClosureAttributesRule [1] [2]
- PHPStan\Rules\Functions\FunctionAttributesRule [1] [2]
- PHPStan\Rules\Functions\ParamAttributesRule [1] [2]
- PHPStan\Rules\Methods\CallMethodsRule [1] [2]
- PHPStan\Rules\Methods\CallStaticMethodsRule [1] [2]
- PHPStan\Rules\Methods\MethodAttributesRule [1] [2]
- PHPStan\Rules\Properties\PropertyAttributesRule [1] [2]
- PHPStan\Rules\Properties\PropertyHookAttributesRule [1] [2]
- PHPStan\Rules\Traits\TraitAttributesRule [1] [2]