From 9a29a1a87b7309d0f5e0acf3b118cd81ff740303 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 4 May 2022 16:43:06 +0200 Subject: [PATCH 1/6] Allow simulation of DateTime objects --- src/QueryReflection/QuerySimulation.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/QueryReflection/QuerySimulation.php b/src/QueryReflection/QuerySimulation.php index db50c6748..0b40cea67 100644 --- a/src/QueryReflection/QuerySimulation.php +++ b/src/QueryReflection/QuerySimulation.php @@ -73,6 +73,10 @@ public static function simulateParamValueType(Type $paramType, bool $preparedPar return null; } + if ($paramType instanceof ObjectType && $paramType->isInstanceOf(\DateTimeInterface::class)) { + return date(self::DATE_FORMAT, 0); + } + $stringType = new StringType(); $isStringableObjectType = $paramType instanceof ObjectType && $paramType->isInstanceOf(Stringable::class)->yes(); From ed166fc0ac2b44cb86f23b882e63bae29f1e9efb Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 18 May 2022 15:18:26 +0200 Subject: [PATCH 2/6] fix --- src/QueryReflection/QuerySimulation.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/QueryReflection/QuerySimulation.php b/src/QueryReflection/QuerySimulation.php index 0b40cea67..28d476359 100644 --- a/src/QueryReflection/QuerySimulation.php +++ b/src/QueryReflection/QuerySimulation.php @@ -73,7 +73,8 @@ public static function simulateParamValueType(Type $paramType, bool $preparedPar return null; } - if ($paramType instanceof ObjectType && $paramType->isInstanceOf(\DateTimeInterface::class)) { + // TODO the dateformat should be taken from bound-parameter-types, see https://github.com/staabm/phpstan-dba/pull/342 + if ($paramType instanceof ObjectType && $paramType->isInstanceOf(\DateTimeInterface::class)->yes()) { return date(self::DATE_FORMAT, 0); } From 29457039cf6dbfe95b2bc5b341f0823b90079f55 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 18 May 2022 15:20:47 +0200 Subject: [PATCH 3/6] testcase --- tests/default/data/doctrine-dbal.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/default/data/doctrine-dbal.php b/tests/default/data/doctrine-dbal.php index 37e045e8c..ea1a41362 100644 --- a/tests/default/data/doctrine-dbal.php +++ b/tests/default/data/doctrine-dbal.php @@ -218,4 +218,11 @@ public function dateParameter(Connection $conn) $fetchResult = $conn->fetchOne($query, [date('Y-m-d', strtotime('-3hour'))]); assertType('int|false', $fetchResult); } + + public function customTypeParameters(Connection $conn) + { + $stmt = $conn->prepare('SELECT count(*) AS c FROM typemix WHERE c_datetime=:last_dt'); + $result = $stmt->execute(['dt' => new \DateTime()] /*, ['dt' => Types::DATETIME_MUTABLE]*/); // XXX use bound parameter types instead + assertType('Doctrine\DBAL\Result', $result); + } } From 8146a4be0a5f88aafcebcd9653f8add8b7358e22 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 18 May 2022 15:31:35 +0200 Subject: [PATCH 4/6] Update doctrine-dbal.php --- tests/default/data/doctrine-dbal.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/default/data/doctrine-dbal.php b/tests/default/data/doctrine-dbal.php index ea1a41362..98e115a74 100644 --- a/tests/default/data/doctrine-dbal.php +++ b/tests/default/data/doctrine-dbal.php @@ -222,7 +222,7 @@ public function dateParameter(Connection $conn) public function customTypeParameters(Connection $conn) { $stmt = $conn->prepare('SELECT count(*) AS c FROM typemix WHERE c_datetime=:last_dt'); - $result = $stmt->execute(['dt' => new \DateTime()] /*, ['dt' => Types::DATETIME_MUTABLE]*/); // XXX use bound parameter types instead + $result = $stmt->execute(['dt' => new \DateTime()], ['dt' => Types::DATETIME_MUTABLE]); // XXX use bound parameter types instead assertType('Doctrine\DBAL\Result', $result); } } From 4f8c10a7d1a38a625a319c79a64c86c1b4f28811 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 18 May 2022 15:35:33 +0200 Subject: [PATCH 5/6] fix --- tests/default/config/.phpstan-dba-mysqli.cache | 7 +++++++ tests/default/config/.phpstan-dba-pdo.cache | 7 +++++++ tests/default/config/.phpunit-phpstan-dba-mysqli.cache | 7 +++++++ tests/default/config/.phpunit-phpstan-dba-pdo.cache | 7 +++++++ tests/default/data/doctrine-dbal.php | 1 + 5 files changed, 29 insertions(+) diff --git a/tests/default/config/.phpstan-dba-mysqli.cache b/tests/default/config/.phpstan-dba-mysqli.cache index e593823a9..481f30412 100644 --- a/tests/default/config/.phpstan-dba-mysqli.cache +++ b/tests/default/config/.phpstan-dba-mysqli.cache @@ -2582,6 +2582,13 @@ )), ), ), + 'SELECT count(*) AS c FROM typemix WHERE c_datetime=:last_dt' => + array ( + 'result' => + array ( + 5 => NULL, + ), + ), 'SELECT count(*) FROM typemix WHERE c_date = \'1970-01-01\'' => array ( 'result' => diff --git a/tests/default/config/.phpstan-dba-pdo.cache b/tests/default/config/.phpstan-dba-pdo.cache index e593823a9..481f30412 100644 --- a/tests/default/config/.phpstan-dba-pdo.cache +++ b/tests/default/config/.phpstan-dba-pdo.cache @@ -2582,6 +2582,13 @@ )), ), ), + 'SELECT count(*) AS c FROM typemix WHERE c_datetime=:last_dt' => + array ( + 'result' => + array ( + 5 => NULL, + ), + ), 'SELECT count(*) FROM typemix WHERE c_date = \'1970-01-01\'' => array ( 'result' => diff --git a/tests/default/config/.phpunit-phpstan-dba-mysqli.cache b/tests/default/config/.phpunit-phpstan-dba-mysqli.cache index 24f5ca5f0..369604c02 100644 --- a/tests/default/config/.phpunit-phpstan-dba-mysqli.cache +++ b/tests/default/config/.phpunit-phpstan-dba-mysqli.cache @@ -2870,6 +2870,13 @@ )), ), ), + 'SELECT count(*) AS c FROM typemix WHERE c_datetime=:last_dt' => + array ( + 'result' => + array ( + 5 => NULL, + ), + ), 'SELECT count(*) FROM typemix WHERE c_date = \'1970-01-01\'' => array ( 'result' => diff --git a/tests/default/config/.phpunit-phpstan-dba-pdo.cache b/tests/default/config/.phpunit-phpstan-dba-pdo.cache index 7271847bd..0bacc0390 100644 --- a/tests/default/config/.phpunit-phpstan-dba-pdo.cache +++ b/tests/default/config/.phpunit-phpstan-dba-pdo.cache @@ -2582,6 +2582,13 @@ )), ), ), + 'SELECT count(*) AS c FROM typemix WHERE c_datetime=:last_dt' => + array ( + 'result' => + array ( + 5 => NULL, + ), + ), 'SELECT count(*) FROM typemix WHERE c_date = \'1970-01-01\'' => array ( 'result' => diff --git a/tests/default/data/doctrine-dbal.php b/tests/default/data/doctrine-dbal.php index ea1a41362..76e41a530 100644 --- a/tests/default/data/doctrine-dbal.php +++ b/tests/default/data/doctrine-dbal.php @@ -4,6 +4,7 @@ use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Types\Types; use function PHPStan\Testing\assertType; use staabm\PHPStanDba\Tests\Fixture\StringableObject; From f4ea8c680b453bf2b8cb8e120bd7e8c30c6a4837 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 18 May 2022 15:40:03 +0200 Subject: [PATCH 6/6] fix test --- tests/default/data/doctrine-dbal.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/default/data/doctrine-dbal.php b/tests/default/data/doctrine-dbal.php index f4641391e..d02eb8825 100644 --- a/tests/default/data/doctrine-dbal.php +++ b/tests/default/data/doctrine-dbal.php @@ -222,8 +222,10 @@ public function dateParameter(Connection $conn) public function customTypeParameters(Connection $conn) { - $stmt = $conn->prepare('SELECT count(*) AS c FROM typemix WHERE c_datetime=:last_dt'); - $result = $stmt->execute(['dt' => new \DateTime()], ['dt' => Types::DATETIME_MUTABLE]); // XXX use bound parameter types instead + $result = $conn->executeQuery( + 'SELECT count(*) AS c FROM typemix WHERE c_datetime=:last_dt', + ['dt' => new \DateTime()], ['dt' => Types::DATETIME_MUTABLE] + ); assertType('Doctrine\DBAL\Result', $result); } }