This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
![]() |
|||
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
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. ![]() |
|||
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
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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
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
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. ![]() |
|||
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 |