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 |
||
13 | class Value implements ArrayAccess, ValueWrapper |
||
14 | { |
||
15 | /** |
||
16 | * @var mixed |
||
17 | */ |
||
18 | private $value; |
||
19 | |||
20 | /** |
||
21 | * @return \Closure |
||
22 | */ |
||
23 | public static function getConstructorForValue() |
||
29 | |||
30 | /** |
||
31 | * @param mixed $value |
||
32 | */ |
||
33 | public function __construct($value = null) |
||
37 | |||
38 | /** |
||
39 | * @return mixed |
||
40 | */ |
||
41 | public function __getWrappedValue() |
||
45 | |||
46 | /** |
||
47 | * @param mixed $key |
||
48 | * |
||
49 | * @return static |
||
50 | */ |
||
51 | View Code Duplication | public function offsetGet($key) |
|
59 | |||
60 | /** |
||
61 | * @param mixed $key |
||
62 | * |
||
63 | * @return void |
||
64 | */ |
||
65 | public function offsetUnset($key) |
||
75 | |||
76 | /** |
||
77 | * @param mixed $key |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | public function offsetExists($key) |
||
89 | |||
90 | /** |
||
91 | * @param mixed $key |
||
92 | * @param mixed $value |
||
93 | * |
||
94 | * @return void |
||
95 | */ |
||
96 | View Code Duplication | public function offsetSet($key, $value) |
|
106 | |||
107 | /** |
||
108 | * @param string $field |
||
109 | * |
||
110 | * @return static |
||
111 | */ |
||
112 | public function __get($field) |
||
120 | |||
121 | /** |
||
122 | * @param string $field |
||
123 | * @param mixed $value |
||
124 | * |
||
125 | * @return void Result is always the assigned value. |
||
126 | */ |
||
127 | public function __set($field, $value) |
||
135 | |||
136 | public function __isset($field) |
||
144 | |||
145 | public function __unset($field) |
||
155 | |||
156 | /** |
||
157 | * @param string $name |
||
158 | * @param array $arguments |
||
159 | * |
||
160 | * @return static |
||
161 | */ |
||
162 | public function __call($name, $arguments) |
||
174 | |||
175 | /** |
||
176 | * @return string |
||
177 | */ |
||
178 | public function __toString() |
||
184 | } |
||
185 |
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.