In PhpStorm when using Request::createFromGlobals - warning says "field x not found in static"

248 Views Asked by At

This is happening in a legacy PHP application that was not built using the Symfony framework, only parts of Http-Foundation are used. So nothing is creating the Request for us, instead we create it using GLOBALS in some setup code. This was done as part of the Modernization process (following Paul M. Jones book, in Chapter 5: Replace global With Dependency Injection).

It works just fine and is much nicer than having $_GET, $_SERVER, etc all over the place... except that in PhpStorm every time a field within the Request object is accessed there's a warning:

"Field 'server' not found in static"

If I remove the PHPDoc from Request::createFromGlobals (@return static) or change it to (@return self) then PhpStorm works as desired.

Alternatively, if I create the request myself using the GLOBALS ($request = new Request($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);), like createFromGlobals() is doing, then PhpStorm is happy.

Is there a way to get around this while using Request::createFromGlobals()?

Edit: Solution, thanks @LazyOne, was to add the following typehint change:

/** @var Request $request */
$request = Request::createFromGlobals();
0

There are 0 best solutions below