From cc1678a2aa777de11d84106a77c7d2e8af6294a9 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 28 Oct 2024 12:10:30 +0100 Subject: [PATCH] Support `iterable` typed parameter (#693) --- src/QueryReflection/QuerySimulation.php | 2 +- tests/default/data/pdo-prepare.php | 14 ++++++++++++ .../rules/UnresolvableQueryMethodRuleTest.php | 5 +++++ tests/rules/data/bug-676.php | 22 +++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/rules/data/bug-676.php diff --git a/src/QueryReflection/QuerySimulation.php b/src/QueryReflection/QuerySimulation.php index 05a9997a3..9ab16ecde 100644 --- a/src/QueryReflection/QuerySimulation.php +++ b/src/QueryReflection/QuerySimulation.php @@ -34,7 +34,7 @@ public static function simulateParamValueType(Type $paramType, bool $preparedPar return (string) $paramType->getValue(); } - if ($paramType->isArray()->yes()) { + if ($paramType->isIterable()->yes()) { return self::simulateParamValueType($paramType->getIterableValueType(), $preparedParam); } diff --git a/tests/default/data/pdo-prepare.php b/tests/default/data/pdo-prepare.php index 1c674561b..4ac808d0c 100644 --- a/tests/default/data/pdo-prepare.php +++ b/tests/default/data/pdo-prepare.php @@ -136,6 +136,20 @@ public function specifiedArray(PDO $pdo, array $idsToUpdate, string $time) assertType('PDOStatement, 0: int<-32768, 32767>}>', $stmt); } + /** + * @param iterable $idsToUpdate + */ + public function specifiedIterable(PDO $pdo, iterable $idsToUpdate, string $time) + { + $query = 'SELECT adaid FROM ada WHERE adaid IN (:ids) AND email LIKE :time'; + $stmt = $pdo->prepare($query); + $stmt->execute([ + 'ids' => $idsToUpdate, + 'time' => $time, + ]); + assertType('PDOStatement, 0: int<-32768, 32767>}>', $stmt); + } + public function noInferenceOnBug196(PDO $pdo, array $minorPhpVersions, \DateTimeImmutable $updateDate) { $sumQueries = []; diff --git a/tests/rules/UnresolvableQueryMethodRuleTest.php b/tests/rules/UnresolvableQueryMethodRuleTest.php index f35255a0d..ed24966ea 100644 --- a/tests/rules/UnresolvableQueryMethodRuleTest.php +++ b/tests/rules/UnresolvableQueryMethodRuleTest.php @@ -78,4 +78,9 @@ public function testBug547(): void { $this->analyse([__DIR__ . '/data/bug-547.php'], []); } + + public function testBug676(): void + { + $this->analyse([__DIR__ . '/data/bug-676.php'], []); + } } diff --git a/tests/rules/data/bug-676.php b/tests/rules/data/bug-676.php new file mode 100644 index 000000000..a5f546010 --- /dev/null +++ b/tests/rules/data/bug-676.php @@ -0,0 +1,22 @@ +connection + ->executeQuery( + 'SELECT id FROM table WHERE id IN (:list)', + ['list' => $ids], + ['list' => DBAL\Connection::PARAM_INT_ARRAY], + ) + ->fetchFirstColumn(); + } +}