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:
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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 implements \IteratorAggregate |
||
23 | { |
||
24 | /** @var string The base path. */ |
||
25 | private $basePath; |
||
26 | |||
27 | /** @var array The routes. */ |
||
28 | private $routes; |
||
29 | |||
30 | /** |
||
31 | * Construct a Router object with the given routers. |
||
32 | * |
||
33 | * @param string $basePath = '' |
||
34 | * @param array $routes = [] |
||
|
|||
35 | */ |
||
36 | public function __construct($basePath = '') |
||
41 | |||
42 | 16 | /** |
|
43 | 16 | * Returns the base path. |
|
44 | 16 | * |
|
45 | 16 | * @return string the base path. |
|
46 | */ |
||
47 | public function getBasePath() |
||
51 | |||
52 | 2 | /** |
|
53 | * Set the base path. |
||
54 | 2 | * |
|
55 | * @param string $basePath |
||
56 | * @return $this |
||
57 | */ |
||
58 | public function setBasePath($basePath) |
||
64 | |||
65 | 2 | /** |
|
66 | * {@inheritdoc} |
||
67 | 3 | */ |
|
68 | public function getIterator() |
||
72 | |||
73 | 1 | /** |
|
74 | * Returns the number of key-value mappings in the router map. |
||
75 | 1 | * |
|
76 | * @return int the number of key-value mappings in the router map. |
||
77 | */ |
||
78 | public function count() |
||
88 | 1 | ||
89 | 1 | /** |
|
90 | * Returns true if the router map contains no key-value mappings. |
||
91 | 1 | * |
|
92 | * @return bool true if the router map contains no key-value mappings. |
||
93 | */ |
||
94 | public function isEmpty() |
||
98 | |||
99 | 2 | /** |
|
100 | * Returns true if the router map contains a mapping for the specified method & route. |
||
101 | 2 | * |
|
102 | * @param string $method |
||
103 | * @param string $route |
||
104 | * @return bool true if the static route map contains a mapping for the specified method & route. |
||
105 | */ |
||
106 | View Code Duplication | public function contains($method, $route) |
|
120 | |||
121 | /** |
||
122 | 1 | * Returns true if the router map maps one or more routes to the specified callable. |
|
123 | * |
||
124 | 1 | * @param callable $callable |
|
125 | 1 | * @return bool true if the router map maps one or more routes to the specified callable. |
|
126 | 1 | */ |
|
127 | 1 | public function containsCallable(callable $callable) |
|
140 | |||
141 | /** |
||
142 | 6 | * Returns the callable to which the specified route is mapped, or null if the router map contains no mapping for the route. |
|
143 | * |
||
144 | 6 | * @param string $method |
|
145 | * @param string $route |
||
146 | * @return callable the callable to which the specified route is mapped, or null if the router map contains no mapping for the route. |
||
147 | */ |
||
148 | View Code Duplication | public function get($method, $route) |
|
162 | |||
163 | /** |
||
164 | * Associates the specified callable with the specified method & route in the route map. |
||
165 | * |
||
166 | * @param string|array $methods |
||
167 | * @param string $route |
||
168 | * @param callable $callable |
||
169 | * @return $this |
||
170 | 1 | */ |
|
171 | public function set($methods, $route, callable $callable) |
||
183 | 1 | ||
184 | /** |
||
185 | * Adds a method & route to the to the route map. |
||
186 | * |
||
187 | * @param string $methods |
||
188 | * @param string $route |
||
189 | * @param callable $callable |
||
190 | */ |
||
191 | private function add(string $method, string $route, callable $callable) |
||
205 | 5 | ||
206 | 1 | /** |
|
207 | 1 | * Creates a regex-enabled pattern from the route |
|
208 | * |
||
209 | 5 | * @param string $route |
|
210 | * @return string the pattern string. |
||
211 | */ |
||
212 | private function createPattern(string $route) |
||
216 | |||
217 | /** |
||
218 | * Removes the mapping for the specified route from the router map if present. |
||
219 | * |
||
220 | 5 | * @param string $method |
|
221 | * @param string $route |
||
222 | 5 | * @return null |
|
223 | 4 | */ |
|
224 | public function remove($method, $route) |
||
240 | |||
241 | /** |
||
242 | * Removes all of the mappings from the router map. |
||
243 | * |
||
244 | * @return null |
||
245 | */ |
||
246 | public function clear() |
||
250 | |||
251 | /** |
||
252 | * Returns the result of the given route's callable. |
||
253 | * |
||
254 | * @param string|null $method = new ServerRequest()->getMethod() |
||
255 | * @param string|null $route = new ServerRequest()->getUri()->getPath() |
||
256 | * @return mixed the result of the given route's callable. |
||
257 | * @throws ServerResponseException |
||
258 | */ |
||
259 | public function resolve($method = null, $route = null) |
||
277 | |||
278 | /** |
||
279 | * Returns the callable to which the specied method and route are mapped. |
||
280 | * |
||
281 | * @param string $method |
||
282 | * @param string $route |
||
283 | * @return array the callable to which the specied method and route are mapped and the route matches. |
||
284 | * @throws ServerResponseException |
||
285 | */ |
||
286 | private function getCallable($method, $route) |
||
301 | |||
302 | /** |
||
303 | * Returns the result of the callable. |
||
304 | * |
||
305 | * @param array the callable and the route matches. |
||
306 | * @return mixed the result the callable. |
||
307 | */ |
||
308 | private function callCallable($callable) |
||
312 | } |
||
313 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.