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 |
||
37 | class RequestDataHolder extends ParameterHolder implements ParametersRequestDataHolderInterface |
||
38 | { |
||
39 | /** |
||
40 | * @constant Constant for source name of parameters. |
||
41 | */ |
||
42 | const SOURCE_PARAMETERS = 'parameters'; |
||
43 | |||
44 | /* |
||
45 | * @var array An array of source names and references to their data |
||
46 | * containers. Unset again after construction is complete. |
||
47 | */ |
||
48 | private $sources = array(); |
||
49 | |||
50 | /* |
||
51 | * @var array An array of plural source names and their singular forms. |
||
52 | */ |
||
53 | private $sourceNames = array(); |
||
54 | |||
55 | /** |
||
56 | * Merge in parameters from another request data holder. |
||
57 | * |
||
58 | * @param RequestDataHolder $other The other request data holder. |
||
59 | * |
||
60 | * @author David Zülke <[email protected]> |
||
61 | * @since 0.11.0 |
||
62 | */ |
||
63 | public function mergeParameters(RequestDataHolder $other) |
||
67 | |||
68 | /** |
||
69 | * Checks if there is a value of a parameter is empty. |
||
70 | * |
||
71 | * @param string $field The field name. |
||
72 | * |
||
73 | * @return bool The result. |
||
74 | * |
||
75 | * @author Dominik del Bondio <[email protected]> |
||
76 | * @since 0.11.0 |
||
77 | */ |
||
78 | public function isParameterValueEmpty($field) |
||
82 | |||
83 | /** |
||
84 | * Deletes all fields in a given source. |
||
85 | * |
||
86 | * @param string $source The name of the source to operate on. |
||
87 | * |
||
88 | * @author Dominik del Bondio <[email protected]> |
||
89 | * @since 0.11.0 |
||
90 | */ |
||
91 | public function clear($source) |
||
100 | |||
101 | /** |
||
102 | * Deletes all fields in all sources. |
||
103 | * |
||
104 | * @author Dominik del Bondio <[email protected]> |
||
105 | * @since 0.11.0 |
||
106 | */ |
||
107 | public function clearAll() |
||
114 | |||
115 | /** |
||
116 | * Retrieves a field from one of the stored data types. |
||
117 | * |
||
118 | * @param string $source The name of the source to operate on. |
||
119 | * @param string $field A field name. |
||
120 | * @param mixed $default A default value. |
||
121 | * |
||
122 | * @return mixed The field value. |
||
123 | * |
||
124 | * @author Dominik del Bondio <[email protected]> |
||
125 | * @author David Zülke <[email protected]> |
||
126 | * @since 0.11.0 |
||
127 | */ |
||
128 | public function &get($source, $field, $default = null) |
||
137 | |||
138 | /** |
||
139 | * Retrieves all fields of a stored data types. |
||
140 | * |
||
141 | * @param string $source The name of the source to operate on. |
||
142 | * |
||
143 | * @return mixed The values. |
||
144 | * |
||
145 | * @author Dominik del Bondio <[email protected]> |
||
146 | * @author David Zülke <[email protected]> |
||
147 | * @since 0.11.0 |
||
148 | */ |
||
149 | public function &getAll($source) |
||
158 | |||
159 | /** |
||
160 | * Checks if a field exists. |
||
161 | * |
||
162 | * @param string $source The name of the source to operate on. |
||
163 | * @param string $field A field name. |
||
164 | * |
||
165 | * @return bool The result. |
||
166 | * |
||
167 | * @author Dominik del Bondio <[email protected]> |
||
168 | * @author David Zülke <[email protected]> |
||
169 | * @since 0.11.0 |
||
170 | */ |
||
171 | View Code Duplication | public function has($source, $field) |
|
180 | |||
181 | /** |
||
182 | * Checks if a field has no value (In web context this would only return true |
||
183 | * when the strings length is 0 or the field is not set. |
||
184 | * |
||
185 | * @param string $source The name of the source to operate on. |
||
186 | * @param string $field A field name. |
||
187 | * |
||
188 | * @return bool The result. |
||
189 | * |
||
190 | * @author Dominik del Bondio <[email protected]> |
||
191 | * @author David Zülke <[email protected]> |
||
192 | * @since 0.11.0 |
||
193 | */ |
||
194 | View Code Duplication | public function isValueEmpty($source, $field) |
|
203 | |||
204 | /** |
||
205 | * Removes a field. |
||
206 | * |
||
207 | * @param string $source The name of the source to operate on. |
||
208 | * @param string $field A field name. |
||
209 | * |
||
210 | * @return mixed The removed value. |
||
211 | * |
||
212 | * @author Dominik del Bondio <[email protected]> |
||
213 | * @author David Zülke <[email protected]> |
||
214 | * @since 0.11.0 |
||
215 | */ |
||
216 | View Code Duplication | public function &remove($source, $field) |
|
225 | |||
226 | /** |
||
227 | * Sets a field. |
||
228 | * |
||
229 | * @param string $source The name of the source to operate on. |
||
230 | * @param string $field A field name. |
||
231 | * @param mixed $value A value. |
||
232 | * |
||
233 | * @author Dominik del Bondio <[email protected]> |
||
234 | * @author David Zülke <[email protected]> |
||
235 | * @since 0.11.0 |
||
236 | */ |
||
237 | public function set($source, $field, $value) |
||
246 | |||
247 | /** |
||
248 | * Register a source with the holder. Must be called in constructors, and |
||
249 | * prior to calling the parent ctor. |
||
250 | * |
||
251 | * @param string $name The source name, typically passed using a constant. |
||
252 | * @param array $holder The variable that will hold the data for the source. |
||
253 | * |
||
254 | * @author David Zülke <[email protected]> |
||
255 | * @since 0.11.0 |
||
256 | */ |
||
257 | final protected function registerSource($name, array &$holder) |
||
262 | |||
263 | /** |
||
264 | * Merge in another request data holder. |
||
265 | * |
||
266 | * This method calls mergeSourcename for each source. |
||
267 | * |
||
268 | * @param RequestDataHolder $other The other request data holder. |
||
269 | * |
||
270 | * @author David Zülke <[email protected]> |
||
271 | * @since 0.11.0 |
||
272 | */ |
||
273 | public function merge(RequestDataHolder $other) |
||
280 | |||
281 | /** |
||
282 | * Returns all the registered source names. |
||
283 | * |
||
284 | * @return array A list of source names. |
||
285 | * |
||
286 | * @author Dominik del Bondio <[email protected]> |
||
287 | * @since 0.11.0 |
||
288 | */ |
||
289 | final public function getSourceNames() |
||
293 | |||
294 | /** |
||
295 | * Constructor |
||
296 | * |
||
297 | * @param array $data An associative array of request data source names and |
||
298 | * data arrays. |
||
299 | * |
||
300 | * @author David Zülke <[email protected]> |
||
301 | * @since 0.11.0 |
||
302 | */ |
||
303 | public function __construct(array $data = array()) |
||
318 | } |
||
319 |
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.