Laravel-ide-helper: Static should be used instead

Created on 4 Sep 2020  ·  4Comments  ·  Source: barryvdh/laravel-ide-helper

Laravel 7.x
IDE helper 2.7

$query = User::query();
$total = $query->count();
$query->where(...);

On any method called non statically on the model PHPStorm is showing the error [EA] '...::count(...)' should be used instead. ... Source: .../_ide_helper.php

However if the method is called statically Laravel throws an error

Not sure if this is the ide helper or the laravel plugin tho? Or maybe I've just done something wrong in my code?

question

All 4 comments

Yes, this is a result of using ide-helper:generate.

Without ide-helper:

  • No IDE assistance for User::count()
  • Autocomplete for $query->count(), clicking on it brings you to \Illuminate\Database\Query\Builder::count

With ide-helper:

  • Autocomplete for User::count(), clicking on it brings you into _ide_helper.php
  • Autocomplete for $query->count(), clicking on it brings you into _ide_helper.php

    • but also warning [EA] …

Now, if we would make the generated stub in _ide_helper not static, we would get

  • Autocomplete for User::count(), clicking on it brings you into _ide_helper.php

    • but also warning [EA] …

  • Autocomplete for $query->count(), clicking on it brings you into _ide_helper.php

It's a trade-off 🤷‍♀️

Maybe the solution is to disable this inspection? The plugin authors do recognize already it's impractical in all cases, i.e. they have an exception for phpunit:
image

As you can see, I use it too but I don't find it much of an issue TBH. To me, slightly wrong inspection is better than having no autocomplete. The whole thing isn't perfect due to Laravels magic and ide-helper tries it best, well, to _help_. But I don't think it can ever be perfect.


That said: maybe there's a solution for fix all this, by using a different strategy to generate this stub files but I've no idea currently.

Thanks for the detailed explanation! I only reported the issue has I hadn't seen this before in previous versions of PHPStorm/IDE helper. Is it a new inspection?

It's just annoying having the yellow highlighting all through my controllers but I agree it's better to have the autocomplete. I just changed this to a weak warning so the error highlighting isn't as pronounced

Is it a new inspection?

Well, I've seen it for a long time but it's from the "EA Inspection plugin":

  • ide-helper => 3rd party to Laravel
  • Laravel => 1st party product
  • PhpStorm => 1st party product
  • EA plugin => 3rd party to PhpStorm

So it's a connection of different expectations from one 3rd party to another 3rd party plugin. Doesn't make the world easier 🤪

Well if it was all so easy half of us would be out of a job haha. Thanks much for your help and all the info!

Was this page helpful?
0 / 5 - 0 ratings