phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

False positive on access to protected/private properties and methods in macros

Open MrMage opened this issue 4 years ago • 2 comments

Bug report

Using PHPStan 0.12.71.

I am using macros like the following, annotating $this as described in https://github.com/nunomaduro/larastan/issues/166:

use GuzzleHttp\Client;

\opencensus_trace_method(Client::class, 'send', function ($request, $options) {
            /** @var Client $this */
            return [
                'name' => 'Guzzle\Client::send',
                'attributes' => [
                    'uri' => $this->buildUri($request->getUri(), $options),
                ],
                'kind' => Span::KIND_CLIENT,
            ];
        });

This results in the following warning:

Call to private method buildUri() of class GuzzleHttp\Client.

It seems like PHPStan is unable to recognize that $this has access to protected and private fields and methods in this context. Is it possible to inform it that those fields and methods are available?

MrMage avatar Feb 03 '21 12:02 MrMage

This bug report is missing a link to reproduction on phpstan.org.

It will most likely be closed after manual review.

mergeable[bot] avatar Feb 03 '21 12:02 mergeable[bot]

I've experienced the same when creating macro for Laravel's Eloquent Builder - when trying to reference any protected property or method I get the Call to protected method / property ....

Example would be:

Builder::macro('testingProtectedAccess',function () {

    /** @var Builder $this */
    return $this->model->getPerPage();
});

sebastiansulinski avatar Jun 04 '25 16:06 sebastiansulinski