From 79f03203a0e532d9d0b0ad1ffc4e38f9da544ffc Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 7 Feb 2022 20:51:50 +0100 Subject: [PATCH 1/2] simplify PdoStatementReflection --- ...octrineStatementExecuteDynamicReturnTypeExtension.php | 2 +- .../PdoStatementExecuteTypeSpecifyingExtension.php | 2 +- src/PdoReflection/PdoStatementReflection.php | 9 ++++----- src/Rules/PdoStatementExecuteMethodRule.php | 2 +- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Extensions/DoctrineStatementExecuteDynamicReturnTypeExtension.php b/src/Extensions/DoctrineStatementExecuteDynamicReturnTypeExtension.php index 63a661cab..cbad59598 100644 --- a/src/Extensions/DoctrineStatementExecuteDynamicReturnTypeExtension.php +++ b/src/Extensions/DoctrineStatementExecuteDynamicReturnTypeExtension.php @@ -63,7 +63,7 @@ private function inferType(MethodReflection $methodReflection, MethodCall $metho $parameterTypes = $scope->getType($paramsExpr); $stmtReflection = new PdoStatementReflection(); - $queryExpr = $stmtReflection->findPrepareQueryStringExpression($methodReflection, $methodCall); + $queryExpr = $stmtReflection->findPrepareQueryStringExpression($methodCall); if (null === $queryExpr) { return null; } diff --git a/src/Extensions/PdoStatementExecuteTypeSpecifyingExtension.php b/src/Extensions/PdoStatementExecuteTypeSpecifyingExtension.php index 65ec511c8..ca6541f9d 100644 --- a/src/Extensions/PdoStatementExecuteTypeSpecifyingExtension.php +++ b/src/Extensions/PdoStatementExecuteTypeSpecifyingExtension.php @@ -60,7 +60,7 @@ private function inferStatementType(MethodReflection $methodReflection, MethodCa } $stmtReflection = new PdoStatementReflection(); - $queryExpr = $stmtReflection->findPrepareQueryStringExpression($methodReflection, $methodCall); + $queryExpr = $stmtReflection->findPrepareQueryStringExpression($methodCall); if (null === $queryExpr) { return null; } diff --git a/src/PdoReflection/PdoStatementReflection.php b/src/PdoReflection/PdoStatementReflection.php index 2ac7003ea..15eaba8c9 100644 --- a/src/PdoReflection/PdoStatementReflection.php +++ b/src/PdoReflection/PdoStatementReflection.php @@ -18,7 +18,7 @@ final class PdoStatementReflection { - public function findPrepareQueryStringExpression(MethodReflection $methodReflection, MethodCall $methodCall): ?Expr + public function findPrepareQueryStringExpression(MethodCall $methodCall): ?Expr { $exprFinder = new ExpressionFinder(); $queryExpr = $exprFinder->findQueryStringExpression($methodCall); @@ -35,10 +35,8 @@ public function findPrepareQueryStringExpression(MethodReflection $methodReflect /** * @param PDO::FETCH* $fetchType - * - * @return Type|null */ - public function getStatementResultType(Type $statementType, int $fetchType) + public function getStatementResultType(Type $statementType, int $fetchType): ?Type { if (!$statementType instanceof GenericObjectType) { return null; @@ -50,7 +48,8 @@ public function getStatementResultType(Type $statementType, int $fetchType) } $resultType = $genericTypes[0]; - if ((PDO::FETCH_NUM === $fetchType || PDO::FETCH_ASSOC === $fetchType) && $resultType instanceof ConstantArrayType) { + if ((PDO::FETCH_NUM === $fetchType || PDO::FETCH_ASSOC === $fetchType) && + $resultType instanceof ConstantArrayType && count($resultType->getValueTypes()) > 0) { $builder = ConstantArrayTypeBuilder::createEmpty(); $keyTypes = $resultType->getKeyTypes(); diff --git a/src/Rules/PdoStatementExecuteMethodRule.php b/src/Rules/PdoStatementExecuteMethodRule.php index 35981208c..f61e4da84 100644 --- a/src/Rules/PdoStatementExecuteMethodRule.php +++ b/src/Rules/PdoStatementExecuteMethodRule.php @@ -59,7 +59,7 @@ private function checkErrors(MethodReflection $methodReflection, MethodCall $met { $queryReflection = new QueryReflection(); $stmtReflection = new PdoStatementReflection(); - $queryExpr = $stmtReflection->findPrepareQueryStringExpression($methodReflection, $methodCall); + $queryExpr = $stmtReflection->findPrepareQueryStringExpression($methodCall); if (null === $queryExpr) { return []; From 3d9026cd72fbfa3d8e378e7a38798cac457fb689 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 7 Feb 2022 20:52:53 +0100 Subject: [PATCH 2/2] Update PdoStatementReflection.php --- src/PdoReflection/PdoStatementReflection.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/PdoReflection/PdoStatementReflection.php b/src/PdoReflection/PdoStatementReflection.php index 15eaba8c9..580156947 100644 --- a/src/PdoReflection/PdoStatementReflection.php +++ b/src/PdoReflection/PdoStatementReflection.php @@ -7,7 +7,6 @@ use PDO; use PhpParser\Node\Expr; use PhpParser\Node\Expr\MethodCall; -use PHPStan\Reflection\MethodReflection; use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\Constant\ConstantArrayTypeBuilder; use PHPStan\Type\Constant\ConstantIntegerType; @@ -49,7 +48,7 @@ public function getStatementResultType(Type $statementType, int $fetchType): ?Ty $resultType = $genericTypes[0]; if ((PDO::FETCH_NUM === $fetchType || PDO::FETCH_ASSOC === $fetchType) && - $resultType instanceof ConstantArrayType && count($resultType->getValueTypes()) > 0) { + $resultType instanceof ConstantArrayType && \count($resultType->getValueTypes()) > 0) { $builder = ConstantArrayTypeBuilder::createEmpty(); $keyTypes = $resultType->getKeyTypes();