date: Incompatible declaration of diffForHumans method

When i ran composer update i found that with the release of briannesbitt/carbon 1.23.0 the diffForHumans method declaration is incompatible with the current one:

ErrorException: Declaration of

Jenssegers\Date\Date::diffForHumans(
  ?Carbon\Carbon $since = NULL, $absolute = false, $short = false
)

should be compatible with

Carbon\Carbon::diffForHumans(
  ?Carbon\Carbon $other = NULL, $absolute = false, $short = false, $parts = 1
)

The related PR is https://github.com/briannesbitt/Carbon/pull/772

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 26
  • Comments: 39 (10 by maintainers)

Most upvoted comments

If you just add “nesbot/carbon”: “1.22.*” to composer.json it should override and work fine.

FYI: in my project, temporary switch to previous carbon version: in the composer.json find: "nesbot/carbon": "~1.0" replace with: "nesbot/carbon": "1.22.*"

Hope this works for you too.

forcing a composer require jenssegers/date:3.3.1 fixed my octobercms issue.

Still an issue.

Hi, sorry guys but yes, now Carbon has a fourth optional argument that allow multiparts diffs. I recommend to inspect if Jessengers Date really still needs to override diffForHumans. It could maybe benefit of the new Carbon implemantation and mabye mutualize evolution efforts of this method in Carbon.

Date::diffForHumans should be:

public function diffForHumans(Carbon $other = null, $absolute = false, $short = false, $parts = 1)

I can confirm that this issue is fixed in the 3.3.1 release, thanks @jenssegers!

@segsalerty2013 yes you’re right, I’ve got the error with php 7.0, but it’s OK with php 7.1 or php 7.2. This is due to symfony/translation:4.0 : https://github.com/symfony/translation/blob/master/Translator.php#L79

Try adding to your composer.json: “symfony/translation”: “3.4.*”

Glad you all got up and running again. I see in the end the override of diffForHumans() was just simply removed.

My recommendations to avoid such crises:

  • By extending Carbon you enter the protected level and so you need to have automated builds that tests your library against dev-master version of Carbon, this way, you will know something changed before the stable release and will be able to adapt softly, warn users, ask for Carbon project to take it into account…
  • By overriding a method you take a risk and must be aware of it. Before any override you should first consider other (often better) options:
    • Propose a PR to Carbon to allow your change in a way that does not need an override (setting, parameter, etc.)
    • Create an other method (so your users can use the native Carbon or yours and each method live its life)
    • If need some soft global override, intercept __call() that you’re sure it will not have prototype change
  • Check Carbon roadmap time to time (active PRs, future milestones) and check consequences. It’s legit that it’s the responsibility of library that extends an other, the same way Carbon watch DateTime coming changes.

@danhunsaker Adding a optional argument is not a breaking change. If we forbid ourselves to make those improvements, we will tag any change as major because if we say so, then adding a method is the same thing. If you used the same method name with other arguments count, it will break you app.

So I’m truly sorry but by extending Carbon, you enter the protected scope of Carbon, and semantic version only imply to keep backward compatibility for public scope. The same goes for us, when PHP add DateTime arguments, methods and so on from PHP 7.0 to 7.1 or to 7.2 (and it happened for several methods), it’s fine to have a minor release version, we have to adapt Carbon, and publish a new version with the PHP compatibility.

The PR for the change was validated for more than one year. And the same way we test Carbon on PHP alpha/beta version, Jenssegers/Date can test and prepare Carbon changes using dev-master.

Jenssegers/Date updated very quickly and deleting the custom diffForHumans from it seems to me a very good option. And the same way we chose minor tag, Jenssegers use also a minor release which is perfectly relevant to me.

Try v3.3.0 of this package.

The following solution should works for everyone:

Run at the root of your project:

composer require nesbot/carbon=1.22.*

This is the best thing to do until Jenssegers/Date publish a version compatible, then you will be able to remove it from you composer.json and benefit of the new features of Carbon.