Completed
Push — master ( b852c1...ed5538 )
by Pierre
03:14
created

Router   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 87.1%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 102
ccs 27
cts 31
cp 0.871
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A compile() 0 20 3
A __construct() 0 10 1
A setRoutes() 0 4 1
A getParams() 0 3 1
A getMatchingRoute() 0 3 1
1
<?php
2
3
namespace App\Component\Http;
4
5
use App\Component\Http\Interfaces\IRoutes;
6
use App\Component\Http\Interfaces\IRequest;
7
use App\Component\Http\Interfaces\IRouter;
8
9
class Router implements IRouter
10
{
11
    /**
12
     * active route
13
     *
14
     * @var string
15
     */
16
    private $activeRoute;
17
18
    /**
19
     * routes collection
20
     *
21
     * @var IRoutes
22
     */
23
    private $routes;
24
25
    /**
26
     * request
27
     *
28
     * @var IRequest
29
     */
30
    private $request = null;
31
32
    /**
33
     * route params
34
     *
35
     * @var array
36
     */
37
    private $params;
38
39
    /**
40
     * route match expr
41
     *
42
     * @var string
43
     */
44
    private $matchingRoute;
45
46
    /**
47
     * instanciate
48
     *
49
     * @param IRoutes $routes
50
     * @param IRequest $request
51
     */
52 3
    public function __construct(IRoutes $routes, IRequest $request)
53
    {
54 3
        $this->routes = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type App\Component\Http\Interfaces\IRoutes of property $routes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55 3
        $this->request = $request;
56 3
        $this->activeRoute = '';
57 3
        $this->params = [];
58 3
        $this->matchingRoute = '';
59 3
        $this->setRoutes($routes);
60 3
        $this->activeRoute = substr($this->request->getUri(), 1);
61 3
        return $this;
62
    }
63
64
    /**
65
     * set routes
66
     *
67
     * @param IRoutes $routes
68
     * @return Router
69
     */
70 3
    public function setRoutes(IRoutes $routes): Router
71
    {
72 3
        $this->routes = $routes->get();
0 ignored issues
show
Documentation Bug introduced by
It seems like $routes->get() of type array is incompatible with the declared type App\Component\Http\Interfaces\IRoutes of property $routes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73 3
        return $this;
74
    }
75
76
    /**
77
     * compile
78
     *
79
     * @return array
80
     */
81 1
    public function compile(): array
82
    {
83
        
84 1
        $routes = $this->routes;
85 1
        $routesLength = count($routes);
0 ignored issues
show
Bug introduced by
$routes of type App\Component\Http\Interfaces\IRoutes is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
        $routesLength = count(/** @scrutinizer ignore-type */ $routes);
Loading history...
86 1
        for ($i = 0; $i < $routesLength; $i++) {
87 1
            $matches = [];
88 1
            $match = preg_match(
89 1
                $routes[$i]->getExpr(),
90 1
                $this->activeRoute,
91 1
                $matches
92
            );
93 1
            if ($match) {
94 1
                $this->params = $matches;
95 1
                $this->matchingRoute = $routes[$i]->getExpr();
96 1
                array_shift($matches);
97 1
                return $matches;
98
            }
99
        }
100 1
        return [];
101
    }
102
103
    public function getParams():array
104
    {
105
        return $this->params;
106
    }
107
108
    public function getMatchingRoute():string
109
    {
110
        return $this->matchingRoute;
111
    }
112
}
113