3

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?

9
  • 2
    your autoloader manages that, need to look up how it works for your project. Commented Mar 6, 2017 at 14:37
  • 2
    If it is laravels' facade, try Laravel IDE Helper blog.jetbrains.com/phpstorm/2015/01/… Commented Mar 6, 2017 at 14:44
  • Load all files from the server to your local workspace (like you have open all files in phpstorm), after that the declaration should be found (Maybe refresh index in phpstorm). Commented Mar 6, 2017 at 14:44
  • 1
    @JustOnUnderMillions If the current file is in a namespace, unprefixed classes will be looked up in the current namespace, not the global namespace. use ClassName will import from the global namespace. Compare 3v4l.org/m2tTa vs 3v4l.org/HPIeC Commented Mar 6, 2017 at 14:54
  • 1
    @IMSoP OK, got it. But then File is NOT from/under Illuminate? Wired. Like the doc page for that laravel.com/api/5.4/index.html can not find File anywhere there. Commented Mar 6, 2017 at 14:56

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hmm, I get Class 'App\Http\Controllers\ReflectionClass' not found
@EdwardBlack Ah, sorry, \ReflectionClass, to avoid the incorrect namespace lookup
Does someone know if there is a way how I can teach my IDE where to find these declarations?
@EdwardBlack Try checking the link in 2nd comment (for actual question) again -- barryvdh/laravel-ide-helper does just that -- translates Laravel's magic into more understandable for IDE form.
1

Laravel is quite "opinionated" in the way they use facades.

Apart from the PHPStorm gudelines how to deal with it, I find artisan tinker a simplest IDE-independent way to get familiar with new codebase.

The command provides a REPL shell, so if you are curious of where the File is actually defined, just invoke it, to get some information from the error message:

>>> File::delete()
PHP warning:  Missing argument 1 for Illuminate\Filesystem\Filesystem::delete(), called in /path/to/project/app/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 213 and defined in /path/to/project/app/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php on line 118

Comments

0

PHPStrom scans all files in Project Root folder. Add an external library (framework) you use to Project Root folder. Maybe you should instal dependecies via composer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.