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?
self::this()Phpstan resolvesselfalready and that causes the first error. But forselfno PHPDoc annotation exists IIRC. Would be delighted to learn something new, however even if it would, it would require that the annotation would makeself$thisand::'->' like so that Phpstans flawed assumption about the binding ofselfcan be circumvented. As outlined in my answer, I'd suggest you report this upstream (also what Paul T wrote).