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
--levelused: 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)
@nivv you need two asterisks at the beginning of the comment:
/** @var Builder $this */+1 for deprecating the
artisanone.Basically you have to create a root file in your project with the name
phpstan.neon.distand the content:./vendor/bin/phpstan analyse.Notes:
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:I’m still seeing this issue, maybe I’m doing something wrong.
Error from phpstan:
Since this issue went a bit off topic: I am still seeing this error with v0.4.2 and Laravel 6.3:
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:
–
Ondřej Mirtes
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.neonworks, 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.neonwhere 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:
–
Ondřej Mirtes