-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
allow construct FrozenClock without argument #31
Conversation
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.
Just a tiny thing 🙂
Thanks for helping out with this lib!
@lcobucci hey there
I was having the very same dilema :/ I was considering several variations, let me show them and make some pros and cons (considering the structure, method names can be different). Current state of things
Pros:
Cons:
Default constructible FrozenClock
Pros:
Cons:
Default constructible with timezone
Pros:
SystemClock->freeze
Cons:
SystemClock::freeze
Cons:
FrozenClock::fromSystemTime
Pros:
FrozenClock::fromClock
Pros:
Cons:
global function (alternative to class extensions like in C#)
Pros:
Cons:
Using the same lower level API (DateTimeImmutable constructor) in two implementations of the same interface is not a code duplication, imho, and so i didn't put this into the "cons" sections. Where I consider code repetition, it is how much the caller must repeat, not the implementation. So to summarize, I like the "default constructible with timezone" variant the most (basically what I implemented in this PR, except I didn't care about the custom timezone variation). I am willing to redo this to any variant you choose. But, honestly If any of the variants below "default constructible" is chosen, I will probably use the "current state of things" variant in my application. And so at least for me, the feature will be useless. Weeell, reconsidering, maybe the fromSystemTime() variant might be ok too... |
@lcobucci Any update on this? I think the best we can agree on is |
Sorry about my delay. Honestly I'm not convinced that it's the best design. It's surely the most practical to meet both our needs. However, I disagree with SRP violation you brought since the client using the API might be interested in both getting the current time and freezing things. When I use this library I always have an instance of the system clock hooked on the DI container, so calling Could you share how you configure things so that we can have a more informed decision? |
If I wanna freeze at current system time I am indeed interested in both those things. The system time is provided by the DateTimeImmutable API, not by SystemClock. Responsibility of SystemClock is to fulfil the Clock interface which is to provide current time in an abstract way, in this case by propagating the actual system time. If There is probably no consumer that will want to use both those methods (now() and freeze()). On other hand, if
Well in my use case, I don't need SystemClock instance, actually I don't even have a DI container, because I only create a single end-user object. There is just a bunch of constructors called and the objects composed together to create the result, which is a symfony console application. It all goes like this:
|
@slepic thanks for sharing your setup (and also for the discussion). Let's go with Would you mind handling the changes? |
@lcobucci Yeah, definitely, I may have some time on the weekend to do it. Btw please choose the name for the method. I find myself usualy struggling with naming things :D I would prefer one over the other and later on realize I shouldn't have :D Personally I think i prefer fromSystemTime, even tho it is longer. I'm even thinking about just There is one more thing I am worried about though. Do you remember #32 ? Of course, I can just implement this feature in the same way that SystemClock behaves and if the timezone logic changes, both will have to be fixed... (starting to realize why you said you didn't like nullable parameters, I actually don't too, but I just didn't see any problem with native php classes which themselves have default constructor. now i realize they use global state and I dont like it....) |
Thanks!
I had the same thought these days... Using system timezone is kind of the "PHP way" (and honestly I just hope folks use UTC). Changing our default is a BC-break, which I'm completely fine doing on the next major version. However we may introduce some named constructors to eventually remove the global state dependency.
What do you about using |
I'm all in about being explicit. But I have little objection, since UTC=Universal Time Coordinated, it seems weird to repeat the "time" word. What do you think about eventually ending up at this:
I wouldn't mind doing it right away... |
Silly me 🤦
I'd like to keep the scope of this PR only about the We can have another PR to add |
Sounds good to me. |
As some food for thought, here's what @OskarStark and I do in a project we work on:
|
@localheinz oh why is that a separate repo? |
I once needed I have dropped support for PHP 7.0 since. Don’t get me wrong, I am not advertising here - only suggesting a slightly different approach that might be as useful. |
I think we can close this one too, since #35 resolved the problem... thanks for coop |
Hi,
I have a real world project where I have implemented basically the same as your package does (except i call Clock ClockInterface, and FronzenClock as ConstantClock instead :)). I have randomly encountered this package and I think that I would replace my code with this package instead of keeping it as part of an unrelated private repository that just wants to use the functionality.
However I have one use case, which is not unsolvable in your implementation, but it would be simpler to allow create frozen clock without arguments, basically to keep the time the same for all consumers thorughout a longer process.