cakephp / chronos

A standalone DateTime library originally based off of Carbon
http://book.cakephp.org/chronos
MIT License
1.34k stars 61 forks source link

Provide PSR-20 Clock implementation #443

Closed odan closed 8 months ago

odan commented 8 months ago

This PR provides a PSR-20 compliant Clock implementation for the Chronos package.

The PSR-20 Clock interface defines a standard way to interact with system time, allowing for better testability and flexibility.

othercorey commented 8 months ago

I am away right now but I think the term ChronosClock is misleading and confusing compared to the other classes. Can we expose this some other way?

odan commented 8 months ago

The idea was to name it ChronosClock because it returns a Chronos instance, that is also a TimeTimeImmutable instance.

Other class names: Clock, ClockFactory or DateTimeImmutableFactory.

odan commented 8 months ago

After thinking about it, I guess my approach doesn't make sense from a Chronos perspective, because if you are already using Chronos, you may not need the ClockInterface at all.

markstory commented 8 months ago

After thinking about it, I guess my approach doesn't make sense from a Chronos perspective, because if you are already using Chronos, you may not need the ClockInterface at all.

I think it is another way for us to participate in the community standards within the larger PHP ecosystem. That alone has value I think.

othercorey commented 8 months ago

What if we implemented it with an anonymous class that's returned by a static helper. That would provide easy access to the interface, right?

odan commented 8 months ago

Error: Type DateTimeImmutable is not used in this file.

phpcs complains about this problem even though Chronos is inherited from DateTimeImmutable.

What if we implemented it with an anonymous class that's returned by a static helper. That would provide easy access to the interface, right?

A static method cannot then be used like an interface, for example when using dependency injection.

othercorey commented 8 months ago

ChronosFactory or ClockFactory should work then.

markstory commented 8 months ago

There is one last lint error to fix and then we should be able to move forward with this.

othercorey commented 8 months ago

The class needs to be renamed to ClockFactory.