path not found after chdir

Is not unusual on PHP to do something like this:

chdir(dirname(__DIR__));
require 'vendor/autoload.php';

Where the first `chdir` will chdir to the root of the project, so all future includes and requires are done from there.

Logically, this confuses PHPStorm's static analysis, and it'll complain that "vendor/autoload.php" is not found.

Is there a way, file-wise or project wise to tell PHPStorm of where should it start looking for files for this kind of expressions, so we can have files with less warnings?

Not a biggie, and I'm sure I'm not the first one to see this, but couldn't find any relevant setting or post anywhere.

Thanks and regards.

1
5 comments

Hi there,

>Is there a way, file-wise or project wise to tell PHPStorm of where should it start looking for files for this kind of expressions, so we can have files with less warnings?

No. IDE does not take any chdir() calls (or alike) into consideration. Plus, the path you pass there might be assembled dynamically .. so it become even less predictable.

Why not use more deterministic require dirname(__DIR__) . '/vendor/autoload.php'; instead? It will be an absolute path so no guessing at all (in IDE during static analysis and during actual execution/at runtime)? Instead of dirname(__DIR__)  (that could be used few times in one script) custom constant can be used (e.g. defined once in entry point script, if you have one).

Other than that -- IDE checks path relative to the current script, project/module root (in case if you have more than one Content Root defined). I'm not 100%  sure but I don't think that include/require paths are checked relative Resource Roots or any other special "marks" that IDE supports (e.g. Source/Test).

P.S. If you wish you may just disable that particular inspection.

0

> No. IDE does not take any chdir() calls (or alike) into consideration. Plus, the path you pass there might be assembled dynamically .. so it become even less predictable.

I wasn't expecting the IDE to take chdir() calls into account. But a way to specify "eval my file lookups starting from THIS path instead of from the path this file resides at".

That code snippet comes straight from a ZF newly created project. If I change it I'd have to change all the other places where requires or includes are being used, and I'd be breaking expectations for other possible modules. The leading comment says:

/**
* This makes our life easier when dealing with paths. Everything is relative
* to the application root now.
*/

And I don't disagree with this. If the IDE checked paths relative to the project root it would work for me, but as far as I can see, that is not the case (unless I'm missing a setting to enable this, which is more or less what I'm looking for).

If not, as you say, disabling that inspection would be the way to go. It would be a shame, though.

Thanks and regards

0

> "eval my file lookups starting from THIS path instead of from the path this file resides at".

Is there a reason $_SERVER['DOCUMENT_ROOT'] prepended doesn't do the trick?

0

It could do the trick, but it would be a "different" trick.

I could change it every time I encounter a require, but the trick used here means that is no necessary.

What I wanted to know if there was a way to inform the IDE of this situation, so that `require file.php` would work directly, and the IDE would be able to find `file.php` residing in the document root.

0
Avatar
Permanently deleted user

Hello,

Looks like we have this request reported as https://youtrack.jetbrains.com/issue/WI-9747.

0

Please sign in to leave a comment.