77use PhpParser \Node \Name ;
88use PHPStan \Analyser \Scope ;
99use PHPStan \DependencyInjection \RegisteredRule ;
10+ use PHPStan \Php \PhpVersion ;
1011use PHPStan \Reflection \ReflectionProvider ;
1112use PHPStan \Rules \Rule ;
1213use PHPStan \Rules \RuleErrorBuilder ;
@@ -23,6 +24,7 @@ final class FilterVarRule implements Rule
2324 public function __construct (
2425 private ReflectionProvider $ reflectionProvider ,
2526 private FilterFunctionReturnTypeHelper $ filterFunctionReturnTypeHelper ,
27+ private PhpVersion $ phpVersion ,
2628 )
2729 {
2830 }
@@ -44,23 +46,28 @@ public function processNode(Node $node, Scope $scope): array
4446
4547 $ args = $ node ->getArgs ();
4648
47- if ($ this ->reflectionProvider ->hasConstant (new Name \FullyQualified ('FILTER_THROW_ON_FAILURE ' ), null )) {
48- if (count ($ args ) < 3 ) {
49- return [];
50- }
49+ if (count ($ args ) < 3 ) {
50+ return [];
51+ }
52+
53+ if (
54+ !$ this ->phpVersion ->hasFilterThrowOnFailureConstant ()
55+ || !$ this ->reflectionProvider ->hasConstant (new Name \FullyQualified ('FILTER_THROW_ON_FAILURE ' ), null )
56+ ) {
57+ return [];
58+ }
5159
52- $ flagsType = $ scope ->getType ($ args [2 ]->value );
60+ $ flagsType = $ scope ->getType ($ args [2 ]->value );
5361
54- if ($ this ->filterFunctionReturnTypeHelper ->hasFlag ('FILTER_NULL_ON_FAILURE ' , $ flagsType )
55- ->and ($ this ->filterFunctionReturnTypeHelper ->hasFlag ('FILTER_THROW_ON_FAILURE ' , $ flagsType ))
56- ->yes ()
57- ) {
58- return [
59- RuleErrorBuilder::message ('Cannot use both FILTER_NULL_ON_FAILURE and FILTER_THROW_ON_FAILURE. ' )
60- ->identifier ('filterVar.nullOnFailureAndThrowOnFailure ' )
61- ->build (),
62- ];
63- }
62+ if ($ this ->filterFunctionReturnTypeHelper ->hasFlag ('FILTER_NULL_ON_FAILURE ' , $ flagsType )
63+ ->and ($ this ->filterFunctionReturnTypeHelper ->hasFlag ('FILTER_THROW_ON_FAILURE ' , $ flagsType ))
64+ ->yes ()
65+ ) {
66+ return [
67+ RuleErrorBuilder::message ('Cannot use both FILTER_NULL_ON_FAILURE and FILTER_THROW_ON_FAILURE. ' )
68+ ->identifier ('filterVar.nullOnFailureAndThrowOnFailure ' )
69+ ->build (),
70+ ];
6471 }
6572
6673 return [];
0 commit comments