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 |
||
11 | class UriMatcher implements MatcherInterface |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var |
||
16 | */ |
||
17 | private $router; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $request = []; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | private $matcher = ['matchControllerTemplate']; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private $dispatcher = [ |
||
33 | 'matchTemplate' => 'JetFire\Routing\Dispatcher\TemplateDispatcher', |
||
34 | 'matchController' => 'JetFire\Routing\Dispatcher\ControllerDispatcher' |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * @param Router $router |
||
39 | */ |
||
40 | public function __construct(Router $router) |
||
44 | |||
45 | /** |
||
46 | * @param string $matcher |
||
47 | */ |
||
48 | public function addMatcher($matcher){ |
||
51 | |||
52 | /** |
||
53 | * @return array |
||
54 | */ |
||
55 | public function getMatcher() |
||
59 | |||
60 | /** |
||
61 | * @param array $dispatcher |
||
62 | */ |
||
63 | public function setDispatcher($dispatcher = []) |
||
67 | |||
68 | /** |
||
69 | * @param $method |
||
70 | * @param $class |
||
71 | * @return mixed|void |
||
72 | */ |
||
73 | public function addDispatcher($method,$class){ |
||
76 | |||
77 | /** |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function match() |
||
91 | |||
92 | /** |
||
93 | * @param array $target |
||
94 | */ |
||
95 | View Code Duplication | public function setTarget($target = []){ |
|
102 | |||
103 | /** |
||
104 | * @return array|bool |
||
105 | */ |
||
106 | public function matchControllerTemplate(){ |
||
117 | |||
118 | /** |
||
119 | * @return bool|array |
||
120 | */ |
||
121 | public function matchTemplate() |
||
141 | |||
142 | /** |
||
143 | * @return bool|array |
||
144 | */ |
||
145 | public function matchController() |
||
171 | |||
172 | } |
||
173 |
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.