Completed
Branch master (ef577c)
by richard
04:50 queued 02:31
created

Environment::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 1
1
<?php
2
3
namespace Almendra\Http\Psr\Messages;
4
5
use Almendra\Http\Collection;
6
use Almendra\Http\Psr\Interfaces\EnvironmentInterface;
7
8
/**
9
 * Represents the message environment.
10
 *
11
 * @package Almendra\Http
12
 */
13
class Environment extends Collection implements EnvironmentInterface
14
{
15
    /**
16
     * Create mock environment
17
     *
18
     * @param  array $userData Array of custom environment keys and values
19
     * @return array
20
     */
21
    public static function mock(array $userData = [])
22
    {
23
        $data = array_merge([
24
            'SERVER_PROTOCOL'      => 'HTTP/1.1',
25
            'REQUEST_METHOD'       => 'GET',
26
            'SCRIPT_NAME'          => '',
27
            'REQUEST_URI'          => '',
28
            'QUERY_STRING'         => '',
29
            'SERVER_NAME'          => 'localhost',
30
            'SERVER_PORT'          => 8000,
31
            'HTTP_HOST'            => 'localhost',
32
            'HTTP_ACCEPT'          => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
33
            'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8',
34
            'HTTP_ACCEPT_CHARSET'  => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
35
            'HTTP_USER_AGENT'      => 'Gate Framework',
36
            'REMOTE_ADDR'          => '127.0.0.1',
37
            'REQUEST_TIME'         => time(),
38
            'REQUEST_TIME_FLOAT'   => microtime(true),
39
        ], $userData);
40
41
        return $data;
42
    }
43
44
    /**
45
     * Create environment from the global variables
46
     *
47
     * @param  array $userData Array of custom environment keys and values
48
     * @return array
49
     */
50
    public static function init(array $userData = [])
0 ignored issues
show
Coding Style introduced by
init 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...
51
    {
52
        $data = array_merge([
53
            'SERVER_PROTOCOL'      => 'HTTP/1.1',
54
            'REQUEST_METHOD'       => $_SERVER['REQUEST_METHOD'],
55
            'SCRIPT_NAME'          => $_SERVER['SCRIPT_NAME'],
56
            'REQUEST_URI'          => $_SERVER['REQUEST_URI'],
57
            // 'QUERY_STRING'         => URIHelper::getQueryParams($_SERVER['REQUEST_URI']),
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
            'QUERY_STRING'         => self::getServerValue('QUERY_STRING'),
59
            'SERVER_NAME'          => $_SERVER['SERVER_NAME'],
60
            'SERVER_PORT'          => $_SERVER['SERVER_PORT'],
61
            'HTTP_HOST'            => $_SERVER['HTTP_HOST'],
62
            'HTTP_ACCEPT'          => $_SERVER['HTTP_ACCEPT'],
63
            'HTTP_ACCEPT_LANGUAGE' => self::getServerValue('HTTP_ACCEPT_LANGUAGE', 'en'),
64
            'HTTP_ACCEPT_CHARSET'  => self::getServerValue('HTTP_ACCEPT_CHARSET', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'),
65
            'HTTP_USER_AGENT'      => $_SERVER['HTTP_USER_AGENT'],
66
            'REMOTE_ADDR'          => self::getServerValue('REMOTE_ADDR'),
67
            'REQUEST_TIME'         => time(),
68
            'REQUEST_TIME_FLOAT'   => microtime(true),
69
        ], $userData);
70
71
        return $data;
72
    }
73
74
    public static function getServerValue($value, $default = '')
0 ignored issues
show
Coding Style introduced by
getServerValue 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...
75
    {
76
        if (array_key_exists($value, $_SERVER)) {
77
            return $_SERVER[$value];
78
        }
79
80
        return $default;
81
    }
82
}
83