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 |
||
12 | trait DynamicObjectsTrait |
||
13 | { |
||
14 | use MemoizeTrait; |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected static $dynamicMethods = array(); |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected static $dynamicProperties = array(); |
||
25 | |||
26 | /** |
||
27 | * Add a dynamic property. |
||
28 | * |
||
29 | * @param string|array $names |
||
30 | * The property name or an array of property names. |
||
31 | * @param mixed $value |
||
32 | * The property value. |
||
33 | * @param bool $memoize |
||
34 | * Memoize parameter. |
||
35 | */ |
||
36 | 5 | View Code Duplication | public static function addDynamicProperty($names, $value, $memoize = false) |
50 | |||
51 | /** |
||
52 | * Add a dynamic method. |
||
53 | * |
||
54 | * @param string|array $names |
||
55 | * The method name or an array of method names. |
||
56 | * @param \Closure $func |
||
57 | * The method. |
||
58 | * @param bool $memoize |
||
59 | * Memoize parameter. |
||
60 | * @param bool $static |
||
61 | * Static flag parameter. |
||
62 | */ |
||
63 | 5 | View Code Duplication | public static function addDynamicMethod($names, \Closure $func, $memoize = false, $static = false) |
78 | |||
79 | /** |
||
80 | * Check if a dynamic property exists. |
||
81 | * |
||
82 | * @param string $name |
||
83 | * The property name. |
||
84 | * @return bool |
||
85 | * True if the property exists, false otherwise. |
||
86 | */ |
||
87 | 8 | public static function hasDynamicProperty($name) |
|
91 | |||
92 | /** |
||
93 | * Check if a dynamic method exists. |
||
94 | * |
||
95 | * @param string $name |
||
96 | * The property name. |
||
97 | * @return bool |
||
98 | * True if the property exists, false otherwise. |
||
99 | */ |
||
100 | 7 | public static function hasDynamicMethod($name) |
|
104 | |||
105 | /** |
||
106 | * Get a dynamic property. |
||
107 | * |
||
108 | * @param $name |
||
109 | * The property name. |
||
110 | * @return mixed|null |
||
111 | * The property value if it exists, null otherwise. |
||
112 | */ |
||
113 | 4 | public static function getDynamicProperty($name) |
|
121 | |||
122 | /** |
||
123 | * Get a dynamic method. |
||
124 | * |
||
125 | * @param $name |
||
126 | * The method name. |
||
127 | * @return array|null |
||
128 | * The method data if it exists, null otherwise. |
||
129 | */ |
||
130 | 4 | public static function getDynamicMethod($name) |
|
135 | |||
136 | /** |
||
137 | * Clear dynamic properties. |
||
138 | */ |
||
139 | 1 | public static function clearDynamicProperties() |
|
143 | |||
144 | /** |
||
145 | * Clear dynamic methods. |
||
146 | */ |
||
147 | 1 | public static function clearDynamicMethods() |
|
151 | |||
152 | /** |
||
153 | * Remove a dynamic property. |
||
154 | * |
||
155 | * @param string $name |
||
156 | * The property name. |
||
157 | */ |
||
158 | 1 | public static function removeDynamicProperty($name) |
|
162 | |||
163 | /** |
||
164 | * Remove a dynamic method. |
||
165 | * |
||
166 | * @param string $name |
||
167 | * The method name. |
||
168 | */ |
||
169 | 1 | public static function removeDynamicMethod($name) |
|
173 | |||
174 | /** |
||
175 | * Execute a closure. |
||
176 | * |
||
177 | * @param \Closure $func |
||
178 | * The closure. |
||
179 | * @param array $parameters |
||
180 | * The closure's parameters. |
||
181 | * @param bool $memoize |
||
182 | * The memoize parameter. |
||
183 | * |
||
184 | * @return mixed|null |
||
185 | * The return of the closure. |
||
186 | * |
||
187 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
188 | */ |
||
189 | 5 | public function doDynamicRequest(\Closure $func, array $parameters = [], $memoize = false) |
|
201 | |||
202 | /** |
||
203 | * Extend the dynamic object. |
||
204 | * |
||
205 | * @param mixed $extensions |
||
206 | * A file that returns a callable or a callable. |
||
207 | * |
||
208 | * @return $this |
||
209 | * |
||
210 | * @throws \InvalidArgumentException |
||
211 | */ |
||
212 | 1 | public function extend($extensions = null) |
|
232 | |||
233 | /** |
||
234 | * @param $method |
||
235 | * @param array $parameters |
||
236 | * |
||
237 | * @return mixed |
||
238 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
239 | */ |
||
240 | 4 | public function __call($method, array $parameters = array()) |
|
248 | |||
249 | /** |
||
250 | * @param $method |
||
251 | * @param array $parameters |
||
252 | * |
||
253 | * @return mixed |
||
254 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
255 | */ |
||
256 | 2 | public static function __callStatic($method, array $parameters = array()) |
|
268 | |||
269 | /** |
||
270 | * {inheritdoc} |
||
271 | */ |
||
272 | 4 | public function __get($property) |
|
286 | |||
287 | /** |
||
288 | * {inheritdoc} |
||
289 | */ |
||
290 | 3 | public function __set($property, $value) |
|
296 | } |
||
297 |
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.