Skip to content

Commit 67dec56

Browse files
phpstan-botclaude
andcommitted
Add tests for FilterVarThrowTypeExtension and FilterVarRule on PHP < 8.5
- Add nsrt test verifying filter_var with FILTER_THROW_ON_FAILURE still returns false on PHP < 8.5 (throw type extension correctly inactive) - Add FilterVarRule test verifying no error for FILTER_NULL_ON_FAILURE + FILTER_THROW_ON_FAILURE combination on PHP < 8.5 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 94b3785 commit 67dec56

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php // lint < 8.5
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug14397PrePhp85;
6+
7+
use function filter_var;
8+
use function PHPStan\Testing\assertType;
9+
10+
function test(mixed $mixed): void
11+
{
12+
// On PHP < 8.5, FILTER_THROW_ON_FAILURE doesn't truly exist
13+
// so it shouldn't remove false from the return type
14+
assertType('int|false', filter_var($mixed, FILTER_VALIDATE_INT, FILTER_THROW_ON_FAILURE));
15+
assertType('int|false', filter_var($mixed, FILTER_VALIDATE_INT, ['flags' => FILTER_THROW_ON_FAILURE]));
16+
}

tests/PHPStan/Rules/Functions/FilterVarRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ public function testRule(): void
3131
]);
3232
}
3333

34+
#[RequiresPhp('< 8.5')]
35+
public function testRuleBeforePhp85(): void
36+
{
37+
$this->analyse([__DIR__ . '/data/filter_var_null_and_throw_pre_php85.php'], []);
38+
}
39+
3440
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php // lint < 8.5
2+
3+
namespace FilterVarNullAndThrowPrePhp85;
4+
5+
// On PHP < 8.5, FILTER_THROW_ON_FAILURE doesn't truly exist
6+
// so combining it with FILTER_NULL_ON_FAILURE should not be an error
7+
filter_var('foo@bar.test', FILTER_VALIDATE_EMAIL, FILTER_THROW_ON_FAILURE|FILTER_NULL_ON_FAILURE);
8+
9+
$flag = FILTER_NULL_ON_FAILURE|FILTER_THROW_ON_FAILURE;
10+
filter_var(100, FILTER_VALIDATE_INT, $flag);
11+
12+
filter_var(
13+
'johndoe',
14+
FILTER_VALIDATE_REGEXP,
15+
['options' => ['regexp' => '/^[a-z]+$/'], 'flags' => FILTER_THROW_ON_FAILURE|FILTER_NULL_ON_FAILURE]
16+
);

0 commit comments

Comments
 (0)