Test Setup Failed
Push — master ( 7450ca...e4bb3e )
by Gabriel
05:50 queued 15s
created

AbstractParser::assemble()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 9.5338

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 13
cts 16
cp 0.8125
rs 8.0555
c 0
b 0
f 0
cc 9
nc 6
nop 1
crap 9.5338
1
<?php
2
3
namespace Nip\Router\Parsers;
4
5
/**
6
 * Class AbstractParser
7
 * @package Nip\Router\Parsers
8
 */
9
abstract class AbstractParser
10
{
11
    /**
12
     * @var \Nip\Request
13
     */
14
    protected $request;
15
16
    /**
17
     * @var string
18
     */
19
    protected $uri;
20
21
    /**
22
     * @var string
23
     */
24
    protected $map;
25
26
    /**
27
     * @var array
28
     */
29
    protected $parts;
30
31
    /**
32
     * @var array
33
     */
34
    protected $params = [];
35
36
    /**
37
     * @var array
38
     */
39
    protected $variables = [];
40
41
    /**
42
     * @var array
43
     */
44
    protected $matches = [];
45
46
    /**
47
     * AbstractParser constructor.
48
     * @param bool $map
49
     * @param array $params
50
     */
51 23
    public function __construct($map = false, $params = [])
52
    {
53 23
        if ($map) {
54
            $this->setMap($map);
55 23
        } elseif ($this->map) {
56 9
            $this->parseMap();
57
        }
58
59
60 23
        if ($params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
61
            $this->setParams($params);
62
        }
63 23
        $this->init();
64 23
    }
65
66 21
    protected function parseMap()
67
    {
68 21
        $this->setParts(explode("/", trim($this->map, '/')));
69 21
    }
70
71 23
    public function init()
72
    {
73 23
    }
74
75
    /**
76
     * @param $uri
77
     * @return bool
78
     */
79 5
    public function match($uri)
80
    {
81 5
        $this->setUri($uri);
82
83 5
        return true;
84
    }
85
86
    /**
87
     * @param mixed $uri
88
     */
89 5
    public function setUri($uri)
90
    {
91 5
        $this->uri = $uri;
92 5
    }
93
94
    /**
95
     * @param array $params
96
     * @return string
97
     */
98 3
    public function assemble($params = [])
99
    {
100 3
        $return = $this->getMap();
101
102 3
        if ($params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
103 3
            foreach ($params as $key => $value) {
104 3
                if (stristr($return, ":" . $key) !== false) {
105 3
                    $return = str_replace(":" . $key, $value, $return);
106 3
                    unset($params[$key]);
107
                }
108 3
                if (array_key_exists($key, $this->params)) {
109 3
                    unset($params[$key]);
110
                }
111
            }
112 3
            if ($params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
113 3
                $return .= "?" . http_build_query($params);
114
            }
115
        }
116
117
        // set defaults
118 3
        if ($this->params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
119
            foreach ($this->params as $key => $value) {
120
                if (is_string($value)) {
121
                    $return = str_replace(":" . $key, $value, $return);
122
                }
123
            }
124
        }
125
126 3
        return $return;
127
    }
128
129
    /**
130
     * @return string
131
     */
132 6
    public function getMap()
133
    {
134 6
        return $this->map;
135
    }
136
137
    /**
138
     * @param boolean $map
139
     */
140 20
    public function setMap($map)
141
    {
142 20
        $this->map = $map;
0 ignored issues
show
Documentation Bug introduced by
The property $map was declared of type string, but $map is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
143 20
        $this->parseMap();
144 20
    }
145
146
    /**
147
     * @param $params
148
     * @return array
149
     */
150 1
    public function stripEmptyParams($params)
151
    {
152 1
        if (is_array($params)) {
153 1
            foreach ($params as $key => $param) {
154 1
                if (empty($param)) {
155
                    unset($params[$key]);
156 1
                } elseif (is_array($param)) {
157
                    $newParams = $this->stripEmptyParams($param);
158
                    if (!is_array($newParams) or count($newParams) < 1) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
159
                        unset($params[$key]);
160
                    } else {
161 1
                        $params[$key] = $newParams;
162
                    }
163
                }
164
            }
165
        }
166
167 1
        return $params;
168
    }
169
170
    /**
171
     * @return array
172
     */
173 12
    public function getParts()
174
    {
175 12
        return $this->parts;
176
    }
177
178
    /**
179
     * @param array $parts
180
     */
181 21
    public function setParts($parts)
182
    {
183 21
        $this->parts = $parts;
184 21
    }
185
186
    /**
187
     * @return array
188
     */
189 2
    public function getParams()
190
    {
191 2
        return $this->params;
192
    }
193
194
    /**
195
     * @param array $params
196
     */
197 3
    public function setParams($params = [])
198
    {
199 3
        if (count($params)) {
200 3
            foreach ($params as $key => $value) {
201 3
                $this->setParam($key, $value);
202
            }
203
        }
204 3
    }
205
206
    /**
207
     * @param $name
208
     * @param $value
209
     * @return $this
210
     */
211 6
    public function setParam($name, $value)
212
    {
213 6
        $this->params[$name] = $value;
214
215 6
        return $this;
216
    }
217
218
    /**
219
     * @param string $key
220
     * @return mixed|null
221
     */
222 2
    public function getParam($key)
223
    {
224 2
        return $this->hasParam($key) ? $this->params[$key] : null;
225
    }
226
227
    /**
228
     * @param $key
229
     * @return bool
230
     */
231 2
    public function hasParam($key)
232
    {
233 2
        return isset($this->params[$key]);
234
    }
235
236
    /**
237
     * @return array
238
     */
239
    public function getMatches()
240
    {
241
        return $this->matches;
242
    }
243
244
    /**
245
     * @return array
246
     */
247 2
    public function getVariables()
248
    {
249 2
        return $this->variables;
250
    }
251
252
    /**
253
     * @param array $variables
254
     */
255 3
    public function setVariables($variables)
256
    {
257 3
        $this->variables = $variables;
258 3
    }
259
260
//    public function setModule($module)
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
261
//    {
262
//        $this->_module = $module;
263
//    }
264
//
265
//    public function setController($controller)
266
//    {
267
//        $this->_controller = $controller;
268
//    }
269
//
270
//    public function setAction($action)
271
//    {
272
//        $this->_action = $action;
273
//    }
274
}
275