[9.x] Use CarbonImmutable by default #38258
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This proposal is on the back of the recent additions to support immutable date casting in Eloquent (#38199) and subsequent discussion around benefits of using immutable dates by default.
To that end, this PR targeted for 9.x next January, aims to make the framework use immutable dates by default.
This is facilitated by updating the few places still referencing
Carbon\Carbon
directly with calls toIlluminate\Support\Carbon
and having that class subsequently extendCarbon\CarbonImmutable
.There was only a few code paths that needed updating, owing to the fact that they were relying on mutable behaviour previously, but overall, the surface area for change is quite small.
This is, of course, a breaking change (hence the target of 9.x) and should come with the caveat that if things are broken, that we recommend users of the framework to switch back to mutable dates by using
Date::use(\Carbon\Carbon::class)
in the upgrade guide.From what I've tested so far, the change will affect dates produced by:
now()
helperIlluminate\Support\Carbon
classIlluminate\Support\Facades\Date
facade (introduced in Laravel 5.8, but undocumented and probably unknown as a result)Usage of
Carbon\Carbon
directly will be unaffected.Using immutable dates will lead to more predictable behaviour with application and test code, as well as reducing confusion and unexpected bugs.
Keen to open a dialogue to discuss further, and any areas I missed.