From 5041b0a0bbc5eb879561a266afa9018b9f777fbe Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 5 Oct 2025 18:34:49 +0200 Subject: [PATCH] Improve is_resource inference --- stubs/typeCheckingFunctions.stub | 2 +- ...ImpossibleCheckTypeFunctionCallRuleTest.php | 6 ++++++ .../Rules/Comparison/data/bug-10394.php | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Rules/Comparison/data/bug-10394.php diff --git a/stubs/typeCheckingFunctions.stub b/stubs/typeCheckingFunctions.stub index 4f06338125..f10609ee77 100644 --- a/stubs/typeCheckingFunctions.stub +++ b/stubs/typeCheckingFunctions.stub @@ -106,7 +106,7 @@ function is_long(mixed $value): bool /** * @phpstan-assert-if-true =resource $value - * @return bool + * @return ($value is resource ? bool : false) */ function is_resource(mixed $value): bool { diff --git a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php index c795706ab9..3a0683d5ac 100644 --- a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php @@ -1094,6 +1094,12 @@ public function testBug12087c(): void ]); } + public function testBug10394(): void + { + $this->treatPhpDocTypesAsCertain = true; + $this->analyse([__DIR__ . '/data/bug-10394.php'], []); + } + public function testBug9666(): void { $tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your %configurationFile%.'; diff --git a/tests/PHPStan/Rules/Comparison/data/bug-10394.php b/tests/PHPStan/Rules/Comparison/data/bug-10394.php new file mode 100644 index 0000000000..e6aaafd247 --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-10394.php @@ -0,0 +1,18 @@ +integer = 45; + + if (is_resource($this->integer)) { + throw new \Exception('A resource??'); + } + } +}