2

So this has been asked several times but I'm yet to find an answer that works.

I'm using PhpStorm - 2016.3.2 and Laravel 5.4

enter image description here

I have tried using https://github.com/barryvdh/laravel-ide-helper and also the Laravel plugin for PhpStorm.

I tried checking the option "Downgrade severity if_magic methods are present in class" - this didn't work.

The only thing I can do to solve this is to turn the warnings off completely for undefined methods, but turning features like this off defeat the point of using an IDE.

Has anyone found a way to solve this?

Sources:

https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/15

https://laracasts.com/discuss/channels/general-discussion/why-does-phpstorm-not-recognise-all-the-classes?page=1

PhpStorm laravel 5 method not found

https://github.com/barryvdh/laravel-ide-helper

Community
  • 1
  • 1
Brad
  • 8,044
  • 10
  • 39
  • 50
  • *"So this has been asked several times but I'm yet to find an answer that works."* Please share links to those questions -- I would like to see what they about and what they offer. Quite possibly they are about different things (compared to your *simple* case). – LazyOne Mar 05 '17 at 00:34
  • I have added some of the links of places Ive looked. Most say the same thing tbh – Brad Mar 05 '17 at 00:43
  • Thanks. The links you have provided more about cases where magic methods are used .. or about some function that returns some object that can be of many types. Your case here is NOT specific to Laravel -- you will see the same with ANY framework, even your own code/library when you will be passing any object instance in the same way. – LazyOne Mar 05 '17 at 10:44
  • You have **very simple and basic case here**: you are passing parameter to a function without specifying what that parameter is. Either use type hints that PHP natively supports (as `Ross Wilson` showed).. or use PHPDoc to do that (`@param` tag) -- https://phpdoc.org/docs/latest/references/phpdoc/tags/param.html. Using inline PHPDoc with `@var` is not needed in this particular case -- the above 2 are much more suitable/correct ways; but it can be used in other/more complex cases. – LazyOne Mar 05 '17 at 10:45
  • Did one of the posts below answer your question, @Brad, or do you need further information? – Rwd Mar 05 '17 at 13:37

2 Answers2

7

PHPDoc blocks can come in handy for this.

You can add this PHP comment just before the statement return $query, inside the function

/** @var $query \Illuminate\Database\Query\Builder */

In this way PHPStorm will correctly recognize the method

Leonardo Atalla
  • 321
  • 2
  • 8
  • 1
    1) `/** @var [Type] [$variable] */` is a correct way 2) Why not simply use more appropriate solution -- type hint for actual parameter (since `$query` is passed to that function that way)? – LazyOne Mar 05 '17 at 00:32
  • absolutely for both 1 & 2, I wrote the first solution that came up in my mind. option two is the proper way :). – Leonardo Atalla Mar 05 '17 at 12:31
3

The reason this is happening is because PHPStorm doesn't know what that variable is meant to be (it has nothing to be with Laravel). As far as PHPStorm knows it's just a param for a method.

As @LazyOne suggested, you can type hint the variable e.g.

public function scopeIncomplete(Builder $query)

Then at the top of the class just add the following use statement

use Illuminate\Database\Eloquent\Builder;

Alternatively, if you're using a OS X (I'm not sure of the shortcuts for Windows or Linux) you can move the caret in the Builder reference and then hit alt enter to import the class.

Hope this helps!

Rwd
  • 34,180
  • 6
  • 64
  • 78