Passed
Push — master ( f815f0...fc5faa )
by Kirill
05:47 queued 11s
created
src/Router/src/UriHandler.php 1 patch
Spacing   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -32,7 +32,7 @@  discard block
 block discarded – undo
32 32
         'integer' => '\d+',
33 33
         'uuid'    => '[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}',
34 34
     ];
35
-    private const URI_FIXERS       = [
35
+    private const URI_FIXERS = [
36 36
         '[]'  => '',
37 37
         '[/]' => '',
38 38
         '['   => '',
@@ -78,7 +78,7 @@  discard block
 block discarded – undo
78 78
     public function __construct(
79 79
         UriFactoryInterface $uriFactory,
80 80
         SlugifyInterface $slugify = null
81
-    ) {
81
+    ){
82 82
         $this->uriFactory = $uriFactory;
83 83
         $this->slugify = $slugify ?? new Slugify();
84 84
     }
@@ -167,12 +167,12 @@  discard block
 block discarded – undo
167 167
      */
168 168
     public function match(UriInterface $uri, array $defaults): ?array
169 169
     {
170
-        if (!$this->isCompiled()) {
170
+        if (!$this->isCompiled()){
171 171
             $this->compile();
172 172
         }
173 173
 
174 174
         $matches = [];
175
-        if (!preg_match($this->compiled, $this->fetchTarget($uri), $matches)) {
175
+        if (!preg_match($this->compiled, $this->fetchTarget($uri), $matches)){
176 176
             return null;
177 177
         }
178 178
 
@@ -190,7 +190,7 @@  discard block
 block discarded – undo
190 190
      */
191 191
     public function uri($parameters = [], array $defaults = []): UriInterface
192 192
     {
193
-        if (!$this->isCompiled()) {
193
+        if (!$this->isCompiled()){
194 194
             $this->compile();
195 195
         }
196 196
 
@@ -200,8 +200,8 @@  discard block
 block discarded – undo
200 200
             $this->fetchOptions($parameters, $query)
201 201
         );
202 202
 
203
-        foreach ($this->constrains as $key => $_) {
204
-            if (empty($parameters[$key])) {
203
+        foreach ($this->constrains as $key => $_){
204
+            if (empty($parameters[$key])){
205 205
                 throw new UriHandlerException("Unable to generate Uri, parameter `{$key}` is missing");
206 206
             }
207 207
         }
@@ -210,7 +210,7 @@  discard block
 block discarded – undo
210 210
         $path = $this->interpolate($this->template, $parameters);
211 211
 
212 212
         //Uri with added prefix
213
-        $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->prefix) . trim($path, '/'));
213
+        $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->prefix).trim($path, '/'));
214 214
 
215 215
         return empty($query) ? $uri : $uri->withQuery(http_build_query($query));
216 216
     }
@@ -227,18 +227,18 @@  discard block
 block discarded – undo
227 227
         $allowed = array_keys($this->options);
228 228
 
229 229
         $result = [];
230
-        foreach ($parameters as $key => $parameter) {
231
-            if (is_numeric($key) && isset($allowed[$key])) {
230
+        foreach ($parameters as $key => $parameter){
231
+            if (is_numeric($key) && isset($allowed[$key])){
232 232
                 // this segment fetched keys from given parameters either by name or by position
233 233
                 $key = $allowed[$key];
234
-            } elseif (!array_key_exists($key, $this->options) && is_array($parameters)) {
234
+            } elseif (!array_key_exists($key, $this->options) && is_array($parameters)){
235 235
                 // all additional parameters given in array form can be glued to query string
236 236
                 $query[$key] = $parameter;
237 237
                 continue;
238 238
             }
239 239
 
240 240
             //String must be normalized here
241
-            if (is_string($parameter) && !preg_match('/^[a-z\-_0-9]+$/i', $parameter)) {
241
+            if (is_string($parameter) && !preg_match('/^[a-z\-_0-9]+$/i', $parameter)){
242 242
                 $result[$key] = $this->slugify->slugify($parameter);
243 243
                 continue;
244 244
             }
@@ -259,15 +259,15 @@  discard block
 block discarded – undo
259 259
     {
260 260
         $path = $uri->getPath();
261 261
 
262
-        if (empty($path) || $path[0] !== '/') {
263
-            $path = '/' . $path;
262
+        if (empty($path) || $path[0] !== '/'){
263
+            $path = '/'.$path;
264 264
         }
265 265
 
266
-        if ($this->matchHost) {
267
-            $uriString = $uri->getHost() . $path;
268
-        } else {
266
+        if ($this->matchHost){
267
+            $uriString = $uri->getHost().$path;
268
+        }else{
269 269
             $uriString = substr($path, strlen($this->prefix));
270
-            if ($uriString === false) {
270
+            if ($uriString === false){
271 271
                 $uriString = '';
272 272
             }
273 273
         }
@@ -280,7 +280,7 @@  discard block
 block discarded – undo
280 280
      */
281 281
     private function compile(): void
282 282
     {
283
-        if ($this->pattern === null) {
283
+        if ($this->pattern === null){
284 284
             throw new UriHandlerException('Unable to compile UriHandler, pattern is not set');
285 285
         }
286 286
 
@@ -288,14 +288,14 @@  discard block
 block discarded – undo
288 288
         $pattern = rtrim(ltrim($this->pattern, ':/'), '/');
289 289
 
290 290
         // correct [/ first occurrence]
291
-        if (strpos($pattern, '[/') === 0) {
292
-            $pattern = '[' . substr($pattern, 2);
291
+        if (strpos($pattern, '[/') === 0){
292
+            $pattern = '['.substr($pattern, 2);
293 293
         }
294 294
 
295
-        if (preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) {
295
+        if (preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)){
296 296
             $variables = array_combine($matches[1], $matches[2]);
297 297
 
298
-            foreach ($variables as $key => $segment) {
298
+            foreach ($variables as $key => $segment){
299 299
                 $segment = $this->prepareSegment($key, $segment);
300 300
                 $replaces["<$key>"] = "(?P<$key>$segment)";
301 301
                 $options[] = $key;
@@ -305,13 +305,13 @@  discard block
 block discarded – undo
305 305
         $template = preg_replace('/<(\w+):?.*?>/', '<\1>', $pattern);
306 306
         $options = array_fill_keys($options, null);
307 307
 
308
-        foreach ($this->constrains as $key => $value) {
309
-            if ($value instanceof Autofill) {
308
+        foreach ($this->constrains as $key => $value){
309
+            if ($value instanceof Autofill){
310 310
                 // only forces value replacement, not required to be presented as parameter
311 311
                 continue;
312 312
             }
313 313
 
314
-            if (!array_key_exists($key, $options) && !isset($this->defaults[$key])) {
314
+            if (!array_key_exists($key, $options) && !isset($this->defaults[$key])){
315 315
                 throw new ConstrainException(
316 316
                     sprintf(
317 317
                         'Route `%s` does not define routing parameter `<%s>`.',
@@ -322,7 +322,7 @@  discard block
 block discarded – undo
322 322
             }
323 323
         }
324 324
 
325
-        $this->compiled = '/^' . strtr($template, $replaces + self::PATTERN_REPLACES) . '$/iu';
325
+        $this->compiled = '/^'.strtr($template, $replaces + self::PATTERN_REPLACES).'$/iu';
326 326
         $this->template = stripslashes(str_replace('?', '', $template));
327 327
         $this->options = $options;
328 328
     }
@@ -337,7 +337,7 @@  discard block
 block discarded – undo
337 337
     private function interpolate(string $string, array $values): string
338 338
     {
339 339
         $replaces = [];
340
-        foreach ($values as $key => $value) {
340
+        foreach ($values as $key => $value){
341 341
             $value = (is_array($value) || $value instanceof \Closure) ? '' : $value;
342 342
             $replaces["<{$key}>"] = is_object($value) ? (string)$value : $value;
343 343
         }
@@ -354,15 +354,15 @@  discard block
 block discarded – undo
354 354
      */
355 355
     private function prepareSegment(string $name, string $segment): string
356 356
     {
357
-        if ($segment !== '') {
357
+        if ($segment !== ''){
358 358
             return self::SEGMENT_TYPES[$segment] ?? $segment;
359 359
         }
360 360
 
361
-        if (!isset($this->constrains[$name])) {
361
+        if (!isset($this->constrains[$name])){
362 362
             return self::DEFAULT_SEGMENT;
363 363
         }
364 364
 
365
-        if (is_array($this->constrains[$name])) {
365
+        if (is_array($this->constrains[$name])){
366 366
             $values = array_map([$this, 'filterSegment'], $this->constrains[$name]);
367 367
 
368 368
             return implode('|', $values);
Please login to merge, or discard this patch.