Dynamic   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 69
c 1
b 0
f 0
dl 0
loc 175
ccs 75
cts 78
cp 0.9615
rs 9.52
wmc 36

11 Methods

Rating   Name   Duplication   Size   Complexity  
B getVariableFromPart() 0 29 9
A postMatch() 0 3 1
A parseMapForVariables() 0 12 5
A parseMap() 0 4 1
A parseUriParts() 0 3 1
A preMatch() 0 9 2
A parseParams() 0 17 6
A setUriParts() 0 3 1
A getUriParts() 0 3 1
A getVariableParts() 0 13 4
A match() 0 20 5
1
<?php
2
3
namespace Nip\Router\Parsers;
4
5
/**
6
 * Class Dynamic
7
 * @package Nip\Router\Parsers
8
 */
9
class Dynamic extends AbstractParser
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $uriParts = [];
15
16 6
    public function parseMap()
17
    {
18 6
        parent::parseMap();
19 6
        $this->parseMapForVariables();
20 6
    }
21
22 6
    protected function parseMapForVariables()
23
    {
24 6
        $parts = $this->getParts();
25 6
        foreach ($parts as &$part) {
26 6
            $variablesCount = substr_count($part, ":");
27 6
            if ($variablesCount >= 1) {
28 6
                if ($variablesCount == 1 && strpos($part, ':') === 0) {
29 6
                    $this->variables[] = str_replace(":", "", $part);
30
                } else {
31 3
                    $variables = $this->getVariableFromPart($part);
32 3
                    $variables = array_merge($this->variables, $variables);
33 3
                    $this->setVariables($variables);
34
                }
35
            }
36
        }
37 6
    }
38
39
    /**
40
     * @param $part
41
     * @return array
42
     */
43 4
    public function getVariableFromPart($part)
44
    {
45 4
        $len = strlen($part);
46 4
        $variables = [];
47 4
        $variable = false;
48 4
        $letters = array_merge(range('A', 'Z'), range('a', 'z'));
49 4
        for ($i = 0; $i < $len; $i++) {
50 4
            $char = $part[$i];
51 4
            if ($char == ':') {
52 4
                if ($variable) {
53 1
                    $variables[] = $variable;
54
                }
55 4
                $variable = '';
56
            } else {
57 4
                $isLetter = in_array($char, $letters);
58 4
                $isAllowed = in_array($char, ['_']);
59 4
                if (($isLetter || $isAllowed) && $variable !== false) {
60 4
                    $variable .= $char;
61 4
                } elseif ($variable !== false) {
62 1
                    $variables[] = $variable;
63 1
                    $variable = false;
64
                }
65
            }
66
        }
67 4
        if (!empty($variable)) {
68 4
            $variables[] = $variable;
69
        }
70
71 4
        return $variables;
72
    }
73
74
    /**
75
     * @param $uri
76
     * @return bool
77
     */
78 3
    public function match($uri)
79
    {
80 3
        $return = parent::match($uri);
81
82 3
        if ($return) {
0 ignored issues
show
introduced by
The condition $return is always true.
Loading history...
83
//            if ($this->uri[strlen($this->uri) - 1] == '/') {
84
//                $this->uri = substr($this->uri, 0, -1);
85
//            }
86 3
            $this->parseUriParts($uri);
87 3
            if ($this->getVariableParts()) {
88 3
                if ($this->preMatch() === true) {
89 3
                    $this->parseParams();
90 3
                    if ($this->postMatch() == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
introduced by
The condition $this->postMatch() == true is always true.
Loading history...
91 3
                        return true;
92
                    }
93
                }
94
            }
95
        }
96
97 3
        return false;
98
    }
99
100
    /**
101
     * @param $uri
102
     */
103 3
    public function parseUriParts($uri)
104
    {
105 3
        $this->uriParts = explode("/", trim($uri, '/'));
106 3
    }
107
108
    /**
109
     * @return bool
110
     */
111 3
    public function getVariableParts()
112
    {
113 3
        foreach ($this->parts as $key => $part) {
114 3
            if (strpos($part, ':') !== false) {
115 3
                break;
116
            }
117 2
            if ($this->uriParts[$key] != $part) {
118 1
                return false;
119
            }
120
//            unset($this->uriParts[$key]);
121
        }
122
123 3
        return true;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129 1
    protected function preMatch()
130
    {
131 1
        $mapCount = count($this->getParts());
132 1
        $uriCount = count($this->getUriParts());
133 1
        if ($mapCount != $uriCount) {
134 1
            return false;
135
        }
136
137 1
        return true;
138
    }
139
140
    /**
141
     * @return array
142
     */
143 3
    public function getUriParts()
144
    {
145 3
        return $this->uriParts;
146
    }
147
148
    /**
149
     * @param array $uriParts
150
     */
151
    public function setUriParts($uriParts)
152
    {
153
        $this->uriParts = $uriParts;
154
    }
155
156
    /**
157
     * @return bool
158
     */
159 3
    protected function parseParams()
160
    {
161 3
        $uriParts = $this->getUriParts();
162 3
        foreach ($this->parts as $key => $part) {
163 3
            if (strstr($part, ":") === false) {
164
                // part is static - no named params
165 2
                if (!isset($uriParts[$key]) || $uriParts[$key] != $part) {
166
                    // corresponding part in URI does not match
167 2
                    return false;
168
                }
169
            } else {
170 3
                $var = str_replace(":", "", $part);
171 3
                $this->setParam($var, isset($uriParts[$key]) ? $uriParts[$key] : null);
172
            }
173
        }
174
175 3
        return true;
176
    }
177
178
    /**
179
     * @return bool
180
     */
181 3
    protected function postMatch()
182
    {
183 3
        return true;
184
    }
185
}
186