Completed
Push — master ( 69d02e...d89245 )
by Tobias
03:45
created

ServerRequestFactory::createServerRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nyholm\Psr7\Factory;
6
7
use Interop\Http\Factory\ServerRequestFactoryInterface;
8
use Nyholm\Psr7\ServerRequest;
9
10
/**
11
 * @author Tobias Nyholm <[email protected]>
12
 * @author Martijn van der Ven <[email protected]>
13
 */
14
class ServerRequestFactory implements ServerRequestFactoryInterface
15
{
16 16
    public function createServerRequest($method, $uri)
17
    {
18 16
        return new ServerRequest($method, $uri);
19
    }
20
21 7
    public function createServerRequestFromArray(array $server)
0 ignored issues
show
Coding Style introduced by
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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(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');
25
        }
26
        // TODO: find a MUCH better way
27 7
        $method = $server['REQUEST_METHOD'];
28 7
        $SERVER = $_SERVER;
0 ignored issues
show
Unused Code introduced by
$SERVER is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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);
37
    }
38
}
39