-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix: make Time immutable #6771
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: make Time immutable #6771
Conversation
79468b0
to
fde0a67
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is another situation of "do we break it to fix it?" Let's get some discussion on #develop and maybe even the forums before we jump in.
Also: instead of TimeLegacy
I would like to see a TimeMutable
class. We could discuss whether that includes changing methods like setTimezone
to be mutable.
If we have Another option is to make the current |
I would like to see the second option. Also, we can extract the methods which do not change in implementation into a trait/traits so that we minimize code duplication. |
Deprecation and adding |
I'm personally not a fan of two classes for this honestly. I know I've been the one arguing against breaking changes, but in this case I think it's justified because it has always been billed as an immutable class and the fact that we determined it wasn't I think definitely qualifies as a bug, personally. |
Usually, I'm like... if there is a bug - fix it, and that's it, but in this case, I'm not sure. Without a doubt, people have already created a lot of code that depends on the current implementation, and manipulating dates can be challenging and tricky the more operations we're doing. I would keep the I know it's not ideal, but it would help keep everyone happy without additional maintenance.
|
@michalsn I think that sounds like a fair compromise. |
What is the service? |
Lol... I guess I don't use this class "often". My advice is more or less useless then. |
@paulbalandan Before refactoring: --- Time.php 2022-10-29 17:26:50.000000000 +0900
+++ TimeLegacy.php 2022-10-29 17:28:38.000000000 +0900
@@ -24,14 +24,17 @@
use ReturnTypeWillChange;
/**
- * A localized date/time package inspired
- * by Nesbot/Carbon and CakePHP/Chronos.
+ * Legacy Time class.
*
- * Requires the intl PHP extension.
+ * This class is only for backward compatibility. Do not use.
+ * This is not immutable! Some methods are immutable,
+ * but some methods can alter the state.
*
* @property string $date
+ *
+ * @deprecated Use Time instead.
*/
-class Time extends DateTimeImmutable
+class TimeLegacy extends DateTime
{
/**
* @var DateTimeZone
@@ -118,7 +121,7 @@
* Returns a new Time instance while parsing a datetime string.
*
* Example:
- * $time = Time::parse('first day of December 2008');
+ * $time = TimeLegacy::parse('first day of December 2008');
*
* @param DateTimeZone|string|null $timezone
*
@@ -314,7 +317,7 @@
/**
* Creates an instance of Time that will be returned during testing
- * when calling 'Time::now' instead of the current time.
+ * when calling 'TimeLegacy::now()' instead of the current time.
*
* @param DateTimeInterface|self|string|null $datetime
* @param DateTimeZone|string|null $timezone |
Yes. Good news is there seems many users who don't use Time. And all the description in the User Guide is immutable. Only some inherit methods are mutable. If fixing Time does not affect many users, it would be better to fix Time. |
d4b2a6c
to
3f0ba03
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good!
I posted. https://forum.codeigniter.com/showthread.php?tid=84677 |
It seems to be that current sentiment is leaning towards "just fix it". I think the proposed solution (TimeLegacy plus switch parent) is a good fit. |
3f0ba03
to
12f3a1e
Compare
Added the docs. |
Co-authored-by: MGatner <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, the difference of Time
and TimeLegacy
, although both using TimeTrait
, is the extended parent. Very nice!
🎉 |
Description
Fixes #6762
Time
will extendDateTimeImmutable
Time
toTimeLegacy
Checklist: