diff --git a/src/QueryReflection/QuerySimulation.php b/src/QueryReflection/QuerySimulation.php index db50c6748..28d476359 100644 --- a/src/QueryReflection/QuerySimulation.php +++ b/src/QueryReflection/QuerySimulation.php @@ -73,6 +73,11 @@ public static function simulateParamValueType(Type $paramType, bool $preparedPar return null; } + // 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); + } + $stringType = new StringType(); $isStringableObjectType = $paramType instanceof ObjectType && $paramType->isInstanceOf(Stringable::class)->yes(); 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 37e045e8c..d02eb8825 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; @@ -218,4 +219,13 @@ public function dateParameter(Connection $conn) $fetchResult = $conn->fetchOne($query, [date('Y-m-d', strtotime('-3hour'))]); assertType('int|false', $fetchResult); } + + public function customTypeParameters(Connection $conn) + { + $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); + } }