Carbon fork that implements DateTimeInterface and extends both DateTime and DateTimeImmutable

Created on 10 Jul 2015  路  12Comments  路  Source: briannesbitt/Carbon

Hi. I created fork of this library that use DateTimeInterface and extends both DateTime and DateTimeImmutable.

https://github.com/meebio/Carbon

Here is a small description:

Carbon library fork that extends both DateTime and DateTimeImmutable extensions with shared interface.

Original Carbon class is broken into CarbonInterface (interface extending DateTimeInterface) and CarbonTrait that implements that interface. There are 2 usable classes Carbon extending DateTime (that has exactly the same functionality as original Carbon class) and CarbonImmutable extending DateTimeImmutable. Both classes implement CarbonInterface and use CarbonTrait for common methods.

Summary:

  • Fork was done at 1.20.0 release.
  • No additional features were added.
  • All original unit tests are passed both by mutable and immutable implementation. I had to modify some tests for immutable class to properly work.
  • All tests that were applied to original Carbon class, have been duplicated and now this tests are applied to both mutable and immutable implementation. There should be better way to do this because there is a lot code duplication. I tried to find a way to use one code to test both implementations, but I couldn't find anything useful. Anyone know better way to organize it?
  • There are only few sample unit tests that checks mutability and immutability, that should be improved.
  • DateTimeInterface and DateTimeImmutable are available for PHP >= 5.5.0 only and this is biggest issue here.

What are your thoughts about it? What can be improved?

Related issues: #103 #2 #233

Most helpful comment

@briannesbitt Do you actually have any plans for a v2, with immutability? Just curious where you're at.

All 12 comments

You should allow issues on your fork IMO.
My question would be: Should it also have a bool isMutable() check for both classes to detect at runtime whether it is mutable or not? Would in some cases help for dynamically created objects to know what course of action to take.

@dereuromark
I enabled issues on my fork. Thanks for pointing that out :)

Method isMutable() totally makes sense, isImmutable() can be added too.

I didn't mention that, but there are two conversion methods added to one for each implementation: CarbonMutable has toImmutable() and CarbonImmutable has toMutable().

I think having one with boolean result might suffice :)

hi all,

hi bump this issues. it's possible to merge advance in this subject?

Thanks in advance.

@ektarum Hi
I can probably make new clean fork, that will be backward compatible (so easily mergeable). I'm not sure how to organize tests to not duplicate code. Hope it will be then accepted.

I created new fork and reimplemented changes trying to preserve as much backward compatibility as it is possible. I also added pull requests here: #426.

@jan-j Your changes have also been implemented in https://github.com/cakephp/chronos - just a heads up.

The general idea here is good but needs to obviously be in a v2.

@briannesbitt Do you actually have any plans for a v2, with immutability? Just curious where you're at.

Is there already a release for the immutable version?

@arjasco, for my needs which are nothing more than cloning, I use ->copy() before doing any modifying actions, for preserving the original Carbon object.

$today =  Carbon::today(); // 2017-06-07 00:00:00
$someOtherDay = $today->copy()->addDays(8); // 2017-06-15 00:00:00
echo $today; // will output: 2017-06-07 00:00:00

I close as it seems this fork is no longer maintained.

Was this page helpful?
0 / 5 - 0 ratings