Define PHP global variable in metadata

Good morning everyone,

Probably a duplicate of this https://intellij-support.jetbrains.com/hc/en-us/community/posts/206356649-Define-common-global-variables-types-as-a-project-setting, but the post is quite old and maybe something changed in ten and more years.

Is there a way to specify a list of files to always check for variable definitions? in altarnative, is there a way to define some list of global variable? I know about the "search outside of the current file" option, but is not what i needed, i just need to consider a short list of variable as always defined

Maybe it could be a good idea to have a way to define them inside a metadata file, so that is valid for a single project

0

Hi,

Sadly, there is no such feature or any whitelists available.
Do you mind me asking why the "Search outside of the current file" does not work for you? In theory, you may enable this option on specific files only (f.e. templates) via scopes if you do not need it everywhere in your project.

0

Hi, thanks for answering.

Even if the option does suppress the Undefined symbol warning, fails to detect the type of the symbol for Code Completion or where the symbol is actually located (for example, to go to it using the "Go to > declaration"). This, for example, can happen in the following situation:

to_import.php
<?php $now = new DateTime(); ?>

another.php
<?php $now = time(); ?>

problem.php
<?php
require "to_import.php";
$now->format("Y-m-d");
?>

In this situation, Code completion won't suggest the method "format", since is unable to detect that $now is a DateTime instance. This will happen even if there isn't an ambiguos symbol name (as such, if another.php doesn't exists).

This could be avoided without writing the PHPDoc block in every single file with a single line of metadata written like this (for example, the actual format is up to you)

{var: $now, type: "DateTime"}

or

{always_check_for_globals: ["to_import.php"]}

0

Hi Francesco,

Thank you for your reply.

From my experience of reading different feedback reports, I would not say that it is a wide-spread request but you may always submit a corresponding ticket on YouTrack to focus the public discussion and channel votes there:
https://youtrack.jetbrains.com/newIssue?project=WI 

As for the completion part, I was not able to reproduce it:

Do I miss something?

0

Hi there, maybe I'm the one that is missing something: here the situation where I can reproduce this:

$current_tournament is defined in "initialization.php", required inside "function.php" and defined as following

Maybe is because the definition happens in a if/else construct, but it seems weird to me that the PHPDoc block doesn't help in defining the variable type.

0

Hi,

I guess that it happens because of the "if/else" construct as you suspect it as, in general, this should work even when "Search for definitions outside" is not enabled (look at the undefined variable highlight):

Out of curiosity, what happens if you use this variable in the same "function.php" file?

0

No result, as you can see. If not using metadata, would be nice to make PHPStorm take into consideration PHPDoc blocks in included files, to avoid replicating the same PHPDoc block in several files

0

Hi Francesco,

I have tried to play around with a similar scenario and managed to reproduce something alike:

I understand that it is a specific case and a general idea of your initial request was different but I submitted it as a bug report:
https://youtrack.jetbrains.com/issue/WI-71437/Cannot-find-method-if-class-is-instantiated-in-if-cycle-in-another-file 

0

Could PhpDoc @global be somehow used to specify the default place to look for a specific global variable? 
When there is only one variable declaration with given name – adding `global` enables PhpStorm to identify the declaration correctly, but when a variable with the same name is declared more than once in different files, PhpStorm seems to randomly pick one (probably the first occurrence) and go to that one  when I click “Go To > Declaration or usages”

0

请先登录再写评论。