Skip to content

Commit cadf7e4

Browse files
Seldaekclxmstaabstaabm
authored
Allow simulation of DateTime objects (#339)
Co-authored-by: Markus Staab <[email protected]> Co-authored-by: Markus Staab <[email protected]>
1 parent d5a4a6e commit cadf7e4

File tree

6 files changed

+43
-0
lines changed

6 files changed

+43
-0
lines changed

src/QueryReflection/QuerySimulation.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public static function simulateParamValueType(Type $paramType, bool $preparedPar
7373
return null;
7474
}
7575

76+
// TODO the dateformat should be taken from bound-parameter-types, see https://github.com/staabm/phpstan-dba/pull/342
77+
if ($paramType instanceof ObjectType && $paramType->isInstanceOf(\DateTimeInterface::class)->yes()) {
78+
return date(self::DATE_FORMAT, 0);
79+
}
80+
7681
$stringType = new StringType();
7782
$isStringableObjectType = $paramType instanceof ObjectType
7883
&& $paramType->isInstanceOf(Stringable::class)->yes();

tests/default/config/.phpstan-dba-mysqli.cache

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/default/config/.phpstan-dba-pdo.cache

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/default/config/.phpunit-phpstan-dba-mysqli.cache

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/default/config/.phpunit-phpstan-dba-pdo.cache

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/default/data/doctrine-dbal.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Doctrine\DBAL\Cache\QueryCacheProfile;
66
use Doctrine\DBAL\Connection;
7+
use Doctrine\DBAL\Types\Types;
78
use function PHPStan\Testing\assertType;
89
use staabm\PHPStanDba\Tests\Fixture\StringableObject;
910

@@ -218,4 +219,13 @@ public function dateParameter(Connection $conn)
218219
$fetchResult = $conn->fetchOne($query, [date('Y-m-d', strtotime('-3hour'))]);
219220
assertType('int|false', $fetchResult);
220221
}
222+
223+
public function customTypeParameters(Connection $conn)
224+
{
225+
$result = $conn->executeQuery(
226+
'SELECT count(*) AS c FROM typemix WHERE c_datetime=:last_dt',
227+
['dt' => new \DateTime()], ['dt' => Types::DATETIME_MUTABLE]
228+
);
229+
assertType('Doctrine\DBAL\Result', $result);
230+
}
221231
}

0 commit comments

Comments
 (0)