ServerRequest::getQueryParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Almendra\Http\Psr\Messages;
4
5
use Psr\Http\Message\ServerRequestInterface as RequestInterface;
6
use Almendra\Http\Psr\Messages\Response;
7
8
use Almendra\Http\Helpers\URI as URIHelper;
9
use Almendra\Http\Server;
10
11
class ServerRequest extends Response implements RequestInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: getAttribute, getAttributes, getMethod, getParsedBody, getRequestTarget, getUploadedFiles, getUri, withAttribute, withMethod, withParsedBody, withQueryParams, withRequestTarget, withUploadedFiles, withUri, withoutAttribute
Loading history...
12
{
13
    protected $_cookies = [];
14
    protected $_serverParams = [];
15
16
    // protocol ver
17
    // headers
18
    // body
19
20
21
    public function __construct($cookies = null, $serverParams = null)
0 ignored issues
show
Unused Code introduced by
The parameter $serverParams is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
    {
23
        if (isset($cookies) && null !== $cookies) {
24
            $this -> setCookies($cookies);
25
        } else {
26
            $this -> setCookies($this -> getCookieParams());
27
        }
28
    }
29
    
30
    /**
31
     * Retrieve server parameters.
32
     *
33
     * Retrieves data related to the incoming request environment,
34
     * typically derived from PHP's $_SERVER superglobal. The data IS NOT
35
     * REQUIRED to originate from $_SERVER.
36
     *
37
     * @return array
38
     */
39
    public function getServerParams()
0 ignored issues
show
Coding Style introduced by
getServerParams 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...
40
    {
41
        if (isset($_SERVER) && null !== $_SERVER) {
42
            $this -> _serverParams = $_SERVER;
43
        }
44
45
        return $this -> _serverParams; // attempt to retrieve them
46
    }
47
48
49
    /**
50
     * Retrieve cookies.
51
     *
52
     * Retrieves cookies sent by the client to the server.
53
     *
54
     * The data MUST be compatible with the structure of the $_COOKIE
55
     * superglobal.
56
     *
57
     * @return array
58
     */
59
    public function getCookieParams()
60
    {
61
        return $this -> _cookies;
62
    }
63
64
    public function withCookieParams(array $cookies)
65
    {
66
        $clone = clone $this;
67
        $clone -> setCookies($cookies);
68
69
        return $clone;
70
    }
71
72
    public function setCookies(array $cookies)
73
    {
74
        foreach ($cookies as $name => $value) {
75
            $this -> setCookie($name, $value);
76
        }
77
    }
78
79
    public function setCookie($name, $value)
80
    {
81
        $this -> _cookies[$name] = $value;
82
    }
83
84
    /**
85
     * Retrieve query string arguments.
86
     *
87
     * Retrieves the deserialized query string arguments, if any.
88
     *
89
     * Note: the query params might not be in sync with the URI or server
90
     * params. If you need to ensure you are only getting the original
91
     * values, you may need to parse the query string from `getUri()->getQuery()`
92
     * or from the `QUERY_STRING` server param.
93
     *
94
     * @return array
95
     */
96
    public function getQueryParams()
97
    {
98
        return URIHelper::getQueryParams($this -> getUri(), false);
0 ignored issues
show
Bug Compatibility introduced by
The expression \Almendra\Http\Helpers\U...this->getUri(), false); of type array|string adds the type string to the return on line 98 which is incompatible with the return type declared by the interface Psr\Http\Message\ServerR...terface::getQueryParams of type array.
Loading history...
99
    }
100
}
101