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 |
||
20 | abstract class AbstractEntity implements EntityInterface |
||
21 | { |
||
22 | /** |
||
23 | * Holds a list of all data keys. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $keys = []; |
||
28 | |||
29 | /** |
||
30 | * Gets the data keys on this entity. |
||
31 | * |
||
32 | * @return array |
||
33 | */ |
||
34 | public function keys() |
||
38 | |||
39 | /** |
||
40 | * Gets all data, or a subset, from this entity. |
||
41 | * |
||
42 | * @uses self::offsetExists() |
||
43 | * @uses self::offsetGet() |
||
44 | * @param string[] $keys Optional. Extracts only the requested data. |
||
45 | * @return array Key-value array of data, excluding pairs with NULL values. |
||
46 | */ |
||
47 | public function data(array $keys = null) |
||
66 | |||
67 | /** |
||
68 | * Sets data on this entity. |
||
69 | * |
||
70 | * @uses self::offsetSet() |
||
71 | * @param array $data Key-value array of data to append. |
||
72 | * @return self |
||
73 | */ |
||
74 | public function setData(array $data) |
||
86 | |||
87 | /** |
||
88 | * Determines if this entity contains the specified key and if its value is not NULL. |
||
89 | * |
||
90 | * @uses self::offsetExists() |
||
91 | * @param string $key The data key to check. |
||
92 | * @return boolean TRUE if $key exists and has a value other than NULL, FALSE otherwise. |
||
93 | */ |
||
94 | public function has($key) |
||
98 | |||
99 | /** |
||
100 | * Find an entry of the configuration by its key and retrieve it. |
||
101 | * |
||
102 | * @uses self::offsetGet() |
||
103 | * @param string $key The data key to retrieve. |
||
104 | * @return mixed Value of the requested $key on success, NULL if the $key is not set. |
||
105 | */ |
||
106 | public function get($key) |
||
110 | |||
111 | /** |
||
112 | * Assign a value to the specified key on this entity. |
||
113 | * |
||
114 | * @uses self::offsetSet() |
||
115 | * @param string $key The data key to assign $value to. |
||
116 | * @param mixed $value The data value to assign to $key. |
||
117 | * @return self Chainable |
||
118 | */ |
||
119 | public function set($key, $value) |
||
124 | |||
125 | /** |
||
126 | * Determines if this entity contains the specified key and if its value is not NULL. |
||
127 | * |
||
128 | * Routine: |
||
129 | * - If the entity has a getter method (e.g., "foo_bar" → `fooBar()`), |
||
130 | * its called and its value is checked; |
||
131 | * - If the entity has a property (e.g., `$fooBar`), its value is checked; |
||
132 | * - If the entity has neither, FALSE is returned. |
||
133 | * |
||
134 | * @see \ArrayAccess |
||
135 | * @param string $key The data key to check. |
||
136 | * @throws InvalidArgumentException If the $key is not a string or is a numeric value. |
||
137 | * @return boolean TRUE if $key exists and has a value other than NULL, FALSE otherwise. |
||
138 | */ |
||
139 | public function offsetExists($key) |
||
169 | |||
170 | /** |
||
171 | * Returns the value from the specified key on this entity. |
||
172 | * |
||
173 | * Routine: |
||
174 | * - If the entity has a getter method (e.g., "foo_bar" → `fooBar()`), |
||
175 | * its called and returns its value; |
||
176 | * - If the entity has a property (e.g., `$fooBar`), its value is returned; |
||
177 | * - If the entity has neither, NULL is returned. |
||
178 | * |
||
179 | * @see \ArrayAccess |
||
180 | * @param string $key The data key to retrieve. |
||
181 | * @throws InvalidArgumentException If the $key is not a string or is a numeric value. |
||
182 | * @return mixed Value of the requested $key on success, NULL if the $key is not set. |
||
183 | */ |
||
184 | public function offsetGet($key) |
||
212 | |||
213 | /** |
||
214 | * Assigns the value to the specified key on this entity. |
||
215 | * |
||
216 | * Routine: |
||
217 | * - The data key is added to the {@see self::$keys entity's key pool}. |
||
218 | * - If the entity has a setter method (e.g., "foo_bar" → `setFooBar()`), |
||
219 | * its called and passed the value; |
||
220 | * - If the entity has NO setter method, the value is assigned to a property (e.g., `$fooBar`). |
||
221 | * |
||
222 | * @see \ArrayAccess |
||
223 | * @param string $key The data key to assign $value to. |
||
224 | * @param mixed $value The data value to assign to $key. |
||
225 | * @throws InvalidArgumentException If the $key is not a string or is a numeric value. |
||
226 | * @return void |
||
227 | */ |
||
228 | public function offsetSet($key, $value) |
||
252 | |||
253 | /** |
||
254 | * Removes the value from the specified key on this entity. |
||
255 | * |
||
256 | * Routine: |
||
257 | * - The data key is removed from the {@see self::$keys entity's key pool}. |
||
258 | * - NULL is {@see self::offsetSet() assigned} to the entity. |
||
259 | * |
||
260 | * @see \ArrayAccess |
||
261 | * @uses self::offsetSet() |
||
262 | * @param string $key The data key to remove. |
||
263 | * @throws InvalidArgumentException If the $key is not a string or is a numeric value. |
||
264 | * @return void |
||
265 | */ |
||
266 | public function offsetUnset($key) |
||
284 | |||
285 | /** |
||
286 | * Gets the data that can be serialized with {@see json_encode()}. |
||
287 | * |
||
288 | * @see \JsonSerializable |
||
289 | * @return array Key-value array of data. |
||
290 | */ |
||
291 | public function jsonSerialize() |
||
295 | |||
296 | /** |
||
297 | * Serializes the data on this entity. |
||
298 | * |
||
299 | * @see \Serializable |
||
300 | * @return string Returns a string containing a byte-stream representation of the object. |
||
301 | */ |
||
302 | public function serialize() |
||
306 | |||
307 | /** |
||
308 | * Applies the serialized data to this entity. |
||
309 | * |
||
310 | * @see \Serializable |
||
311 | * @param string $data The serialized data to extract. |
||
312 | * @return void |
||
313 | */ |
||
314 | public function unserialize($data) |
||
319 | |||
320 | /** |
||
321 | * Transform a string from "snake_case" to "camelCase". |
||
322 | * |
||
323 | * @param string $str The string to camelize. |
||
324 | * @return string The camelized string. |
||
325 | */ |
||
326 | final protected function camelize($str) |
||
333 | } |
||
334 |
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.