Search code examples
phplaravelphpstorm

PhpStorm - Cannot find declaration to go to


I try to lookup the declaration of File but PhpStorm says Cannot find declaration to go to.

enter image description here

I also tried it with netbeans, it can't find the declartion too. I also tried to lookup the alias use File;

enter image description here

I get No usage found in project files.

How does my code even know what it has to do if It can't find any declarations? This makes no sense to me.

How can I find out where File is declared?


Solution

  • How does my code even know what it has to do if It can't find any declarations?

    By using an autoloader. This is basically a function which is called whenever an unknown class is referenced, and attempts to define it, usually by including a file according to some naming convention. You will need to find how your particular framework manages this.

    Note that it's possible it's including a file from outside the directory you have set up as the "project" in your IDE. Once you've figured out where it is, you may be able to configure your IDE to know about these extra files.

    How can I find out where File is declared?

    Find a place where the class is used, and using a debugger or just "dump value and die", you can use ReflectionClass::getFilename() to find out about it:

    $r = new \ReflectionClass(File::class);
    $r->getFilename();
    

    Note that the File::class syntax (available since PHP 5.5) gives you the fully qualified name of the class, ignoring any aliasing or namespace imports.

    It's also possible for an extension (a library written in C, not PHP) to define a class. In that case, ReflectionClass::getFilename() will return false, and you'll need to use ReflectionClass::getExtensionName(), then track down the documentation for that extension.