Error Identifier: filterVar.nullOnFailureAndThrowOnFailure
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 = filter_var(
'test',
FILTER_VALIDATE_INT,
FILTER_NULL_ON_FAILURE | FILTER_THROW_ON_FAILURE,
);
Why is it reported? #
The flags FILTER_NULL_ON_FAILURE and FILTER_THROW_ON_FAILURE are mutually exclusive. FILTER_NULL_ON_FAILURE causes filter_var() to return null on failure, while FILTER_THROW_ON_FAILURE causes it to throw an exception. Using both at the same time produces contradictory behaviour.
How to fix it #
Use only one of the two flags depending on the desired failure handling:
To return null on failure:
<?php declare(strict_types = 1);
$value = filter_var(
'test',
FILTER_VALIDATE_INT,
- FILTER_NULL_ON_FAILURE | FILTER_THROW_ON_FAILURE,
+ FILTER_NULL_ON_FAILURE,
);
To throw an exception on failure:
<?php declare(strict_types = 1);
$value = filter_var(
'test',
FILTER_VALIDATE_INT,
- FILTER_NULL_ON_FAILURE | FILTER_THROW_ON_FAILURE,
+ FILTER_THROW_ON_FAILURE,
);
How to ignore this error #
You can use the identifier filterVar.nullOnFailureAndThrowOnFailure to ignore this error using a comment:
// @phpstan-ignore filterVar.nullOnFailureAndThrowOnFailure
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: filterVar.nullOnFailureAndThrowOnFailure
Rules that report this error #
- PHPStan\Rules\Functions\FilterVarRule [1]