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 |
||
18 | class Config implements ArrayAccess, Serializable, JsonSerializable |
||
19 | { |
||
20 | use SingletonTrait; |
||
21 | |||
22 | /** |
||
23 | * Store config |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected static $container = []; |
||
28 | |||
29 | /** |
||
30 | * Store registered engine |
||
31 | * |
||
32 | * @var EngineInterface[] |
||
33 | */ |
||
34 | protected static $engines = []; |
||
35 | |||
36 | /** |
||
37 | * Load config |
||
38 | * |
||
39 | * @param mixed $config Config data for passed to engine load method |
||
40 | * @param string $engineName Engine registered name |
||
41 | * @param bool $merge Is config merged or overwrite |
||
42 | * |
||
43 | * @return bool |
||
44 | */ |
||
45 | public static function load($config, $engineName = 'default', $merge = true) |
||
59 | |||
60 | /** |
||
61 | * Dump config |
||
62 | * |
||
63 | * @param string $file File path for save config |
||
64 | * @param string $engineName Engine name for use in dump |
||
65 | * |
||
66 | * @return bool |
||
67 | */ |
||
68 | public static function dump($file, $engineName = 'default') |
||
76 | |||
77 | /** |
||
78 | * Set config |
||
79 | * |
||
80 | * @param string $identifier Parameter name. |
||
81 | * @param mixed $value Value to set |
||
82 | */ |
||
83 | public static function set($identifier, $value) |
||
99 | |||
100 | /** |
||
101 | * Get config |
||
102 | * |
||
103 | * @param string $identifier Parameter name. |
||
104 | * @param null $default Default value |
||
105 | * |
||
106 | * @return array|null |
||
107 | */ |
||
108 | public static function get($identifier, $default = null) |
||
118 | |||
119 | /** |
||
120 | * Indicates whether parameter exists or not |
||
121 | * |
||
122 | * @param string $identifier Parameter name. |
||
123 | * |
||
124 | * @return bool |
||
125 | */ |
||
126 | public static function has($identifier) |
||
130 | |||
131 | /** |
||
132 | * Register engine |
||
133 | * |
||
134 | * @param string $name Engine name |
||
135 | * @param EngineInterface $engine Engine instance |
||
136 | */ |
||
137 | public static function registerEngine($name, EngineInterface $engine) |
||
141 | |||
142 | /** |
||
143 | * Get engine |
||
144 | * |
||
145 | * @param string $name Engine name |
||
146 | * |
||
147 | * @return bool|EngineInterface |
||
148 | */ |
||
149 | protected static function getEngine($name) |
||
160 | |||
161 | /** |
||
162 | * Whether a offset exists |
||
163 | * |
||
164 | * @param mixed $offset An offset to check for. |
||
165 | * |
||
166 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
167 | * @return boolean true on success or false on failure. |
||
168 | * The return value will be casted to boolean if non-boolean was returned. |
||
169 | */ |
||
170 | public function offsetExists($offset) |
||
174 | |||
175 | /** |
||
176 | * Offset to retrieve |
||
177 | * |
||
178 | * @param mixed $offset The offset to retrieve. |
||
179 | * |
||
180 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
181 | * @return mixed Can return all value types. |
||
182 | */ |
||
183 | public function offsetGet($offset) |
||
187 | |||
188 | /** |
||
189 | * Offset to set |
||
190 | * |
||
191 | * @param mixed $offset The offset to assign the value to. |
||
192 | * @param mixed $value The value to set. |
||
193 | * |
||
194 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
195 | * @return void |
||
196 | */ |
||
197 | public function offsetSet($offset, $value) |
||
201 | |||
202 | /** |
||
203 | * Offset to unset |
||
204 | * |
||
205 | * @param mixed $offset The offset to unset. |
||
206 | * |
||
207 | * @throws Exception |
||
208 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
209 | */ |
||
210 | public function offsetUnset($offset) |
||
214 | |||
215 | /** |
||
216 | * Retrieves parameter |
||
217 | * |
||
218 | * @param string $identifier Parameter name. |
||
219 | * |
||
220 | * @return array|null |
||
221 | */ |
||
222 | protected static function getInternal($identifier) |
||
239 | |||
240 | /** |
||
241 | * Merge config |
||
242 | * |
||
243 | * @param mixed $newData |
||
244 | * @param array $baseConfig |
||
245 | */ |
||
246 | protected static function mergeConfig($newData, &$baseConfig) |
||
260 | |||
261 | /** |
||
262 | * String representation of object |
||
263 | * |
||
264 | * @link http://php.net/manual/en/serializable.serialize.php |
||
265 | * @return string the string representation of the object or null |
||
266 | */ |
||
267 | public function serialize() |
||
271 | |||
272 | /** |
||
273 | * Constructs the object |
||
274 | * |
||
275 | * @param string $serialized The string representation of the object. |
||
276 | * |
||
277 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
278 | * @return void |
||
279 | */ |
||
280 | public function unserialize($serialized) |
||
284 | |||
285 | /** |
||
286 | * Specify data which should be serialized to JSON |
||
287 | * |
||
288 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
289 | * @return mixed data which can be serialized by "json_encode", |
||
290 | * which is a value of any type other than a resource. |
||
291 | */ |
||
292 | public function jsonSerialize() |
||
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.