Skip to content
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 error when app is set to use CarbonImmutable #285

Merged

Conversation

sshead
Copy link
Contributor

@sshead sshead commented Jun 10, 2024

Replaces #284.

Recap: Currently the package breaks if your app has configured the Date facade to use CarbonImmutable instead of Carbon (as per this blog post by Michael Dyrynda).

To get it working is a one-line fix - changing one instance of now() to Carbon::now() in the Builder class.

Some comments/caveats:

  • The reason no other changes are needed is because of Eloquent's casting magic. It ensures that all datetime properties on Eloquent models contain an object of the correct class (either Carbon or CarbonImmutable, depending on how you've configured the Date facade), no matter what was used to set them.
  • However, that also means the doc blocks for the ShortURL and ShortURLVisit models are incorrect: all references to Carbon there should be Carbon|CarbonImmutable. Would changing those doc blocks be a breaking change? Otherwise I can change those as well.
  • The single test I've added isn't particularly defensive - it is my "failing test" that the one-liner fixes. I couldn't work out a good way of future-proofing similar bugs, short of doing something so that you can run the entire test suite with the environment set to use CarbonImmutable ... not sure that's worth doing!

Hope this is an improvement on the last effort.

@ash-jc-allen
Copy link
Owner

Hey @sshead! Sorry for the slow response on this one - this week's been crazy busy haha! I'm intending on checking this PR out tonight or tomorrow and getting back to you on it 😄

@ash-jc-allen ash-jc-allen merged commit 48a55af into ash-jc-allen:master Jun 14, 2024
11 checks passed
@ash-jc-allen
Copy link
Owner

Hey @sshead, huge thanks for this fix and the test update too! I'd have only done pretty much the same thing myself 😄

The Carbon, CarbonInterface, and CarbonImmutable topic is something I'm definitely going to think about when I do the next major version. This works at the moment (and is the quickest/easiest to get released), but I reckon I'd prefer to use an approach like you proposed in your last PR in the long run. Maybe we can reopen that PR at some point in the future.

Thanks again, I appreciate it! 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants