Matcher   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 19
c 2
b 0
f 0
dl 0
loc 64
ccs 19
cts 19
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compileRegex() 0 15 3
A __construct() 0 4 1
A resolves() 0 16 4
1
<?php
2
namespace Fyuze\Routing;
3
4
use Psr\Http\Message\ServerRequestInterface;
5
6
class Matcher
7
{
8
    /**
9
     * @var ServerRequestInterface
10
     */
11
    protected $request;
12
13
    /**
14
     * @var Route
15
     */
16
    protected $route;
17
18
    /**
19
     * @param ServerRequestInterface $request
20
     * @param Route $route
21
     */
22 10
    public function __construct(ServerRequestInterface $request, Route $route)
23
    {
24 10
        $this->request = $request;
25 10
        $this->route = $route;
26
    }
27
28
    /**
29
     * @return bool
30
     */
31 10
    public function resolves()
32
    {
33 10
        if (preg_match($this->compileRegex(), $this->request->getUri()->getPath(), $parameters) > 0) {
34
35 7
            $params = [];
36
37 7
            foreach ($parameters as $key => $value) {
38 7
                if (!is_int($key)) {
39 2
                    $params[$key] = $value;
40
                }
41
            }
42
43 7
            return $params;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $params returns the type array which is incompatible with the documented return type boolean.
Loading history...
44
        }
45
46 7
        return false;
47
    }
48
49
    /**
50
     * Returns the regex needed to match the route.
51
     *
52
     * @access  public
53
     * @return  string
54
     */
55 10
    public function compileRegex()
56
    {
57 10
        $route = $this->route->getUri();
58
59 10
        if (strpos($route, '?')) {
60 1
            $route = preg_replace('@\/{([\w]+)\?}@', '(?:/{$1})?', $route);
61
        }
62
63 10
        $route = preg_replace('/{([a-z0-9_-]+)}/i', '(?P<$1>[^/]+)', $route);
64
65 10
        if(substr($route, -1) === '/') {
66 5
            $route .= '?';
67
        }
68
69 10
        return "%^{$route}$%s";
70
    }
71
}
72