From fc104a46335ce74ddf163295792f741d48dc7e65 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Tue, 6 Aug 2024 12:36:03 +0200 Subject: [PATCH 1/5] is_resource: remove type specification --- stubs/typeCheckingFunctions.stub | 9 --------- 1 file changed, 9 deletions(-) diff --git a/stubs/typeCheckingFunctions.stub b/stubs/typeCheckingFunctions.stub index 82d9c83cde..01f72ce150 100644 --- a/stubs/typeCheckingFunctions.stub +++ b/stubs/typeCheckingFunctions.stub @@ -104,15 +104,6 @@ function is_long(mixed $value): bool } -/** - * @phpstan-assert-if-true resource $value - * @return bool - */ -function is_resource(mixed $value): bool -{ - -} - /** * @return ($value is array ? true : false) */ From 9e8ca3ad829cdf1eb54e1df9c06b7e450495d16c Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Tue, 6 Aug 2024 13:00:34 +0200 Subject: [PATCH 2/5] Fix test --- tests/PHPStan/Analyser/data/specifiedTypesUsingIsFunctions.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/PHPStan/Analyser/data/specifiedTypesUsingIsFunctions.php b/tests/PHPStan/Analyser/data/specifiedTypesUsingIsFunctions.php index c7d2ca259c..90f909dbf3 100644 --- a/tests/PHPStan/Analyser/data/specifiedTypesUsingIsFunctions.php +++ b/tests/PHPStan/Analyser/data/specifiedTypesUsingIsFunctions.php @@ -41,7 +41,8 @@ function () { throw new \Exception(); } - if (!is_resource($resource)) { + $resource = fopen('php://memory', 'r'); + if ($resource === false) { throw new \Exception(); } From 4d82d78fa30ee273c3e620de293770d128e6e2ad Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Wed, 7 Aug 2024 15:07:41 +0200 Subject: [PATCH 3/5] IsResourceFunctionTypeSpecifyingExtension for truthy context --- conf/config.neon | 5 ++ ...esourceFunctionTypeSpecifyingExtension.php | 50 +++++++++++++++++++ .../Analyser/NodeScopeResolverTest.php | 2 + .../Analyser/data/is-resource-specified.php | 11 ++++ 4 files changed, 68 insertions(+) create mode 100644 src/Type/Php/IsResourceFunctionTypeSpecifyingExtension.php create mode 100644 tests/PHPStan/Analyser/data/is-resource-specified.php diff --git a/conf/config.neon b/conf/config.neon index c85cce945d..dc9c5d6be2 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -1797,6 +1797,11 @@ services: - class: PHPStan\Type\Php\IsAFunctionTypeSpecifyingHelper + - + class: PHPStan\Type\Php\IsResourceFunctionTypeSpecifyingExtension + tags: + - phpstan.typeSpecifier.functionTypeSpecifyingExtension + - class: PHPStan\Type\Php\CtypeDigitFunctionTypeSpecifyingExtension tags: diff --git a/src/Type/Php/IsResourceFunctionTypeSpecifyingExtension.php b/src/Type/Php/IsResourceFunctionTypeSpecifyingExtension.php new file mode 100644 index 0000000000..e7e1be03cc --- /dev/null +++ b/src/Type/Php/IsResourceFunctionTypeSpecifyingExtension.php @@ -0,0 +1,50 @@ +getName()) === 'is_resource' + && !$context->null(); + } + + public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes + { + if ($context->null()) { + throw new ShouldNotHappenException(); + } + + if (!isset($node->getArgs()[0])) { + return new SpecifiedTypes(); + } + + if ($context->truthy()) { + return $this->typeSpecifier->create($node->getArgs()[0]->value, new ResourceType(), $context, false, $scope); + } + + return new SpecifiedTypes(); + } + + public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void + { + $this->typeSpecifier = $typeSpecifier; + } + +} diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 3a1bf98033..6a8c6b517d 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -143,6 +143,8 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Functions/data/bug-7823.php'); } + yield from $this->gatherAssertTypes(__DIR__ . '/../Analyser/data/is-resource-specified.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/bug-7954.php'); yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Comparison/data/docblock-assert-equality.php'); yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Properties/data/bug-7839.php'); diff --git a/tests/PHPStan/Analyser/data/is-resource-specified.php b/tests/PHPStan/Analyser/data/is-resource-specified.php new file mode 100644 index 0000000000..c78d5ad75a --- /dev/null +++ b/tests/PHPStan/Analyser/data/is-resource-specified.php @@ -0,0 +1,11 @@ + Date: Wed, 7 Aug 2024 15:44:29 +0200 Subject: [PATCH 4/5] Remove extension, use phpstan-assert-if-true =resource --- conf/config.neon | 5 -- ...esourceFunctionTypeSpecifyingExtension.php | 50 ------------------- stubs/typeCheckingFunctions.stub | 9 ++++ 3 files changed, 9 insertions(+), 55 deletions(-) delete mode 100644 src/Type/Php/IsResourceFunctionTypeSpecifyingExtension.php diff --git a/conf/config.neon b/conf/config.neon index dc9c5d6be2..c85cce945d 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -1797,11 +1797,6 @@ services: - class: PHPStan\Type\Php\IsAFunctionTypeSpecifyingHelper - - - class: PHPStan\Type\Php\IsResourceFunctionTypeSpecifyingExtension - tags: - - phpstan.typeSpecifier.functionTypeSpecifyingExtension - - class: PHPStan\Type\Php\CtypeDigitFunctionTypeSpecifyingExtension tags: diff --git a/src/Type/Php/IsResourceFunctionTypeSpecifyingExtension.php b/src/Type/Php/IsResourceFunctionTypeSpecifyingExtension.php deleted file mode 100644 index e7e1be03cc..0000000000 --- a/src/Type/Php/IsResourceFunctionTypeSpecifyingExtension.php +++ /dev/null @@ -1,50 +0,0 @@ -getName()) === 'is_resource' - && !$context->null(); - } - - public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes - { - if ($context->null()) { - throw new ShouldNotHappenException(); - } - - if (!isset($node->getArgs()[0])) { - return new SpecifiedTypes(); - } - - if ($context->truthy()) { - return $this->typeSpecifier->create($node->getArgs()[0]->value, new ResourceType(), $context, false, $scope); - } - - return new SpecifiedTypes(); - } - - public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void - { - $this->typeSpecifier = $typeSpecifier; - } - -} diff --git a/stubs/typeCheckingFunctions.stub b/stubs/typeCheckingFunctions.stub index 01f72ce150..4f06338125 100644 --- a/stubs/typeCheckingFunctions.stub +++ b/stubs/typeCheckingFunctions.stub @@ -104,6 +104,15 @@ function is_long(mixed $value): bool } +/** + * @phpstan-assert-if-true =resource $value + * @return bool + */ +function is_resource(mixed $value): bool +{ + +} + /** * @return ($value is array ? true : false) */ From 91d1314217b9bbe47352e58291b2c2012f01017a Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Thu, 8 Aug 2024 10:39:36 +0200 Subject: [PATCH 5/5] Revert test change + add test for ImpossibleCheckTypeFunctionCallRule --- .../Analyser/data/specifiedTypesUsingIsFunctions.php | 3 +-- .../Rules/Comparison/data/check-type-function-call.php | 9 +++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/PHPStan/Analyser/data/specifiedTypesUsingIsFunctions.php b/tests/PHPStan/Analyser/data/specifiedTypesUsingIsFunctions.php index 90f909dbf3..c7d2ca259c 100644 --- a/tests/PHPStan/Analyser/data/specifiedTypesUsingIsFunctions.php +++ b/tests/PHPStan/Analyser/data/specifiedTypesUsingIsFunctions.php @@ -41,8 +41,7 @@ function () { throw new \Exception(); } - $resource = fopen('php://memory', 'r'); - if ($resource === false) { + if (!is_resource($resource)) { throw new \Exception(); } diff --git a/tests/PHPStan/Rules/Comparison/data/check-type-function-call.php b/tests/PHPStan/Rules/Comparison/data/check-type-function-call.php index d9fb0b0845..814d93ae60 100644 --- a/tests/PHPStan/Rules/Comparison/data/check-type-function-call.php +++ b/tests/PHPStan/Rules/Comparison/data/check-type-function-call.php @@ -967,3 +967,12 @@ function checkSuperGlobals(): void if (is_int($k)) {} } } + +/** + * @param resource $resource + */ +function checkClosedResource($resource): void { + if (!is_resource($resource)) { + + } +}