Total Complexity | 53 |
Total Lines | 336 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like Segments often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Segments, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Segments extends ArrayIterator |
||
28 | { |
||
29 | /** |
||
30 | * Segments::__construct |
||
31 | * |
||
32 | * @param string|null $segments |
||
33 | * |
||
34 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
35 | */ |
||
36 | public function __construct($segments = null) |
||
37 | { |
||
38 | parent::__construct([]); |
||
39 | |||
40 | if (is_null($segments)) { |
||
41 | if (kernel()->services->has('config')) { |
||
42 | if (config()->offsetExists('uri')) { |
||
|
|||
43 | $protocol = strtoupper(config('uri')->offsetGet('protocol')); |
||
44 | } |
||
45 | } |
||
46 | |||
47 | empty($protocol) && $protocol = 'REQUEST_URI'; |
||
48 | |||
49 | switch ($protocol) { |
||
50 | case 'AUTO': |
||
51 | case 'REQUEST_URI': |
||
52 | $segments = $this->parseRequestUri(); |
||
53 | break; |
||
54 | case 'QUERY_STRING': |
||
55 | $segments = $this->parseQueryString(); |
||
56 | break; |
||
57 | case 'PATH_INFO': |
||
58 | default: |
||
59 | $segments = isset($_SERVER[ $protocol ]) |
||
60 | ? $_SERVER[ $protocol ] |
||
61 | : $this->parseRequestUri(); |
||
62 | break; |
||
63 | } |
||
64 | |||
65 | } elseif (is_array($segments)) { |
||
66 | $segments = implode('/', $segments); |
||
67 | } |
||
68 | |||
69 | $segments = str_replace(['\\', '_'], ['/', '-'], $segments); |
||
70 | $segments = trim(remove_invisible_characters($segments, false), '/'); |
||
71 | |||
72 | $this->setParts(explode('/', $segments)); |
||
73 | } |
||
74 | |||
75 | // ------------------------------------------------------------------------ |
||
76 | |||
77 | /** |
||
78 | * Segments::parseRequestUri |
||
79 | * |
||
80 | * Parse REQUEST_URI |
||
81 | * |
||
82 | * Will parse REQUEST_URI and automatically detect the URI from it, |
||
83 | * while fixing the query string if necessary. |
||
84 | * |
||
85 | * @access protected |
||
86 | * @return string |
||
87 | */ |
||
88 | protected function parseRequestUri() |
||
89 | { |
||
90 | if ( ! isset($_SERVER[ 'REQUEST_URI' ], $_SERVER[ 'SCRIPT_NAME' ])) { |
||
91 | return ''; |
||
92 | } |
||
93 | |||
94 | $uri = parse_url($_SERVER[ 'REQUEST_URI' ]); |
||
95 | $query = isset($uri[ 'query' ]) |
||
96 | ? $uri[ 'query' ] |
||
97 | : ''; |
||
98 | $uri = isset($uri[ 'path' ]) |
||
99 | ? $uri[ 'path' ] |
||
100 | : ''; |
||
101 | |||
102 | if (isset($_SERVER[ 'SCRIPT_NAME' ][ 0 ])) { |
||
103 | if (strpos($uri, $_SERVER[ 'SCRIPT_NAME' ]) === 0) { |
||
104 | $uri = (string)substr($uri, strlen($_SERVER[ 'SCRIPT_NAME' ])); |
||
105 | } elseif (strpos($uri, dirname($_SERVER[ 'SCRIPT_NAME' ])) === 0) { |
||
106 | $uri = (string)substr($uri, strlen(dirname($_SERVER[ 'SCRIPT_NAME' ]))); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct |
||
111 | // URI is found, and also fixes the QUERY_STRING server var and $_GET array. |
||
112 | if (trim($uri, '/') === '' AND strncmp($query, '/', 1) === 0) { |
||
113 | $query = explode('?', $query, 2); |
||
114 | $uri = $query[ 0 ]; |
||
115 | |||
116 | $_SERVER[ 'QUERY_STRING' ] = isset($query[ 1 ]) |
||
117 | ? $query[ 1 ] |
||
118 | : ''; |
||
119 | } else { |
||
120 | $_SERVER[ 'QUERY_STRING' ] = $query; |
||
121 | } |
||
122 | |||
123 | if (isset($_GET[ 'SEGMENTS_STRING' ])) { |
||
124 | $uri = $_GET[ 'SEGMENTS_STRING' ]; |
||
125 | unset($_GET[ 'SEGMENTS_STRING' ]); |
||
126 | |||
127 | $_SERVER[ 'QUERY_STRING' ] = str_replace([ |
||
128 | 'SEGMENTS_STRING=' . $uri . '&', |
||
129 | 'SEGMENTS_STRING=' . $uri, |
||
130 | ], '', $_SERVER[ 'QUERY_STRING' ]); |
||
131 | } |
||
132 | |||
133 | parse_str($_SERVER[ 'QUERY_STRING' ], $_GET); |
||
134 | |||
135 | if ($uri === '/' || $uri === '') { |
||
136 | return '/'; |
||
137 | } |
||
138 | |||
139 | return $uri; |
||
140 | } |
||
141 | |||
142 | // ------------------------------------------------------------------------ |
||
143 | |||
144 | /** |
||
145 | * Segments::parseQueryString |
||
146 | * |
||
147 | * Parse QUERY_STRING |
||
148 | * |
||
149 | * Will parse QUERY_STRING and automatically detect the URI from it. |
||
150 | * |
||
151 | * @access protected |
||
152 | * @return string |
||
153 | */ |
||
154 | protected function parseQueryString() |
||
155 | { |
||
156 | $uri = isset($_SERVER[ 'QUERY_STRING' ]) |
||
157 | ? $_SERVER[ 'QUERY_STRING' ] |
||
158 | : @getenv('QUERY_STRING'); |
||
159 | |||
160 | if (trim($uri, '/') === '') { |
||
161 | return ''; |
||
162 | } elseif (strncmp($uri, '/', 1) === 0) { |
||
163 | $uri = explode('?', $uri, 2); |
||
164 | $_SERVER[ 'QUERY_STRING' ] = isset($uri[ 1 ]) |
||
165 | ? $uri[ 1 ] |
||
166 | : ''; |
||
167 | $uri = rawurldecode($uri[ 0 ]); |
||
168 | } |
||
169 | |||
170 | parse_str($_SERVER[ 'QUERY_STRING' ], $_GET); |
||
171 | |||
172 | return $uri; |
||
173 | } |
||
174 | |||
175 | // -------------------------------------------------------------------- |
||
176 | |||
177 | /** |
||
178 | * Segments::addString |
||
179 | * |
||
180 | * @param string $string |
||
181 | * |
||
182 | * @return \O2System\Kernel\Http\Message\Uri\Segments |
||
183 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
184 | */ |
||
185 | public function addString($string) |
||
186 | { |
||
187 | $string = $this->__toString() . '/' . trim($string, '/'); |
||
188 | |||
189 | return $this->withString($string); |
||
190 | } |
||
191 | |||
192 | // ------------------------------------------------------------------------ |
||
193 | |||
194 | /** |
||
195 | * Segments::withString |
||
196 | * |
||
197 | * @param string $string |
||
198 | * |
||
199 | * @return \O2System\Kernel\Http\Message\Uri\Segments |
||
200 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
201 | */ |
||
202 | public function withString($string) |
||
207 | } |
||
208 | |||
209 | // ------------------------------------------------------------------------ |
||
210 | |||
211 | /** |
||
212 | * Segments::withParts |
||
213 | * |
||
214 | * @param array $parts |
||
215 | * |
||
216 | * @return \O2System\Kernel\Http\Message\Uri\Segments |
||
217 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
218 | */ |
||
219 | public function withParts(array $parts) |
||
220 | { |
||
221 | $uri = clone $this; |
||
222 | $uri->setParts($parts); |
||
223 | |||
224 | return $uri; |
||
225 | } |
||
226 | |||
227 | // ------------------------------------------------------------------------ |
||
228 | |||
229 | /** |
||
230 | * Segments::addParts |
||
231 | * |
||
232 | * @param array $parts |
||
233 | * |
||
234 | * @return \O2System\Kernel\Http\Message\Uri\Segments |
||
235 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
236 | */ |
||
237 | public function addParts(array $parts) |
||
238 | { |
||
239 | return $this->withParts($parts); |
||
240 | } |
||
241 | |||
242 | // ------------------------------------------------------------------------ |
||
243 | |||
244 | /** |
||
245 | * Segments::getPart |
||
246 | * |
||
247 | * Get Segment |
||
248 | * |
||
249 | * @param int $index (n) of Uri Segments |
||
250 | * |
||
251 | * @return mixed |
||
252 | */ |
||
253 | public function getPart($index) |
||
254 | { |
||
255 | if($this->offsetExists($index)) { |
||
256 | return $this->offsetGet($index); |
||
257 | } |
||
258 | |||
259 | return false; |
||
260 | } |
||
261 | |||
262 | // ------------------------------------------------------------------------ |
||
263 | |||
264 | /** |
||
265 | * Segments::setParts |
||
266 | * |
||
267 | * @param array $parts |
||
268 | * |
||
269 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
270 | */ |
||
271 | protected function setParts(array $parts) |
||
301 | } |
||
302 | } |
||
303 | |||
304 | // ------------------------------------------------------------------------ |
||
305 | |||
306 | /** |
||
307 | * Segments::__toString |
||
308 | * |
||
309 | * @return string |
||
310 | */ |
||
311 | public function __toString() |
||
312 | { |
||
313 | if ($this->count()) { |
||
314 | return implode('/', $this->getArrayCopy()); |
||
315 | } |
||
316 | |||
317 | return ''; |
||
318 | } |
||
319 | |||
320 | // ------------------------------------------------------------------------ |
||
321 | |||
322 | /** |
||
323 | * Segments::filterPart |
||
324 | * |
||
325 | * Filters segments for malicious characters. |
||
326 | * |
||
327 | * @param string $string URI String |
||
328 | * |
||
329 | * @return mixed |
||
330 | * @throws RuntimeException |
||
331 | */ |
||
332 | protected function filterPart($string) |
||
363 | } |
||
364 | } |