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 Configuration */ |
||
21 | private static $CONFIG = []; |
||
22 | /** @var array $INJECTS Configured injects */ |
||
23 | private static $INJECTS = []; |
||
24 | |||
25 | |||
26 | /** |
||
27 | * Injector constructor. |
||
28 | * |
||
29 | * @access public |
||
30 | * @param string $configPath |
||
31 | * @result void |
||
32 | */ |
||
33 | public function __construct($configPath = '') |
||
42 | |||
43 | /** |
||
44 | * Add requirement to injector |
||
45 | * |
||
46 | * @access public |
||
47 | * @param string $name |
||
48 | * @param mixed $component |
||
49 | * @return void |
||
50 | */ |
||
51 | public function addRequirement($name, $component) |
||
59 | |||
60 | /** |
||
61 | * Build object with injector |
||
62 | * |
||
63 | * class LogInject extends Injector { |
||
64 | * public function build() { |
||
65 | * return $this->get('logger'); |
||
66 | * } |
||
67 | * } |
||
68 | * $log = (new LogInject())->build(); |
||
69 | * |
||
70 | * @access protected |
||
71 | * @param string $name |
||
72 | * @return bool |
||
73 | */ |
||
74 | protected function get($name) |
||
86 | |||
87 | /** |
||
88 | * Load injection |
||
89 | * |
||
90 | * @access public |
||
91 | * |
||
92 | * @param string $name Name injection |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | private function loadInjection($name) |
||
141 | |||
142 | /** |
||
143 | * Build params from array |
||
144 | * |
||
145 | * @access private |
||
146 | * @param array $params |
||
147 | * @return array |
||
148 | */ |
||
149 | private function buildParams(array $params) |
||
164 | |||
165 | /** |
||
166 | * Build calls arguments |
||
167 | * |
||
168 | * @access private |
||
169 | * @param array $params |
||
170 | * @return array |
||
171 | */ |
||
172 | View Code Duplication | private function buildCalls(array $params) |
|
194 | |||
195 | /** |
||
196 | * Make object with arguments |
||
197 | * |
||
198 | * @access private |
||
199 | * |
||
200 | * @param string $className |
||
201 | * @param array $arguments |
||
202 | * |
||
203 | * @return mixed |
||
204 | */ |
||
205 | View Code Duplication | private function makeObject($className, array $arguments = []) |
|
220 | } |