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 /** MicroInjector */ |
||
18 | class Injector |
||
19 | { |
||
20 | /** @var array $CONFIG Config data for components */ |
||
21 | private static $CONFIG; |
||
22 | |||
23 | |||
24 | /** |
||
25 | * Injector constructor. |
||
26 | * |
||
27 | * @access public |
||
28 | * @param array $config |
||
29 | * @result void |
||
30 | */ |
||
31 | public function __construct(array $config) |
||
35 | |||
36 | /** |
||
37 | * Build object with injector |
||
38 | * |
||
39 | * class LogInject extends Injector { |
||
40 | * public function build() { |
||
41 | * return $this->get('logger'); |
||
42 | * } |
||
43 | * } |
||
44 | * $log = (new LogInject())->build(); |
||
45 | * |
||
46 | * @access private |
||
47 | * @param string $name |
||
48 | * @return bool |
||
49 | */ |
||
50 | private function get($name) |
||
58 | |||
59 | /** |
||
60 | * Load component |
||
61 | * |
||
62 | * @access public |
||
63 | * |
||
64 | * @param array $options component configs |
||
65 | * |
||
66 | * @return bool |
||
67 | */ |
||
68 | private function loadComponent($options) |
||
109 | |||
110 | /** |
||
111 | * Build params from array |
||
112 | * |
||
113 | * @access private |
||
114 | * @param array $params |
||
115 | * @return array |
||
116 | */ |
||
117 | private function buildParams(array $params) |
||
132 | |||
133 | /** |
||
134 | * Build calls arguments |
||
135 | * |
||
136 | * @access private |
||
137 | * @param array $params |
||
138 | * @return array |
||
139 | */ |
||
140 | View Code Duplication | private function buildCalls(array $params) |
|
162 | |||
163 | /** |
||
164 | * Make object with arguments |
||
165 | * |
||
166 | * @access private |
||
167 | * |
||
168 | * @param string $className |
||
169 | * @param array $arguments |
||
170 | * |
||
171 | * @return mixed |
||
172 | */ |
||
173 | View Code Duplication | private function makeObject($className, array $arguments = []) |
|
188 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.