Skip to content
5 changes: 5 additions & 0 deletions src/Php/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,9 @@ public function supportsObjectsInArraySumProduct(): bool
return $this->versionId >= 80300;
}

public function hasFilterThrowOnFailureConstant(): bool
{
return $this->versionId >= 80500;
}

}
4 changes: 3 additions & 1 deletion src/Rules/Functions/FilterVarRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand All @@ -23,6 +24,7 @@ final class FilterVarRule implements Rule
public function __construct(
private ReflectionProvider $reflectionProvider,
private FilterFunctionReturnTypeHelper $filterFunctionReturnTypeHelper,
private PhpVersion $phpVersion,
)
{
}
Expand All @@ -44,7 +46,7 @@ public function processNode(Node $node, Scope $scope): array

$args = $node->getArgs();

if ($this->reflectionProvider->hasConstant(new Name\FullyQualified('FILTER_THROW_ON_FAILURE'), null)) {
if ($this->phpVersion->hasFilterThrowOnFailureConstant() && $this->reflectionProvider->hasConstant(new Name\FullyQualified('FILTER_THROW_ON_FAILURE'), null)) {
Comment thread
VincentLanglet marked this conversation as resolved.
Outdated
if (count($args) < 3) {
return [];
}
Expand Down
4 changes: 3 additions & 1 deletion src/Type/Php/FilterFunctionReturnTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ public function getType(Type $inputType, ?Type $filterType, ?Type $flagsType): T

$inputIsArray = $inputType->isArray();
$hasRequireArrayFlag = $this->hasFlag('FILTER_REQUIRE_ARRAY', $flagsType);
$hasThrowOnFailureFlag = $this->hasFlag('FILTER_THROW_ON_FAILURE', $flagsType);
$hasThrowOnFailureFlag = $this->phpVersion->hasFilterThrowOnFailureConstant()
? $this->hasFlag('FILTER_THROW_ON_FAILURE', $flagsType)
: TrinaryLogic::createNo();
if ($inputIsArray->no() && $hasRequireArrayFlag->yes()) {
if ($hasThrowOnFailureFlag->yes()) {
return new ErrorType();
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Php/FilterVarThrowTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Constant\ConstantIntegerType;
Expand All @@ -20,6 +21,7 @@ final class FilterVarThrowTypeExtension implements DynamicFunctionThrowTypeExten

public function __construct(
private ReflectionProvider $reflectionProvider,
private PhpVersion $phpVersion,
)
{
}
Expand All @@ -29,6 +31,7 @@ public function isFunctionSupported(
): bool
{
return $functionReflection->getName() === 'filter_var'
&& $this->phpVersion->hasFilterThrowOnFailureConstant()
Comment thread
VincentLanglet marked this conversation as resolved.
Outdated
&& $this->reflectionProvider->hasConstant(new Name\FullyQualified('FILTER_THROW_ON_FAILURE'), null);
}

Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14397.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php // lint >= 8.2

declare(strict_types = 1);

namespace Bug14397;

use function filter_var;
use function PHPStan\Testing\assertType;

function test(string $ipAddress): void
{
assertType('non-falsy-string|false', filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_GLOBAL_RANGE));
}
2 changes: 2 additions & 0 deletions tests/PHPStan/Rules/Functions/FilterVarRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Functions;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\Php\FilterFunctionReturnTypeHelper;
Expand All @@ -16,6 +17,7 @@ protected function getRule(): Rule
return new FilterVarRule(
self::createReflectionProvider(),
self::getContainer()->getByType(FilterFunctionReturnTypeHelper::class),
self::getContainer()->getByType(PhpVersion::class),
);
}

Expand Down
Loading