createServerRequestFromArray uses the super-global variable $_SERVER which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies
of your class. This makes your code less dependent on global state and it
becomes generally more testable:
// BadclassRouter{publicfunctiongenerate($path){return$_SERVER['HOST'].$path;}}// BetterclassRouter{private$host;publicfunction__construct($host){$this->host=$host;}publicfunctiongenerate($path){return$this->host.$path;}}classController{publicfunctionmyAction(Request$request){// Instead of$page=isset($_GET['page'])?intval($_GET['page']):1;// Better (assuming you use the Symfony2 request)$page=$request->query->get('page',1);}}
Loading history...
22
{
23
7
if (!isset($server['REQUEST_METHOD'])) {
24
throw new \InvalidArgumentException('Cannot determine HTTP method');
Both the $myVar assignment in line 1 and the $higher assignment in line 2
are dead. The first because $myVar is never used and the second because
$higher is always overwritten for every possible time line.
Loading history...
29
7
$_SERVER = $server;
30
// Until https://github.com/guzzle/psr7/pull/116 is resolved
31
7
if (!isset($_SERVER['HTTPS'])) {
32
7
$_SERVER['HTTPS'] = 'off';
33
}
34
7
$uri = ServerRequest::getUriFromGlobals();
35
36
7
return new ServerRequest($method, $uri, [], null, '1.1', $server);
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: