Skip to content

Commit acd0511

Browse files
authored
Ignore FILTER_THROW_ON_FAILURE bit when the PHP version range only maybe includes PHP 8.5 and the runtime FILTER_FLAG_GLOBAL_RANGE shares it (#6779)
1 parent 43a651b commit acd0511

5 files changed

Lines changed: 53 additions & 8 deletions

File tree

‎src/Type/Php/FilterFunctionReturnTypeHelper.php‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,11 +515,12 @@ public function hasFlag(string $flagName, ?Type $flagsType): TrinaryLogic
515515
);
516516
}
517517

518-
private function hasThrowOnFailureFlag(?Type $flagsType, PhpVersions $phpVersions): TrinaryLogic
518+
public function hasThrowOnFailureFlag(?Type $flagsType, PhpVersions $phpVersions): TrinaryLogic
519519
{
520-
return $phpVersions->hasFilterThrowOnFailureConstant()->no()
521-
? TrinaryLogic::createNo()
522-
: $this->hasFlag('FILTER_THROW_ON_FAILURE', $flagsType);
520+
// Flag values are resolved from the runtime. Before PHP 8.5, FILTER_FLAG_GLOBAL_RANGE used the bit
521+
// that PHP 8.5 assigned to FILTER_THROW_ON_FAILURE, so a set bit only definitely means
522+
// FILTER_THROW_ON_FAILURE when the analysed code definitely runs on PHP 8.5+.
523+
return $phpVersions->hasFilterThrowOnFailureConstant()->and($this->hasFlag('FILTER_THROW_ON_FAILURE', $flagsType));
523524
}
524525

525526
private function getFlagsValue(Type $exprType): Type

‎src/Type/Php/FilterFunctionsThrowTypeExtension.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ public function getThrowTypeFromFunctionCall(
3737
Scope $scope,
3838
): ?Type
3939
{
40+
$phpVersions = $scope->getPhpVersion();
4041
if (
41-
$scope->getPhpVersion()->hasFilterThrowOnFailureConstant()->no()
42+
$phpVersions->hasFilterThrowOnFailureConstant()->no()
4243
|| !$this->reflectionProvider->hasConstant(new Name\FullyQualified('FILTER_THROW_ON_FAILURE'), null)
4344
) {
4445
return null;
4546
}
4647

4748
foreach ($this->filterFunctionFlagsHelper->getFlagsTypes($functionReflection, $funcCall, $scope) as $flagsType) {
48-
if ($this->filterFunctionReturnTypeHelper->hasFlag('FILTER_THROW_ON_FAILURE', $flagsType)->no()) {
49+
if ($this->filterFunctionReturnTypeHelper->hasThrowOnFailureFlag($flagsType, $phpVersions)->no()) {
4950
continue;
5051
}
5152

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php // lint >= 8.2
2+
3+
namespace ScopePhpVersionRangeReturnTypeExtensions;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function filterFlagGlobalRange(string $address): void
8+
{
9+
assertType('non-falsy-string|false', filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_GLOBAL_RANGE));
10+
assertType('non-falsy-string|false', filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_GLOBAL_RANGE));
11+
assertType('non-falsy-string|null', filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_GLOBAL_RANGE | FILTER_NULL_ON_FAILURE));
12+
assertType('array{ip: non-falsy-string|false|null}', filter_input_array(INPUT_GET, ['ip' => ['filter' => FILTER_VALIDATE_IP, 'flags' => FILTER_FLAG_GLOBAL_RANGE]]));
13+
}

‎tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleConfigPhpTest.php‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPStan\Rules\Rule;
66
use PHPStan\Testing\RuleTestCase;
7+
use const PHP_VERSION_ID;
78

89
/**
910
* @extends RuleTestCase<CatchWithUnthrownExceptionRule>
@@ -24,12 +25,23 @@ protected function getRule(): Rule
2425

2526
public function testThrowTypesInPhpVersionRange(): void
2627
{
27-
$this->analyse([__DIR__ . '/data/throw-type-php-versions.php'], [
28+
$errors = [
2829
[
2930
'Dead catch - ValueError is never thrown in the try block.',
3031
19,
3132
],
32-
]);
33+
];
34+
35+
if (PHP_VERSION_ID >= 80500) {
36+
// Before PHP 8.5, FILTER_FLAG_GLOBAL_RANGE has the same runtime value as FILTER_THROW_ON_FAILURE on PHP 8.5,
37+
// so it might throw Filter\FilterFailedException in a PHP version range that only maybe includes PHP 8.5.
38+
$errors[] = [
39+
'Dead catch - Filter\\FilterFailedException is never thrown in the try block.',
40+
58,
41+
];
42+
}
43+
44+
$this->analyse([__DIR__ . '/data/throw-type-php-versions.php'], $errors);
3345
}
3446

3547
public static function getAdditionalConfigFiles(): array

‎tests/PHPStan/Rules/Exceptions/data/throw-type-php-versions.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,22 @@ public function doStrSplit(string $s, int $length): void
5151
}
5252
}
5353

54+
public function doFilterVar(string $address): void
55+
{
56+
try {
57+
$a = filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_GLOBAL_RANGE);
58+
} catch (\Filter\FilterFailedException $e) {
59+
60+
}
61+
}
62+
63+
public function doFilterVarThrowOnFailure(string $address): void
64+
{
65+
try {
66+
$a = filter_var($address, FILTER_VALIDATE_IP, FILTER_THROW_ON_FAILURE);
67+
} catch (\Filter\FilterFailedException $e) {
68+
69+
}
70+
}
71+
5472
}

0 commit comments

Comments
 (0)