Constants - Unresolved include.

Hello!
Problem is with highlight included path with defined constants (Path 'file.php' not found more...)
I read that PHPStorm can analyze constants value, and should understand full path with constant using.
In my project there's file const.php, where i keep all my constants, that way - define('INCLUDE_DIR', $_SERVER['DOCUMENT_ROOT'] . '/_include/');
then i require this file in other, and try to include path using that constant - require_once INCLUDE_DIR . 'class.MyClass.php';
PHP interpreter understand this path, and script work normally, but PHPStorm always show error about incorrect path.
Generally is it real to let PHPStorm understanding constants, and not highlight including path?

And if this is not possible, is it real to set one Suppress statement for multi row (/** @noinspection PhpIncludeInspection */)
not have to do something like this: (screenshot in attachment)



Attachment(s):
1.PNG
4 comments
Comment actions Permalink

Hi there,

Generally is it real to let PHPStorm understanding constants, and not highlight including path?

Yes. Works for me (although I have like 2-4 includes per project -- all classes are loaded via autoloader).

is it real to set one Suppress statement for multi row

IMO: just disable that inspection altogether. I personally do not see much benefit in it.

define('INCLUDE_DIR', $_SERVER['DOCUMENT_ROOT'] . '/_include/');

1) What $_SERVER['DOCUMENT_ROOT'] points to -- is it the same as project root or it is a subfolder in a project?

2) IDE may not properly resolve such constant because of presence of $_SERVER['DOCUMENT_ROOT'] in it.

3) Do you have single entry point into your app (where all requests are router trough central index.php (or alike) file) or is it every script is a separate entry point?

0
Comment actions Permalink

Hi, Andriy! Thanks for answer.

IMO: just disable that inspection altogether. I personally do not see much benefit in it.

But can i disable only for one file? Or it only can be disabled for all current project?

1) What $_SERVER['DOCUMENT_ROOT'] points to -- is it the same as project root or it is a subfolder in a project?

Yes, $_SERVER['DOCUMENT_ROOT'] points to my project root directory. For example i have all files index.php, img/, css/, _include/ in my www directory (same as httpdocs on server) $_SERVER['DOCUMENT_ROOT'] points there (/some_path/project.name/www/)

2) IDE may not properly resolve such constant because of presence of $_SERVER['DOCUMENT_ROOT'] in it.

Yeah, you're right. I just tryed to define only one constant with value $_SERVER['DOCUMENT_ROOT'] and IDE failed to recognized path, but with default strings it works, thanks. So there's no way to let IDE understand $_SERVER['DOCUMENT_ROOT'] path?

3) Do you have single entry point into your app (where all requests are router trough central index.php (or alike) file) or is it every script is a separate entry point?

Yes, i have public_setup.php (which require const.php) file in my _include/ directory, and all script proccess goes throught index.php file, which require that public_setup.php. But somewhere i stiil needed to individual require files, and since i use index.php which include public_setup.php with constants, i use constants for individual include.

I have small example in attachments, where i dont use $_SERVER['DOCUMENT_ROOT'] in constant definition, but warning still displaying. Can you please check it?



Attachment(s):
index.PNG
handler_edit.PNG
public_setup.PNG
base.PNG
structure.PNG
0
Comment actions Permalink

When IDE resolves paths .. it checks

  • absolute path
  • relative to the current file
  • relative to the project root
  • relative to the content root (typical PhpStorm project has only single content root, which is the same as project root)


During script execution by PHP, it may apply different logic when searching for included files ("include_path" option etc) -- that's why it's different.

Yes, $_SERVER['DOCUMENT_ROOT'] points to my project root directory. For example i have all files index.php, img/, css/, _include/ in my www directory (same as httpdocs on server) $_SERVER['DOCUMENT_ROOT'] points there (/some_path/project.name/www/)

At the moment (v8.0.3 and never) PhpStorm resolves $_SERVER['DOCUMENT_ROOT'] to the PhpStorm's project root (i.e. "/some/path/project_name/") and takes local path only (does not care about Deployment at all). If your actual website root is located in a subfolder (e.g. "/some/path/project_name/www") then it will NOT point to the "www" -- https://youtrack.jetbrains.com/issue/WI-3321#comment=27-890003

I'm not 100% sure.. but based on the paths you have provided your DOCUMENT_ROOT is not the same as PhpStorm's project root. Although I may just interpret your info in a wrong way.

But can i disable only for one file? Or it only can be disabled for all current project?

How did you supress them in first place? Via Alt+Enter (light builb icon menu)? If yes -- there is an option -- "suppress for file" (as opposed to the "suppress for line" that you have used) -- see if this will help (not sure though .. as it may not yet be implemented).

If this does not help .. then you can disable inspection for a specific file/folder by:

  1. Create custom scope (Settings (Preferences on Mac) | Appearance & Behaviour | Scopes) and include only files/folders where such inspection should be disabled
  2. Go to Inspections (Settings | Editor | Inspections) and locate that particular inspection
  3. Add rule for that scope from #1 and disable inspection for it.

I have small example in attachments, where i dont use $_SERVER['DOCUMENT_ROOT'] in constant definition, but warning still displaying. Can you please check it?

Sure. But it will be much better if you zip whole project and attach it here as it's hard to say what is wrong based on screenshots only.

So far I may say that  HANDLERS most likely resolved by PhpStorm as "project_root/_const/_include/handlers/" and not "project_root/_include/handlers/" as you would expect.

This is what I use:

project_root/www/index.php

<?php

...

// defining global constants
define('DIR_WEB', __DIR__); // points to project_root/www (website folder)
define('DIR_ROOT', dirname(__DIR__)); // points to project root
define('DIR_VAR', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'var'); // points to project_root/var (temp/writable folder)
define('DIR_APP', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'app'); // points to project_root/app (actual app: classes/configs/templates/controllers/etc)

...

Since this file is an entry point .. these constants get defined in a same way/value in any URL php code handles.

Then if I need include/require, I just use:

// register class loaders
$loader = require_once DIR_ROOT . '/vendor/autoload.php';
....
$app->init(
    include (DIR_APP . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'app.php')
);

0
Comment actions Permalink

I'm not 100% sure.. but based on the paths you have provided your DOCUMENT_ROOT is not the same as PhpStorm's project root. Although I may just interpret your info in a wrong way.

You're absolutely right. I've just updated my PHPStorm from version 8.0.2 -> 8.0.3 and it started to recognize $_SERVER['DOCUMENT_ROOT'] in include path. But they point not to my www/ directory as PHP interpriter, but in root directory of my www/ folder (some_path/project.name/)

How did you supress them in first place? Via Alt+Enter (light builb icon menu)? If yes -- there is an option -- "suppress for file" (as opposed to the "suppress for line" that you have used) -- see if this will help (not sure though .. as it may not yet be implemented).

Yeap, i've tryed that but that not working for me, nothing happens when i press suppress for file. But if that need will arises, i'll use your second advice with scopes, thanks!

This is what I use:

project_root/www/index.php

<?php

...

// defining global constants
define('DIR_WEB', __DIR__); // points to project_root/www (website folder)
define('DIR_ROOT', dirname(__DIR__)); // points to project root
define('DIR_VAR', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'var'); // points to project_root/var (temp/writable folder)
define('DIR_APP', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'app'); // points to project_root/app (actual app: classes/configs/templates/controllers/etc)

...

Wow, it works perfect for me!
define('DIR_ROOT', dirname(__DIR__)); correctly recognize path to project root from file www/_const/base.php, and then perfectly understand this include constant autocomplection in any project file. Now it's important to keep the folder with this constant in correct directory.)
I replaced my BASE_DIR constant value from  $_SERVER['DOCUMENT_ROOT'] . '/'  to dirname(__DIR__) . '/' and now there's no any problem with include warnings(Path nor found....)
Many thanks to you Andriy! I have no more questions and misunderstandings. You're best! :D

P.S If still necessary i can attach that example files from screenshot, but obviously this is not necessary, you explain everything  clearly and correctly. Thanks!

0

Please sign in to leave a comment.