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 |
||
8 | trait ArrayAccessTrait |
||
9 | { |
||
10 | /** |
||
11 | * Returns true when a key exists of the same type and value as $OFFSET |
||
12 | * |
||
13 | * @param mixed $offset |
||
14 | * @return bool |
||
15 | */ |
||
16 | 1 | View Code Duplication | public function offsetExists($offset) |
27 | |||
28 | /** |
||
29 | * Returns the value of a key with the same type and value as $OFFSET, or returns |
||
30 | * $DEFAULT when it this key does not exist |
||
31 | * |
||
32 | * @param mixed $offset |
||
33 | * @param mixed $default |
||
34 | * @return mixed |
||
35 | */ |
||
36 | 2 | View Code Duplication | public function offsetGet($offset, $default = null) |
47 | |||
48 | /** |
||
49 | * Not implemented |
||
50 | * |
||
51 | * @param mixed $offset |
||
52 | * @param mixed $value |
||
53 | */ |
||
54 | 1 | public function offsetSet($offset, $value) |
|
58 | |||
59 | /** |
||
60 | * Not implemented |
||
61 | * |
||
62 | * @param mixed $offset |
||
63 | */ |
||
64 | 1 | public function offsetUnset($offset) |
|
68 | } |
||
69 |
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.