UrlQuery::parseQuery()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Weew\Url;
4
5
class UrlQuery implements IUrlQuery {
6
    /**
7
     * @var array
8
     */
9
    protected $query = [];
10
11
    /**
12
     * @param mixed $query
13
     */
14
    public function __construct($query = []) {
15
        $this->query = $this->parseQuery($query);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parseQuery($query) can be null. However, the property $query is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
16
    }
17
18
    /**
19
     * @param string $key
20
     * @param null $default
21
     *
22
     * @return mixed
23
     */
24
    public function get($key, $default = null) {
25
        return array_get($this->query, $key, $default);
26
    }
27
28
    /**
29
     * @param string $key
30
     * @param $value
31
     */
32
    public function set($key, $value) {
33
        array_set($this->query, $key, $value);
34
    }
35
36
    /**
37
     * @param string $key
38
     *
39
     * @return bool
40
     */
41
    public function has($key) {
42
        return array_has($this->query, $key);
43
    }
44
45
    /**
46
     * @param string $key
47
     */
48
    public function remove($key) {
49
        array_remove($this->query, $key);
50
    }
51
52
    /**
53
     * @return int
54
     */
55
    public function count() {
56
        return count($this->query);
57
    }
58
59
    /**
60
     * @param bool $encode
61
     *
62
     * @return string
63
     */
64
    public function toString($encode = false) {
65
        $query = http_build_query($this->query, '', '&', PHP_QUERY_RFC3986);
66
67
        if ( ! $encode) {
68
            return rawurldecode($query);
69
        }
70
71
        return $query;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function __toString() {
78
        return $this->toString();
79
    }
80
81
    /**
82
     * @param array $data
83
     */
84
    public function extend(array $data) {
85
        $this->query = array_extend($this->query, $data);
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function toArray() {
92
        return $this->query;
93
    }
94
95
    /**
96
     * @param string|array $query
97
     *
98
     * @return array
99
     */
100
    private function parseQuery($query) {
101
        if (is_array($query)) {
102
            return $query;
103
        }
104
105
        // "+" sign is reserved and must always be encoded,
106
        // otherwise it is interpreted as a space
107
        $queryString = str_replace('+', '%2B', $query);
108
        parse_str($queryString, $query);
109
110
        return $query;
111
    }
112
}
113