Infer datetime_immutable
DBAL type for \DateTimeImmutable
instance parameters
#8328
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The support for passing
\DateTimeImmutable
instance as a query parameter has been added to ORM in #1333 (the year 2015), a long time before immutable date types (datetime_immutable
etc) were introduced to DBAL in doctrine/dbal#2450 (2017).Back then, it made sense to treat
\DateTimeImmutable
(or any\DateTimeInterface
) in the same way as\DateTime
and infer parameter type asdatetime
. However, when immutable date types were later added to DBAL, it wasn't reflected anyhow in type inference in ORM and\DateTimeImmmutable
instances are still inferred asdatetime
DBAL type.This PR fixes this IMO incorrect behaviour of
ParameterTypeInferer::inferType()
: for a\DateTimeImmmutable
parameter, it now returnsdatetime_immutable
DBAL type; for\DateTime
or any other types implementing\DateTimeInterface
, it returnsdatetime
DBAL type as it did before.This behaviour is in line with
DateTimeImmutableType
handling only\DateTimeImmutable
andDateTimeType
handling any\DateTimeInterface
.Why? In most cases, it doesn't matter and
datetime
works for\DateTimeImmutable
parameters just fine. But it does matter if using custom implementation ofdatetime_immutable
type likeUTCDateTimeImmutableType
from simpod/doctrine-utcdatetime. Then the broken type inference is revealed.This is partially related to #6443, however, this PR isn't about custom DBAL types but about correct type inference for build-in types.