I’m registering a custom Carbon macro in my Laravel’s AppServiceProvider
Carbon::macro('itFormat', static function () {
return ucwords(self::this()->translatedFormat('l d/m/Y'));
});
But PHPStan insists that, in this code, self
is referred to the AppServiceProvider itself
Call to an undefined static method App\Providers\AppServiceProvider::this()
An easy solution (found here) should be to use a non-static function and a PHPDoc annotation targetting $this
, such as
Carbon::macro('itFormat', function () {
/** @var Carbon $this */
return ucwords($this->translatedFormat('l d/m/Y'));
});
But this is in contrast to the Carbon’s documentation itself, saying:
Note that the closure is preceded by static and uses self::this()
(available since version 2.25.0) instead of $this. This is the
standard way to create Carbon macros, and this also applies to macros
on other classes
Which may be the proper PHPDoc annotation for the “self in static function” way?