8000 [PropertyAccess] Implement DateTime type conversion in PropertyAccess by curry684 · Pull Request #47776 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccess] Implement DateTime type conversion in PropertyAccess #47776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for Gi 10000 tHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

curry684
Copy link
Contributor
@curry684 curry684 commented Oct 3, 2022
Q A
Branch? 6.2
Bug fix? no
New feature? no
Deprecations? no
License MIT

In short

This PR enables implicit conversion between \DateTime and \DateTimeImmutable objects in the PropertyAccess component, allowing developers to store values of one type where the other was typed.

Longer story

Since PHP introduced the \DateTimeImmutable type, and related to that the \DateTimeInterface, and afterwards added strict typing, programmers have been banging their heads over converting between 2 datetime types which essentially implement the same thing but are incompatible without helper methods. nelmio/alice#1096 is a notable example of the frustrations this can cause, it makes no sense for a helper library like Faker to implement tons of extra methods just to support that PHP has 2 different ways of storing dates. Similar issues occur with Symfony Forms and Doctrine in certain integrations and combinations.

Which got me thinking why the PropertyAccess component, at the core of a lot of these integrations, doesn't simply support automatic conversion between the types. Conversion from a DateTime to a DateTimeImmutable is by definition harmless - the copy in the target is locked. The other way around it may not be entirely safe, as a programmer could expect a modification of the target to be reflected in the source property, however this is not a logical expectation since in that case the source was Immutable. Hence I implemented the implicit conversion both ways.

Coercion was set up to be more extensible for future similar cases, and will only be attempted for method-based and public access. Coercing values for adder/remover makes no sense as it would (nearly) always break the remover functionality. Likewise coercion is specifically not attempted for union/intersection types, only for explicitly named types.

@@ -13,5 +13,4 @@

class ExtendedUninitializedProperty extends UninitializedProperty
{

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewers - this was an unintended side effect of running CS Fixer, should happen anyway.

@carsonbot
Copy link

Hey!

I think @PhilETaylor has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas
Copy link
Member

Looks like I missed your PR when I submitted #50295
Very good idea ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0