larastan: False positive on Macros ($this is not what larastan think it is), there is a way to tell larastan “ignore this” ?

  • Larastan Version: 0.2.10
  • --level used: 5

Description:

While defining a Query Builder macro, larastan makes the wrong assumption about $this type inside the macro: it complains the AppServiceProvider does not have a where method. shellcheck has a standard way to put comments in the code to make it ignore certain errors, for example.

I think instead of trying to make larastan really perfect (which is a wonderful target but not always achievable), a more real-world implementation will have a way to declare “larastan, please ignore this”.

Laravel code where the issue was found

        // Builder macro search
        Builder::macro('search', function ($attributes, string $searchTerms) {
            $this->where(function (Builder $query) use ($attributes, $searchTerms) {
                foreach (array_wrap($attributes) as $attribute) {
                    $query->orWhere(function ($query) use ($attribute, $searchTerms) {
                        foreach(explode(' ', $searchTerms) as $searchTerm) {
                            $query->where($attribute, 'LIKE', "%{$searchTerm}%");
                        }
                    });
                }
            });

            return $this;
        });

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 4
  • Comments: 21 (9 by maintainers)

Most upvoted comments

@nivv you need two asterisks at the beginning of the comment: /** @var Builder $this */

+1 for deprecating the artisan one.

Basically you have to create a root file in your project with the name phpstan.neon.dist and the content:

includes:
    - ./vendor/nunomaduro/larastan/extension.neon
parameters:
    level: 5
    paths:
        - app
  1. And then just use phpstan directly:

./vendor/bin/phpstan analyse.

Notes:

  • I am considering deprecate the Artisan Command code:analyse. And force people to use phpstan directly. What do you folks think?

The thing with /* @var Collection $this */ is now fixed in PHPStan’s dev-master, soon to be released as 0.12.3:

  • phpstan/phpstan-src@a020fdc
  • phpstan/phpdoc-parser@a6d1352

I’m still seeing this issue, maybe I’m doing something wrong.

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        Builder::macro('whereLike', function ($attributes, string $searchTerm) {
            /* @var Builder $this */
            $this->where(function (Builder $query) use ($attributes, $searchTerm) {
                foreach (Arr::wrap($attributes) as $attribute) {
                    $query->when(
                        str_contains($attribute, '.'),
                        function (Builder $query) use ($attribute, $searchTerm) {
                            [$relationName, $relationAttribute] = explode('.', $attribute);
                            $query->orWhereHas($relationName, function (Builder $query) use ($relationAttribute, $searchTerm) {
                                $query->where($relationAttribute, 'LIKE', "%{$searchTerm}%");
                            });
                        },
                        function (Builder $query) use ($attribute, $searchTerm) {
                            $query->orWhere($attribute, 'LIKE', "%{$searchTerm}%");
                        }
                    );
                }
            });
            return $this;
        });
    }

Error from phpstan:

 ------ --------------------------------------------------------------------------
  Line   Providers/MacroServiceProvider.php
 ------ --------------------------------------------------------------------------
  30     Call to an undefined method App\Providers\MacroServiceProvider::where().
 ------ --------------------------------------------------------------------------


 [ERROR] Found 1 error

Since this issue went a bit off topic: I am still seeing this error with v0.4.2 and Laravel 6.3:

Collection::macro('sortNatural', function ($callback) {
    /* @var Collection $this */
    return $this->sortBy($callback, SORT_NATURAL | SORT_FLAG_CASE);
});

PHPStorm properly parses the $this typehint, phpstan/larastan just ignores it.

Closing this as it’s fixed in PHPStan.

PHPStan allows to ignore specific errors in a single file only.

And it’s also how all the other extensions are written.

On Wed, 24 Oct 2018 at 12:21, Ondřej Mirtes ondrej@mirtes.cz wrote:

I’d prefer that, you don’t have to mirror all the same stuff PHPStan commands too (there will be more commands in the near future).

On Wed, 24 Oct 2018 at 11:07, Viktor Szépe notifications@github.com wrote:

  • What do you folks think?

Not that Laravelly!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/nunomaduro/larastan/issues/166#issuecomment-432576641, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGZuMyuFe0XiLsfPQW24D9jE9055g9Hks5uoC24gaJpZM4XyGbk .

Ondřej Mirtes

Ondřej Mirtes

* What do you folks think?

Not that Laravelly!

@fgilio It’s not possible to persuade PHPStan about this in an extension.

Ok, read about phpstan.neon and I think you didnt understood what I was talking about ‘ignoring errors’.

shellcheck has that capability that Im talking about:

When you have a false positive in your code you can put a comment one line earlier in your code instructing shellcheck to ignore that particular error, so you can continue to analyse the rest of the same source file.

With the tools on phpstan.neon Im only finding a way to ignore whole directories, exclude whole files or ignore particular errors in the whole project.

phpstan.neon works, too.

you can create an alias to call code analyse:

alias ca='$(git rev-parse --show-toplevel)/vendor/bin/phpstan analyse’

in the meantime Im ignoring a whole trait in phpstan.neon where phpstan is loosing his marbles with fancy Eloquent where()-s and references to eloquent events.

I’d prefer that, you don’t have to mirror all the same stuff PHPStan commands too (there will be more commands in the near future).

On Wed, 24 Oct 2018 at 11:07, Viktor Szépe notifications@github.com wrote:

  • What do you folks think?

Not that Laravelly!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/nunomaduro/larastan/issues/166#issuecomment-432576641, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGZuMyuFe0XiLsfPQW24D9jE9055g9Hks5uoC24gaJpZM4XyGbk .

Ondřej Mirtes