GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

UrlMatcher::createRegex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 1
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lib\Routing;
6
7
class UrlMatcher
8
{
9
    /**
10
     * @var RouteCollection
11
     */
12
    private $collection;
13
14 6
    public function __construct(RouteCollection $collection)
15
    {
16 6
        $this->collection = $collection;
17 6
    }
18
19 4
    public function match($path)
20
    {
21 4
        if ($match = $this->matchCollection($path)) {
22 1
            return $match;
23
        }
24
25 3
        return ['controller' => 'DefaultController', 'action' => 'notFoundAction', 'params' => []];
26
    }
27
28 6
    public function matchCollection($path)
29
    {
30 6
        foreach ($this->collection->all() as $route) {
31 6
            $regex = $this->createRegex($route);
32
33 6
            if (preg_match('#^'.$regex.'$#', $path, $matches)) {
34
                $params = array_filter($matches, function ($key) {
35 2
                    return !\is_int($key);
36 2
                }, ARRAY_FILTER_USE_KEY);
37
38
                return [
39 2
                    'controller' => $route->getDefault('controller'),
40 2
                    'action' => $route->getDefault('action'),
41 6
                    'params' => $params,
42
                ];
43
            }
44
        }
45 4
    }
46
47 6
    private function createRegex(RouteDefinition $route): string
48
    {
49 6
        $regex = $route->getPathRegex();
50 6
        $reqs = $route->getRequirements();
51 6
        $regex = preg_replace_callback(
52 6
            '/{(\w+)}/',
53
            function ($matches) use ($reqs) {
54 4
                $req = $reqs[$matches[1]] ?? '.+';
55
56 4
                return "(?<{$matches[1]}>{$req})";
57 6
            },
58 6
            $regex
59
        );
60
61 6
        return $regex;
62
    }
63
}
64