0

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();
Sophia
  • 1
  • 1
  • 1
    So ... where do you use that `Request::createFromGlobals()`? Are you assigning to some variable (`$request` perhaps)? If so -- have you tried overriding typehint with local @var (e.g. `/** @var Request $request */` just before the line where `$request` is created)? – LazyOne Dec 01 '17 at 17:36
  • @LazyOne Request::createFromGlobals() was being used in the setup script and being assigned to $request, then being picked up downstream in many locations. (As mentioned, it's a legacy php application going through the modernization process) -- I just tried your change and it solved my problem. I hadn't realized that such a simple solution existed. Thank you! – Sophia Dec 01 '17 at 17:57

0 Answers0