type inference of global variables from included files not working

In our application we have a handful of global variables, which are set up in a file which is included in all calls. Not ideal, but it's rather old code. PHPStorm always understood the type of these objects, since we had an @var annotation in a central file. This stopped working a couple of weeks ago. If I had to guess I would say with 2021.1.2. Now PHPStorm treats these globals as unknown or String, so we don't have autocomplete anymore and can't navigate to the methods.

A very simple setup to show the problem:

test-include.php:

<?php

/**
* Example class
*/
class MyExample
{
/**
* Say Hello.
*
* @return string
*/
public function sayHello() : string
{
return 'Hello';

} # function sayHello()

} # class

/** @var \MyExample $example3 */
global $example3;

$example3 = new MyExample();

PHPStorm marks the @var annottation with: "@var tag specifies the type already inferred from source code " Which is not surprising in this case. In the real example the initialisation happens in another class.

Second file in the same directory:

/**
* File that includes and uses the globals
*/

require_once __DIR__ . '/test_include.inc.php';

global $example3;

$example3->sayHello();

There is no code-completion for the call on "$example3"'s methods. If I write the method name manually, place the cursor there and press F3, PHPStorm doesn't know where to go.

My setup:

PhpStorm 2021.1.3
Build #PS-211.7442.50, built on June 2, 2021
Runtime version: 11.0.11+9-b1341.57 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows Server 2016 10.0
GC: ParNew, ConcurrentMarkSweep
Memory: 1979M
Cores: 32

Any idea how to make this work again would be appreciated! We're slowly getting rid of the global variables, but while we do this we need auto-complete and don't need all kinds of false warnings like "Method 'abcdef' not found in string".

 

3
3 comments

Thank you for a good report! I think it's a bug that has to be fixed on our side, I don't think you can fix it by any kind of configuration.
Please vote for the report to get notified on any progres

0

Thanks for the confirmation! Should I put this in the bug tracker or will you do this?

0

I actually did that, but somehow I forgot to include the link in my previous comment, sorry. Here it is:

https://youtrack.jetbrains.com/issue/WI-61116

0

Please sign in to leave a comment.