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.

Router   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 193
Duplicated Lines 12.44 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 24
loc 193
ccs 0
cts 64
cp 0
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 3

14 Methods

Rating   Name   Duplication   Size   Complexity  
A connected() 0 4 1
A getRoute() 0 4 1
A getRoutes() 0 8 2
A initRoutes() 0 4 1
A newRoutesCollection() 0 4 1
B route() 0 22 4
A setCurrent() 0 6 1
A assembleFull() 12 12 2
B getDefaultRoute() 0 18 6
A getRequest() 0 4 1
A setRequest() 0 4 1
A assemble() 12 12 2
A getCurrent() 0 4 1
A hasRoute() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Nip\Router;
4
5
use Nip\Request;
6
use Nip\Router\Route\AbstractRoute as Route;
7
use Psr\Http\Message\ServerRequestInterface;
8
9
/**
10
 * Class Router
11
 * @package Nip\Router
12
 */
13
class Router
14
{
15
16
    /**
17
     * @var \Nip\Request
18
     */
19
    protected $request;
20
21
22
    /**
23
     * @var Route
24
     */
25
    protected $route;
26
27
    /**
28
     * @var RouteCollection|Route[]
29
     */
30
    protected $routes = null;
31
32
    /**
33
     * @param $name
34
     * @return bool
35
     */
36
    public function connected($name)
37
    {
38
        return ($this->getRoute($name) instanceof Route);
39
    }
40
41
    /**
42
     * @param $name
43
     * @return Route
44
     */
45
    public function getRoute($name)
46
    {
47
        return $this->getRoutes()->get($name);
48
    }
49
50
    /**
51
     * @return RouteCollection
52
     */
53
    public function getRoutes()
54
    {
55
        if ($this->routes === null) {
56
            $this->initRoutes();
57
        }
58
59
        return $this->routes;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->routes; of type Nip\Router\RouteCollecti...r\Route\AbstractRoute[] adds the type Nip\Router\Route\AbstractRoute[] to the return on line 59 which is incompatible with the return type documented by Nip\Router\Router::getRoutes of type Nip\Router\RouteCollection.
Loading history...
60
    }
61
62
    protected function initRoutes()
63
    {
64
        $this->routes = $this->newRoutesCollection();
65
    }
66
67
    /**
68
     * @return RouteCollection
69
     */
70
    protected function newRoutesCollection()
71
    {
72
        return new RouteCollection();
73
    }
74
75
    /**
76
     * @param Request|ServerRequestInterface $request
77
     * @return array
78
     */
79
    public function route($request)
80
    {
81
        $current = false;
82
        $uri = $request->path();
83
84
        foreach ($this->routes as $name => $route) {
85
            $route->setRequest($request);
86
            if ($route->match($uri)) {
87
                $current = $route;
88
                break;
89
            }
90
        }
91
92
        if ($current instanceof Route) {
93
            $this->setCurrent($current);
94
            $current->populateRequest();
95
96
            return $current->getParams() + $current->getMatches();
97
        } else {
98
            return [];
99
        }
100
    }
101
102
    /**
103
     * @param Route $route
104
     * @return $this
105
     */
106
    public function setCurrent($route)
107
    {
108
        $this->route = $route;
109
110
        return $this;
111
    }
112
113
    /**
114
     * @param $name
115
     * @param array $params
116
     * @return string
117
     */
118 View Code Duplication
    public function assembleFull($name, $params = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        $route = $this->getDefaultRoute($name, $params);
121
        if ($route) {
122
            $route->setRequest($this->getRequest());
123
            return $route->assembleFull($params);
124
        }
125
126
        trigger_error("Route \"$name\" not connected", E_USER_ERROR);
127
128
        return null;
129
    }
130
131
    /**
132
     * @param $name
133
     * @param array $params
134
     * @return Route
135
     */
136
    public function getDefaultRoute($name, &$params = [])
137
    {
138
        $route = $this->getRoute($name);
139
        if (!$route) {
140
            $parts = explode(".", $name);
141
            $count = count($parts);
142
            if ($count <= 3) {
143
                if (in_array(reset($parts), app('mvc.modules')->getNames())) {
144
                    $module = array_shift($parts);
145
                    $params['controller'] = isset($parts[0]) ? $parts[0] : null;
146
                    $params['action'] = isset($parts[1]) ? $parts[1] : null;
147
                    $route = $this->getRoute($module.'.default');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $route is correct as $this->getRoute($module . '.default') (which targets Nip\Router\Router::getRoute()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
148
                }
149
            }
150
        }
151
152
        return $route;
153
    }
154
155
    /**
156
     * @return mixed
157
     */
158
    public function getRequest()
159
    {
160
        return $this->request;
161
    }
162
163
    /**
164
     * @param mixed $request
165
     */
166
    public function setRequest($request)
167
    {
168
        $this->request = $request;
169
    }
170
171
    /**
172
     * @param $name
173
     * @param array $params
174
     * @return mixed|string
175
     */
176 View Code Duplication
    public function assemble($name, $params = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178
        $route = $this->getDefaultRoute($name, $params);
179
180
        if ($route) {
181
            $route->setRequest($this->getRequest());
182
            return $route->assemble($params);
183
        }
184
185
        trigger_error("Route \"$name\" not connected", E_USER_ERROR);
186
        return null;
187
    }
188
189
    /**
190
     * @return Route
191
     */
192
    public function getCurrent()
193
    {
194
        return $this->route;
195
    }
196
197
    /**
198
     * @param $name
199
     * @return bool
200
     */
201
    public function hasRoute($name)
202
    {
203
        return $this->getRoutes()->has($name);
204
    }
205
}
206