From a9881e3afd4a105f0bd1391fb57a8e5c3462d2bc Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sat, 8 Jan 2022 21:57:40 +0100 Subject: [PATCH] PDO::prepare returns BenevolentUnionType --- conf/config.neon | 5 +++ .../PdoPrepareDynamicReturnTypeExtension.php | 37 +++++++++++++++++++ .../Analyser/NodeScopeResolverTest.php | 1 + tests/PHPStan/Analyser/data/pdo-prepare.php | 17 +++++++++ 4 files changed, 60 insertions(+) create mode 100644 src/Type/Php/PdoPrepareDynamicReturnTypeExtension.php create mode 100644 tests/PHPStan/Analyser/data/pdo-prepare.php diff --git a/conf/config.neon b/conf/config.neon index 0e051152fe..a7325cb18c 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -1144,6 +1144,11 @@ services: tags: - phpstan.dynamicFunctionThrowTypeExtension + - + class: PHPStan\Type\Php\PdoPrepareDynamicReturnTypeExtension + tags: + - phpstan.broker.dynamicMethodReturnTypeExtension + - class: PHPStan\Type\Php\ReflectionClassConstructorThrowTypeExtension tags: diff --git a/src/Type/Php/PdoPrepareDynamicReturnTypeExtension.php b/src/Type/Php/PdoPrepareDynamicReturnTypeExtension.php new file mode 100644 index 0000000000..e5d417ad49 --- /dev/null +++ b/src/Type/Php/PdoPrepareDynamicReturnTypeExtension.php @@ -0,0 +1,37 @@ +getName() === 'prepare'; + } + + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type + { + $returnType = ParametersAcceptorSelector::selectFromArgs( + $scope, + $methodCall->getArgs(), + $methodReflection->getVariants(), + )->getReturnType(); + + return TypeUtils::toBenevolentUnion($returnType); + } + +} diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 77101cf375..be41d9ea00 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -594,6 +594,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/classPhpDocs-phpstanPropertyPrefix.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/array-destructuring-types.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/pdo-prepare.php'); } /** diff --git a/tests/PHPStan/Analyser/data/pdo-prepare.php b/tests/PHPStan/Analyser/data/pdo-prepare.php new file mode 100644 index 0000000000..72c4ab39c6 --- /dev/null +++ b/tests/PHPStan/Analyser/data/pdo-prepare.php @@ -0,0 +1,17 @@ +prepare('DELETE FROM log'); + assertType('(PDOStatement|false)', $logDeleteQuery); + } + +}