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 ArrayMatcher 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 ArrayMatcher, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class ArrayMatcher implements MatcherInterface |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * @var |
||
17 | */ |
||
18 | private $router; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $request = []; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $resolver = ['isClosureAndTemplate','isControllerAndTemplate','isTemplate']; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $dispatcher = [ |
||
34 | 'isClosure' => 'JetFire\Routing\Dispatcher\ClosureDispatcher', |
||
35 | 'isController' => 'JetFire\Routing\Dispatcher\ControllerDispatcher', |
||
36 | 'isTemplate' => 'JetFire\Routing\Dispatcher\TemplateDispatcher', |
||
37 | 'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher','JetFire\Routing\Dispatcher\TemplateDispatcher'], |
||
38 | 'isClosureAndTemplate' => ['JetFire\Routing\Dispatcher\ClosureDispatcher','JetFire\Routing\Dispatcher\TemplateDispatcher'], |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * @param Router $router |
||
43 | */ |
||
44 | public function __construct(Router $router) |
||
48 | |||
49 | /** |
||
50 | * @param array $resolver |
||
51 | */ |
||
52 | public function setResolver($resolver = []){ |
||
55 | |||
56 | /** |
||
57 | * @param string $resolver |
||
58 | */ |
||
59 | public function addResolver($resolver){ |
||
62 | |||
63 | /** |
||
64 | * @return array |
||
65 | */ |
||
66 | public function getResolver() |
||
70 | |||
71 | /** |
||
72 | * @param array $dispatcher |
||
73 | */ |
||
74 | public function setDispatcher($dispatcher = []){ |
||
77 | |||
78 | /** |
||
79 | * @param $method |
||
80 | * @param $class |
||
81 | * @return mixed|void |
||
82 | * @internal param array $dispatcher |
||
83 | */ |
||
84 | public function addDispatcher($method,$class){ |
||
87 | |||
88 | /** |
||
89 | * @return bool |
||
90 | */ |
||
91 | public function match() |
||
112 | |||
113 | /** |
||
114 | * @param $route |
||
115 | * @return bool |
||
116 | */ |
||
117 | private function checkSubdomain($route){ |
||
134 | |||
135 | /** |
||
136 | * @return string |
||
137 | */ |
||
138 | private function subdomainMatch() |
||
146 | |||
147 | /** |
||
148 | * @param $match |
||
149 | * @return string |
||
150 | */ |
||
151 | private function paramMatch($match) |
||
159 | |||
160 | /** |
||
161 | * @param $regex |
||
162 | * @return bool |
||
163 | */ |
||
164 | private function routeMatch($regex) |
||
177 | |||
178 | /** |
||
179 | * @return bool |
||
180 | */ |
||
181 | private function generateTarget() |
||
193 | |||
194 | /** |
||
195 | * @param array $target |
||
196 | */ |
||
197 | View Code Duplication | public function setTarget($target = []){ |
|
204 | |||
205 | /** |
||
206 | * |
||
207 | */ |
||
208 | private function setCallback(){ |
||
221 | |||
222 | /** |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function validMethod() |
||
233 | |||
234 | /** |
||
235 | * @param $callback |
||
236 | * @return array|bool |
||
237 | * @throws \Exception |
||
238 | */ |
||
239 | View Code Duplication | public function isClosureAndTemplate($callback){ |
|
250 | |||
251 | /** |
||
252 | * @param $callback |
||
253 | * @return array|bool |
||
254 | * @throws \Exception |
||
255 | */ |
||
256 | View Code Duplication | public function isControllerAndTemplate($callback){ |
|
267 | |||
268 | |||
269 | /** |
||
270 | * @param $callback |
||
271 | * @return bool|array |
||
272 | */ |
||
273 | public function isClosure($callback) |
||
283 | |||
284 | /** |
||
285 | * @param $callback |
||
286 | * @throws \Exception |
||
287 | * @return bool|array |
||
288 | */ |
||
289 | public function isController($callback) |
||
312 | |||
313 | /** |
||
314 | * @param $callback |
||
315 | * @throws \Exception |
||
316 | * @return bool|array |
||
317 | */ |
||
318 | public function isTemplate($callback) |
||
348 | |||
349 | } |
||
350 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: