vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

PHPDoc @var popup missing description with global statements

Open bimimicah opened this issue 4 years ago • 3 comments

Describe the bug Hover popup doesn't show the PHPDoc description when the PHPDoc is next to a global statement. If it is next to an assignment, it works fine.

To Reproduce

<?php

/** @var bool $var1 description goes here */
global $var1;

/** @var bool $var2 description goes here */
$var2 = NULL;


global $var1;
global $var2;
?>

Expected behavior The description appears in both hover popups.

Screenshots image image

Platform and version Windows 10 1909 x64 Intelephense 1.6.3

bimimicah avatar Feb 25 '21 22:02 bimimicah

I think this is happening because intelephense considers global $var1 an import and $var = 1 the declaration. The phpdoc is attached to the declaration so this appears to be working as expected.

bmewburn avatar Mar 02 '21 20:03 bmewburn

Thanks for your reply. In our case, we do use global $var1 as a declaration of sorts. See the following made-up example:

<?php
/** 
 * GLOBALS.php 
 * all our globals are here.... 
 */

/** @var bool $stateIsProduction description goes here */
global $stateIsProduction;
/** @var string $locationEmployeePhotos description goes here */
global $locationEmployeePhotos;
/** @var string $locationSomethingOrOther description goes here */
global $locationSomethingOrOther;


if ($condition){
	$stateIsProduction = true;	// Yay! Production!
}else{
	$stateIsProduction = false;	// :( Just testing...
}
if ($stateIsProduction){
	$locationEmployeePhotos = "/a/path/of/sorts";
}else{
	$locationEmployeePhotos = "A:\path\of\sorts\also";
}
if ($stateIsProduction){
	$locationSomethingOrOther = "/another/path/of/sorts";
}
?>
<?php
/** 
 * page.php 
 * do some work.... 
 */
include_once("GLOBALS.php");

if (isset($locationSomethingOrOther)){
	//do some work if set....
}

?>

In the case where $stateIsProduction is false, the only place where $locationSomethingOrOther is declared is in the global statement at the top of the file. It is not assigned a value (and our code depends on it not being assigned a value, which is a perfectly valid semantic in PHP).

Of course we could add the PHPDoc to the assignments inside the if statements, but that is very messy and reduces the readability of the code.

bimimicah avatar Mar 03 '21 15:03 bimimicah

What happens if you assign a value immediately?

# GLOBALS.php

/** @var bool $stateIsProduction description goes here */
global bool $stateIsProduction = false; // Initial assignment here

if (true) {
	$stateIsProduction = true;
}

mathmul avatar May 10 '22 08:05 mathmul

We have code that depends on whether it is set or not, as I mentioned above.

bimimicah avatar Mar 31 '23 15:03 bimimicah