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 |
||
10 | class Collection extends Coollection |
||
11 | { |
||
12 | /** |
||
13 | * Magic call methods. |
||
14 | * |
||
15 | * @param string $name |
||
16 | * @param array $arguments |
||
17 | * @return mixed|static |
||
18 | */ |
||
19 | 32 | public function __call($name, $arguments) |
|
34 | |||
35 | /** |
||
36 | * Hydrate configured default elements. |
||
37 | * |
||
38 | * @param Collection $countries |
||
39 | * @return Collection |
||
40 | */ |
||
41 | 27 | public function hydrateDefaultElements($countries) |
|
45 | |||
46 | /** |
||
47 | * Where on steroids. |
||
48 | * |
||
49 | * @param string $key |
||
50 | * @param mixed $operator |
||
51 | * @param null $value |
||
52 | * @return static |
||
53 | */ |
||
54 | 27 | public function where($key, $operator, $value = null) |
|
68 | |||
69 | /** |
||
70 | * Where language. |
||
71 | * |
||
72 | * @param $value |
||
73 | * @return static |
||
74 | */ |
||
75 | 1 | public function whereLanguage($value) |
|
79 | |||
80 | /** |
||
81 | * Where language using iso code. |
||
82 | * |
||
83 | * @param $value |
||
84 | * @return static |
||
85 | */ |
||
86 | 1 | public function whereISO639_3($value) |
|
90 | |||
91 | /** |
||
92 | * Where currency using ISO code. |
||
93 | * |
||
94 | * @param $value |
||
95 | * @return static |
||
96 | */ |
||
97 | 1 | public function whereISO4217($value) |
|
101 | |||
102 | /** |
||
103 | * Where for different attributes. |
||
104 | * |
||
105 | * @param string $propertyName |
||
106 | * @param $find |
||
107 | * @param Closure $finderClosure |
||
108 | * @return static |
||
109 | */ |
||
110 | 3 | private function arrayFinder(string $propertyName, $find, Closure $finderClosure) |
|
124 | |||
125 | /** |
||
126 | * Where for keys. |
||
127 | * |
||
128 | * @param string $arrayName |
||
129 | * @param $value |
||
130 | * @return static |
||
131 | */ |
||
132 | 1 | View Code Duplication | private function _whereKey(string $arrayName, $value) |
142 | |||
143 | /** |
||
144 | * Where for different attributes. |
||
145 | * |
||
146 | * @param string $arrayName |
||
147 | * @param $value |
||
148 | * @return static |
||
149 | */ |
||
150 | 2 | View Code Duplication | private function _whereAttribute(string $arrayName, $value) |
160 | } |
||
161 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.