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

Created on 20 Oct 2018  Â·  21Comments  Â·  Source: nunomaduro/larastan

  • 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;
        });
question

Most helpful comment

+1 for deprecating the artisan one.

All 21 comments

ignore certain errors

I hope we can fix this!

@sdbruder @szepeviktor You can already ignore errors using Phpstan/Larastan. Tomorrow I will update the readme about this.

cool, waiting the readme update. for the time being I’ve added an empty where method on the AppServiceProvider only to satisfy phpstan.

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

    1.
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?
* What do you folks think?

Not that Laravelly!

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

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

+1 for deprecating the artisan one.

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.

Yes, PHPStan does yet support local ignoring.

On Fri, 26 Oct 2018 at 02:50, Sergio Bruder notifications@github.com
wrote:

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.

—
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/nunomaduro/larastan/issues/166#issuecomment-433251348,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGZuFBn5v2xOHeAfxrkNsv2_s2t1a9Dks5uolxggaJpZM4XyGbk
.

>

Ondƙej Mirtes

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.

@spaceemotion phpstan after 0.11.8 has a feature that prevents you from var-ing $this :)

+1

@sdbruder @szepeviktor You can already ignore errors using Phpstan/Larastan. Tomorrow I will update the readme about this.

phpstan implementation to ignore errors is too broad: it’s global.
shellcheck implementation (a comment on code) is local, it’s “ignore THIS error in the next line” only.

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

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

Closing this as it's fixed in PHPStan.

Is there a way to configure Larastan to do the /* @var Collection $this */ automatically?

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

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

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

Was this page helpful?
0 / 5 - 0 ratings