-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[5.7] Move Carbon macro methods #23938
Conversation
I would not rely on Carbon to implement the Macros. |
@jmarcher What you should be aware of is that now macros/mixins for Carbon can take place in two variables, that means: Illuminate\Support\Carbon::macro('test' function () { return 'foo'; });
Carbon\Carbon::macro('test' function () { return 'bar'; });
Illuminate\Support\Carbon::test(); // foo
Carbon\Carbon::test(); // bar So using a library that will use By removing the Illuminate\Support\Carbon overrides, Laravel users will be able to fully rely on the Carbon library documentation for all methods and will be able to use any third-party macros with no particular installation. |
@jmarcher why not? If the purpose of |
Well for me that means that if Carbon wants to add a breaking change to macros this will result in a breaking change for us. |
Last breaking changes that happened happened because of override incompatibility. No override, no incompatibility and from our side, we keep backward behavior in 1.x (we do not change or remove existing tests and they cover 100% of our code, all passed tests in the current version are guaranteed until 2.0). I make the commitment. What we do in minor releases is adding methods and classes (the same thing Laravel does in minor releases). |
@kylekatarnls: Is the macro implementation identical to Laravel's, or are there differences? |
Only one difference for PHP 5.3 compatibility, if the last argument of the closure is named |
IMHO 5.7 should remove If not removing it, then at least |
This would make migration from 5.6 to 5.7 nearly impossible for a lot of applications. A change of date library should wait for a major release (6.0), it should not happen on a minor release. |
Laravel 5.7 is the next major release. Laravel does not follow SemVer. From the docs:
|
Yeah, I'd agree if Laravel follows Semver, but it doesn't, so removing for 5.7 would be fine because as @36864 says that is the next major release. As you've suggested that the there are no differences between the Carbon and the Laravel implementations, it should just be a search/replace of import statements? |
@lindyhopchris Sorry I did not understand you were speaking about the wrapper only; I thought you were suggesting using raw DateTime or an other library. I think this wrapper is still a good place for Laravel overrides. If in some future version, Laravel needs a date feature that Carbon cannot support, it will be needed to re-change all the imports. But else, I'm quit OK to remove |
Problem with On the other hand it could be quite useful if one could specify the desired |
Amending type hints to the base Carbon\Carbon class would fix that particular use case and let people use other datetime libraries that extend Carbon, while maintaining BC for Illuminate\Support\Carbon. Might be worth a PR there. |
Are we OK with this being merged as-is? |
There's a very narrow path to a BC break here, as any code that explicitly relies on the Macroable trait being present in the Carbon class will now fail, and the exception messages for non existing macros are now different. Intuitively I would think the odds of this actually breaking anyone's existing code are "improbable at best", but it should at least be mentioned in the release notes. As a side note, it would be neat to know if we're planning on removing the Support\Carbon class entirely in a future patch and use Carbon\Carbon directly, thus allowing for carbon extensions to be dropped in. |
@taylorotwell > OK to merge it as-is. Complete removing of @36864 > |
<?php
namespace Illuminate\Support;
use Carbon\Carbon as BaseCarbon;
class Carbon extends BaseCarbon
{
//
} Should we use the next major version update of Laravel as an opportunity to finally get rid of it? |
I’m for it sticking around but understand either way. I utilize the extended class quite a bit. |
Hi @devcircus, the only reason to keep it I see is backward compatibility, can you explain why you're for it? Why couldn't you replace every |
In one big code repository I have, we even made a PHPStan rule which disallows use of Carbon\Carbon and requires to use Illuminate\Support\Carbon. Why? Homogeneity, actually. When you use But then devs would, rightly so, just use Carbon\Carbon in other places. And sometimes they would use the CarbonInterface, too. Etc. Then you end up with Maybe something changed recently, still on 5.8 and quickly tried 6.x but actually got the same result. From a technical standpoint I wouldn't mind a change. Would probably simplify things. Would this mean that e.g. framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php Lines 745 to 747 in e95bf0e
Carbon\Carbon instead?
Or, rather: I guess the framework-internal type hints would not cater for when someone activates the immutable variant via In the end it's just some clarification question how this would play out, thank you! |
Homogeneity is there too if you use
The /**
* @return Carbon\CarbonImmutable
*/
protected function asDateTime($value)
{
/** @var Carbon\CarbonImmutable $date */
$date = parent::asDateTime($value);
return $date;
} And this is more a PHP/PHPDoc limitation (no generic type or dynamic type support as it exists in some other languages like C# or TypeScript) than the Laravel implementation fault. So to focus on the removing hypothesis, considering a major version X of Laravel would remove This could be made smoother by first adding a |
Again, I get it. I don't really see the harm of keeping it but since it no longer adds any value, I wouldn't be against a search-and-replace next time I upgrade. |
If you have a fairly big codebase, i would recommend to make your own DateTime class and let that extend from Carbon. Then set up phpstan to always require that. Would be easy to search/replace. |
class_uses
result.