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 Set extends AbstractCollection implements \ArrayAccess |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * Whether a offset exists |
||
23 | * |
||
24 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
25 | * |
||
26 | * @param mixed $offset <p> |
||
27 | * An offset to check for. |
||
28 | * </p> |
||
29 | * |
||
30 | * @return boolean true on success or false on failure. |
||
31 | * </p> |
||
32 | * <p> |
||
33 | * The return value will be casted to boolean if non-boolean was returned. |
||
34 | * @since 5.0.0 |
||
35 | */ |
||
36 | public function offsetExists($offset) |
||
40 | |||
41 | /** |
||
42 | * Offset to retrieve |
||
43 | * |
||
44 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
45 | * |
||
46 | * @param mixed $offset <p> |
||
47 | * The offset to retrieve. |
||
48 | * </p> |
||
49 | * |
||
50 | * @return mixed Can return all value types. |
||
51 | * @since 5.0.0 |
||
52 | */ |
||
53 | public function offsetGet($offset) |
||
57 | |||
58 | /** |
||
59 | * Offset to set |
||
60 | * |
||
61 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
62 | * |
||
63 | * @param mixed $offset The offset to assign the value to. |
||
64 | * @param mixed $value The value to set. |
||
65 | * |
||
66 | * @throws NoDuplicateAllowedException When try to add an object already added |
||
67 | * @since 5.0.0 |
||
68 | */ |
||
69 | public function offsetSet($offset, $value) |
||
81 | |||
82 | /** |
||
83 | * Offset to unset |
||
84 | * |
||
85 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
86 | * |
||
87 | * @param mixed $offset <p> |
||
88 | * The offset to unset. |
||
89 | * </p> |
||
90 | * |
||
91 | * @return void |
||
92 | * @since 5.0.0 |
||
93 | */ |
||
94 | public function offsetUnset($offset) |
||
98 | } |
||
99 |
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.