Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Clock] AddDatePoint: an immutable DateTime implementation with stricter error handling and return types#51415

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 GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:6.4fromnicolas-grekas:clock-datetime
Sep 26, 2023

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekasnicolas-grekas commentedAug 17, 2023
edited
Loading

QA
Branch?6.4
Bug fix?no
New feature?yes
Deprecations?no
Tickets-
LicenseMIT
Doc PR-

The nativeDateTime/Immutable classes have some legacy that we can get rid of by creating a child class.

Here comesSymfony\Component\Clock\DatePoint, which extends the nativeDateTimeImmutable and provides the following advantages:

  • usable in defaults expressions
  • shorter (short) class name
  • throws exceptions instead of returning false
  • and last but not least, uses the global static clock to define "now" (akanew DatePoint is compatible withClock::set())

This also opens the possibility to add more methods to thisDatePoint class in the future if we want to. <- we don't want that :)

artyuum and alexislefebvre reacted with thumbs up emojiWebMamba reacted with hooray emojiHeahDude and dmaicher reacted with eyes emoji
@derrabus
Copy link
Member

The nativeDateTime/Immutable classes have some legacy that we can get rid of by creating a child class.

Nice. 🙂

This also opens the possibility to add more methods to thisDateTime class in the future if we want to.

That's my biggest fear about adding such a class, tbh.

If by "more methods" we mean polyfills for features that will be added in future PHP versions: yes, totally. But please, let's not open this class up for all kinds of utility methods. 🙈

Oh, and please add tests for that new class. 🙏🏻

Alpheus and jvasseur reacted with thumbs up emoji

@GromNaN
Copy link
Member

This also opens the possibility to add more methods to thisDateTime class in the future if we want to.

That's my biggest fear about adding such a class, tbh.

If by "more methods" we mean polyfills for features that will be added in future PHP versions: yes, totally. But please, let's not open this class up for all kinds of utility methods. 🙈

We don't want to create a newCarbon class.

derrabus, OskarStark, and artyuum reacted with thumbs up emoji

@antonkomarev
Copy link

Have you looked at the Brick DateTime library? Its API looks very good and has many types for many cases. Much more stricter and more powerful than Carbon.

ste93cry and jvasseur reacted with thumbs up emoji

@derrabus
Copy link
Member

We don't want to create a newCarbon class.

My point exactly.

GromNaN and welcoMattic reacted with thumbs up emoji

@nicolas-grekasnicolas-grekasforce-pushed theclock-datetime branch 4 times, most recently fromd158df1 to0dd8cfaCompareAugust 17, 2023 10:33
@nicolas-grekas
Copy link
MemberAuthor

PR ready, with tests !

We don't want to create a new Carbon

Same here, I stroke the line in the PR description. Guarding this is why we have a core-team :)

Have you looked at the Brick DateTime library

I didn't, but we just ruled such approaches out so 🤷

@nicolas-grekas
Copy link
MemberAuthor

Next steps to consider:

But before, votes pending @symfony/mergers ;)

@wouterj
Copy link
Member

wouterj commentedAug 18, 2023
edited
Loading

I think it's not a good idea to name this class similar to one from PHP's stdlib that has different behavior. You'll have to scroll up to the use statements to know ifnew DateTime means the PHP or Symfony one in any project.

The only case where this is desired is our polyfill packages, which provide polyfills for stdlib features. Eventhough all behavior changes in this class are probably the desired behavior forDateTime of all PHP internal devs, there is no official agreement/RFC that this is truly the behavior of the next PHP version as far as I know. We should make sure not to pretend to make a polyfill.

Kocal, OskarStark, chalasr, ste93cry, dmaicher, and fancyweb reacted with thumbs up emoji

@kbond
Copy link
Member

kbond commentedAug 18, 2023
edited
Loading

I think it's not a good idea to name this class similar to one from PHP's stdlib that has different behavior.

Timestamp?

@Kocal
Copy link
Member

Kocal commentedAug 18, 2023
edited
Loading

To me, PHP should have only offered aDateTime class (noDateTimeImmutable) which would be immutable, and I understand that you want to improve things.

But I don't think this is the job of a framework / component to do that, as the class name is identical but for a totally different and breaking behavior. Sure you explicity import it, but people will need to check twice if thisDateTime is imported from the Symfony Clock or is the one from PHP core.

@nicolas-grekasnicolas-grekasforce-pushed theclock-datetime branch 2 times, most recently from70cce4e to6bd09cfCompareAugust 21, 2023 09:50
@nicolas-grekas
Copy link
MemberAuthor

DateTime has my preference. It's how it should have been from the beginning. Unless they go with a very very bad BC break, PHP can't fix this. The ship has sailed on the topic 🤷 .

Figuring out the namespace requires a basic IDE and hovering the symbol, no need to scroll up. For ppl that don't useuse for root-namespaced classes (following Symfony's recommended CS), there's nothing to do.

there is no official agreement/RFC that this is truly the behavior of the next PHP version

I'm not sure to get what you mean here. The proposed class is not a polyfill. It's a stricter DateTime. We don't need any agreement from php-core to be future proof, thanks to LSP.

Still, if we want another name, I'd proposeNow.
This came to my mind when I realized that the implementation of thenow($modifier) helper can be reduced to the following (if you look at the attached patch, the implementation has a few more lines, but that's just for optimizing performance.):

functionnow(string$modifier ='now'){returnnewNow($modifier);}

@kbond
Copy link
Member

Not a fan ofNow:

publicfunction getLastUpdated():Now
nicolas-grekas, artyuum, and DavidBadura reacted with thumbs up emoji

@GromNaN
Copy link
Member

ClockDateTime would be descriptive. Even if that repeats theSymfony\Component\Clock namespace.

chalasr and welcoMattic reacted with thumbs up emoji

@ro0NL
Copy link
Contributor

BetterDateTime? because why not 😅

HeahDude reacted with laugh emoji

@nicolas-grekas
Copy link
MemberAuthor

DateMark
DatePoint
DateTick
?

@wouterj
Copy link
Member

To be honest, since typing my previous message I'm a bit on the fence on whether we should want this feature at all. I don't feel comfortable with a framework that "fixes"/"patches" the standard library.
In my eye, Symfony has always provided low and high level APIson top of the standard library. This is strong: you're not writing some custom language based on PHP, you're just writing PHP. I like how we as Symfony developers aren't "writing Symfony" like everyone used to say in the jQuery days, but most people in the community are aware that they are writing PHP.

Of course, this is only one class, hence I'm not 100% against it, but if we accept this, would we also accept e.g. adding new namespaced functions that introduce strict error handling to stdlib functions that currently return false on error? (i.e.https://github.com/azjezz/psl) And if we don't (I hope we don't!), why would we accept this?


This is a language issue that should be solved on language level imho. Create a new date time object (you can directly properly namespace it) and make it behave like it should with our current experience. In a major version or two, deprecate the current date time objects and we're done. PHP has managed much bigger API BC breaks like this (e.g.mysql_* to MySQLi/PDO).

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedAug 21, 2023
edited
Loading

The core motivation for adding this class on my side is to be able to create date-time instances that leverage the global static clock. Aka to makenew DateTime mockable. That's a unique feature that can't be provided by any future PHP version. The rest is a nice bonus to me and I'd agree with you if strictness was the only argument.

One could say that thenow() helper is enough, but it can't be used as a default argument value. Adding the class would:

functionfoo(DateTime$date =newDateTime) {...}
GromNaN, kbond, mtarld, OskarStark, and B-Galati reacted with thumbs up emoji

@wouterj
Copy link
Member

wouterj commentedSep 3, 2023
edited
Loading

Thanks for the explanation. While using Cronos, I haven't found the need tonew DateTime as a default value, but I can see the added value of it for the Clock component. Now, yourNow name suggestion also makes lots of sense :)

I'm now learning towards reducing this class to exactly this use-case: being thenow() equivalent for cases where you need a constant expression, without all the "API improvements". Something along the lines of this:

class Nowextends \DateTimeImmutable{publicfunction__construct()    {$now = Clock::get()->now();parent::__construct($now->format('c'),$now->getTimezone());    }}

This does not broaden the scope of the Clock component and keeps us far away from inventing our own language :)

Kocal reacted with thumbs up emoji

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedSep 14, 2023
edited
Loading

I like it too! PR updated,TimePoint FTW!

OskarStark, kbond, and smnandre reacted with heart emoji

@weaverryan
Copy link
Member

TimePoint is fine for me. I preferDatePoint as "Date" makes a closer association withDateTime than the word "Time".

But we can also call itRyansDate - that's probably my favorite 😇

smnandre, OskarStark, yceruto, dunglas, Alpheus, derrabus, and chalasr reacted with laugh emoji

@nicolas-grekasnicolas-grekas changed the title[Clock] AddTimePoint: an immutable DateTime implementation with stricter error handling and return types[Clock] AddDatePoint: an immutable DateTime implementation with stricter error handling and return typesSep 26, 2023
@nicolas-grekas
Copy link
MemberAuthor

Updated toDatePoint 🤘

I like that typingDate when using autocompletion might help discover the new class.

smnandre, Kocal, and javiereguiluz reacted with thumbs up emojiOskarStark reacted with hooray emoji

@OskarStark
Copy link
Contributor

That makes sense 👍🏻😃

@fabpot
Copy link
Member

Thank you@nicolas-grekas.

OskarStark reacted with hooray emojiOskarStark reacted with rocket emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@javiereguiluzjaviereguiluzjaviereguiluz left review comments

@GromNaNGromNaNGromNaN left review comments

@stofstofstof left review comments

@derrabusderrabusderrabus left review comments

@fabpotfabpotfabpot approved these changes

@kbondkbondkbond approved these changes

@OskarStarkOskarStarkOskarStark approved these changes

+4 more reviewers

@AlpheusAlpheusAlpheus approved these changes

@ro0NLro0NLro0NL left review comments

@HeahDudeHeahDudeHeahDude left review comments

@fancywebfancywebfancyweb approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

6.4

Development

Successfully merging this pull request may close these issues.

22 participants

@nicolas-grekas@derrabus@GromNaN@antonkomarev@wouterj@kbond@Kocal@ro0NL@OskarStark@javiereguiluz@stof@aaa2000@remi-vasco@bendavies@craigh@smnandre@weaverryan@fabpot@Alpheus@fancyweb@HeahDude@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp