Total Complexity | 47 |
Total Lines | 247 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Router often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Router, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Router |
||
23 | { |
||
24 | /** |
||
25 | * Router::$uri |
||
26 | * |
||
27 | * @var Router\Uri |
||
|
|||
28 | */ |
||
29 | protected $uri; |
||
30 | |||
31 | /** |
||
32 | * Router::$addresses |
||
33 | * |
||
34 | * @var Router\Addresses |
||
35 | */ |
||
36 | protected $addresses; |
||
37 | |||
38 | // ------------------------------------------------------------------------ |
||
39 | |||
40 | public function getUri() |
||
41 | { |
||
42 | return $this->uri; |
||
43 | } |
||
44 | |||
45 | public function setAddresses(Router\Addresses $addresses) |
||
50 | } |
||
51 | |||
52 | public function getAddresses() |
||
55 | } |
||
56 | |||
57 | // ------------------------------------------------------------------------ |
||
58 | |||
59 | public function parseRequest(Message\Uri $uri = null) |
||
60 | { |
||
61 | $this->uri = is_null($uri) ? server_request()->getUri() : $uri; |
||
62 | $uriSegments = $this->uri->getSegments()->getParts(); |
||
63 | $uriString = $this->uri->getSegments()->getString(); |
||
64 | |||
65 | if($this->uri->getSegments()->getTotalParts()) { |
||
66 | if(strpos(end($uriSegments), '.json') !== false) { |
||
67 | output()->setContentType('application/json'); |
||
68 | $endSegment = str_replace('.json', '', end($uriSegments)); |
||
69 | array_pop($uriSegments); |
||
70 | array_push($uriSegments, $endSegment); |
||
71 | $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
||
72 | $uriString = $this->uri->getSegments()->getString(); |
||
73 | } elseif(strpos(end($uriSegments), '.xml') !== false) { |
||
74 | output()->setContentType('application/xml'); |
||
75 | $endSegment = str_replace('.xml', '', end($uriSegments)); |
||
76 | array_pop($uriSegments); |
||
77 | array_push($uriSegments, $endSegment); |
||
78 | $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
||
79 | $uriString = $this->uri->getSegments()->getString(); |
||
80 | } |
||
81 | } else { |
||
82 | $uriPath = urldecode( |
||
83 | parse_url($_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH) |
||
84 | ); |
||
85 | |||
86 | $uriPathParts = explode('public/', $uriPath); |
||
87 | $uriPath = end($uriPathParts); |
||
88 | |||
89 | if ($uriPath !== '/') { |
||
90 | $uriString = $uriPath; |
||
91 | $uriSegments = array_filter(explode('/', $uriString)); |
||
92 | |||
93 | $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
||
94 | $uriString = $this->uri->getSegments()->getString(); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | if ($this->addresses instanceof Router\Addresses) { |
||
99 | // Domain routing |
||
100 | if (null !== ($domain = $this->addresses->getDomain())) { |
||
101 | if (is_array($domain)) { |
||
102 | $uriSegments = array_merge($domain, $uriSegments); |
||
103 | $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
||
104 | $uriString = $this->uri->getSegments()->getString(); |
||
105 | } |
||
106 | } elseif (false !== ($subdomain = $this->uri->getSubdomain())) { |
||
107 | if (is_array($subdomain)) { |
||
108 | $uriSegments = array_merge($subdomain, $uriSegments); |
||
109 | $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
||
110 | $uriString = $this->uri->getSegments()->getString(); |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | |||
115 | // Try to translate from uri string |
||
116 | if (false !== ($action = $this->addresses->getTranslation($uriString))) { |
||
117 | if ( ! $action->isValidHttpMethod(server_request()->getMethod()) && ! $action->isAnyHttpMethod()) { |
||
118 | output()->sendError(405); |
||
119 | } else { |
||
120 | if (false !== ($parseSegments = $action->getParseUriString($uriString))) { |
||
121 | $uriSegments = $parseSegments; |
||
122 | } else { |
||
123 | $uriSegments = []; |
||
124 | } |
||
125 | |||
126 | $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
||
127 | $uriString = $this->uri->getSegments()->getString(); |
||
128 | |||
129 | $this->parseAction($action, $uriSegments); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | // Let's the app do the rest when there is no controller found |
||
134 | // the app should redirect to PAGE 404 |
||
135 | } |
||
136 | |||
137 | // ------------------------------------------------------------------------ |
||
138 | |||
139 | protected function parseAction(Router\Datastructures\Action $action, array $uriSegments = []) |
||
140 | { |
||
141 | $closure = $action->getClosure(); |
||
142 | if (empty($closure)) { |
||
143 | output()->sendError(204); |
||
144 | } |
||
145 | |||
146 | if ($closure instanceof Controller) { |
||
147 | $uriSegments = empty($uriSegments) |
||
148 | ? $action->getClosureParameters() |
||
149 | : $uriSegments; |
||
150 | $this->setController( |
||
151 | (new Router\Datastructures\Controller($closure)) |
||
152 | ->setRequestMethod('index'), |
||
153 | $uriSegments |
||
154 | ); |
||
155 | } elseif ($closure instanceof Router\Datastructures\Controller) { |
||
156 | $this->setController($closure, $action->getClosureParameters()); |
||
157 | } elseif (is_array($closure)) { |
||
158 | $uri = (new Message\Uri()) |
||
159 | ->withSegments(new Message\Uri\Segments('')) |
||
160 | ->withQuery(''); |
||
161 | $this->parseRequest($this->uri->addSegments($closure)); |
||
162 | } else { |
||
163 | if (class_exists($closure)) { |
||
164 | $this->setController( |
||
165 | (new Router\Datastructures\Controller($closure)) |
||
166 | ->setRequestMethod('index'), |
||
167 | $uriSegments |
||
168 | ); |
||
169 | } elseif (preg_match("/([a-zA-Z0-9\\\]+)(@)([a-zA-Z0-9\\\]+)/", $closure, $matches)) { |
||
170 | $this->setController( |
||
171 | (new Router\Datastructures\Controller($matches[ 1 ])) |
||
172 | ->setRequestMethod($matches[ 3 ]), |
||
173 | $uriSegments |
||
174 | ); |
||
175 | } elseif (is_string($closure) && $closure !== '') { |
||
176 | if (is_json($closure)) { |
||
177 | output()->setContentType('application/json'); |
||
178 | output()->send($closure); |
||
179 | } else { |
||
180 | output()->send($closure); |
||
181 | } |
||
182 | } elseif (is_array($closure) || is_object($closure)) { |
||
183 | output()->send($closure); |
||
184 | } elseif (is_numeric($closure)) { |
||
185 | output()->sendError($closure); |
||
186 | } else { |
||
187 | output()->sendError(204); |
||
188 | exit(EXIT_ERROR); |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | |||
193 | // ------------------------------------------------------------------------ |
||
194 | |||
195 | protected function setController( |
||
269 | } |
||
270 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths