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 |
||
33 | class ConsoleRequestDataHolder extends RequestDataHolder implements FilesRequestDataHolderInterface |
||
34 | { |
||
35 | /** |
||
36 | * @constant Constant for source name of files. |
||
37 | */ |
||
38 | const SOURCE_FILES = 'files'; |
||
39 | |||
40 | /** |
||
41 | * @var array An array of files uploaded during the request. |
||
42 | */ |
||
43 | protected $files = array(); |
||
44 | |||
45 | /** |
||
46 | * Retrieve an array of file information. |
||
47 | * |
||
48 | * @param string $name A file name. |
||
49 | * @param mixed $default A default return value. |
||
50 | * |
||
51 | * @return mixed An AgaviUploadedFile object with file information, or an |
||
52 | * array if the field name has child elements, or null (or |
||
53 | * the supplied default return value) no such file exists. |
||
54 | * |
||
55 | * @author David Zülke <[email protected]> |
||
56 | * @since 1.0.0 |
||
57 | */ |
||
58 | View Code Duplication | public function &getFile($name, $default = null) |
|
74 | |||
75 | /** |
||
76 | * Retrieve an array of files. |
||
77 | * |
||
78 | * @return array An associative array of files. |
||
79 | * |
||
80 | * @author David Zülke <[email protected]> |
||
81 | * @since 1.0.0 |
||
82 | */ |
||
83 | public function &getFiles() |
||
87 | |||
88 | /** |
||
89 | * Indicates whether or not a file exists. |
||
90 | * |
||
91 | * @param string $name A file name. |
||
92 | * |
||
93 | * @return bool true, if the file exists, otherwise false. |
||
94 | * |
||
95 | * @author David Zülke <[email protected]> |
||
96 | * @since 1.0.0 |
||
97 | */ |
||
98 | View Code Duplication | public function hasFile($name) |
|
111 | |||
112 | /** |
||
113 | * Indicates whether or not any files exist. |
||
114 | * |
||
115 | * @return bool true, if any files exist, otherwise false. |
||
116 | * |
||
117 | * @author David Zülke <[email protected]> |
||
118 | * @since 1.0.0 |
||
119 | */ |
||
120 | public function hasFiles() |
||
124 | |||
125 | /** |
||
126 | * Checks if a file is empty, i.e. not set or set, but not actually uploaded. |
||
127 | * |
||
128 | * @param string $name The file name. |
||
129 | * |
||
130 | * @return bool The result. |
||
131 | * |
||
132 | * @author David Zülke <[email protected]> |
||
133 | * @since 1.0.0 |
||
134 | */ |
||
135 | View Code Duplication | public function isFileValueEmpty($name) |
|
143 | |||
144 | /** |
||
145 | * Removes file information for given file. |
||
146 | * |
||
147 | * @param string $name A file name |
||
148 | * |
||
149 | * @return mixed The old AgaviUploadedFile instance or array of elements. |
||
150 | * |
||
151 | * @author David Zülke <[email protected]> |
||
152 | * @since 1.0.0 |
||
153 | */ |
||
154 | View Code Duplication | public function &removeFile($name) |
|
166 | |||
167 | /** |
||
168 | * Set a file. |
||
169 | * |
||
170 | * If a file with the name already exists the value will be overridden. |
||
171 | * |
||
172 | * @param string $name A file name. |
||
173 | * @param UploadedFile $file An UploadedFile object. |
||
174 | * |
||
175 | * @author David Zülke <[email protected]> |
||
176 | * @since 1.0.0 |
||
177 | */ |
||
178 | public function setFile($name, UploadedFile $file) |
||
182 | |||
183 | /** |
||
184 | * Set an array of files. |
||
185 | * |
||
186 | * @param array $files An assoc array of names and UploadedFile objects. |
||
187 | * |
||
188 | * @author David Zülke <[email protected]> |
||
189 | * @since 1.0.0 |
||
190 | */ |
||
191 | public function setFiles(array $files) |
||
195 | |||
196 | /** |
||
197 | * Clear all files. |
||
198 | * |
||
199 | * @author David Zülke <[email protected]> |
||
200 | * @since 1.0.0 |
||
201 | */ |
||
202 | public function clearFiles() |
||
206 | |||
207 | /** |
||
208 | * Retrieve an array of file names. |
||
209 | * |
||
210 | * @return array An indexed array of file names. |
||
211 | * |
||
212 | * @author David Zülke <[email protected]> |
||
213 | * @since 1.0.0 |
||
214 | */ |
||
215 | public function getFileNames() |
||
219 | |||
220 | /** |
||
221 | * Retrieve an array of flattened file names. This means when a file is an |
||
222 | * array you wont get the name of the file in the result but instead all child |
||
223 | * keys appended to the name (like foo[0],foo[1][0], ...). |
||
224 | * |
||
225 | * @return array An indexed array of file names. |
||
226 | * |
||
227 | * @author David Zülke <[email protected]> |
||
228 | * @since 1.0.0 |
||
229 | */ |
||
230 | public function getFlatFileNames() |
||
234 | |||
235 | /** |
||
236 | * Constructor |
||
237 | * |
||
238 | * @param array $data An associative array of request data source names and |
||
239 | * data arrays. |
||
240 | * |
||
241 | * @author David Zülke <[email protected]> |
||
242 | * @since 1.0.0 |
||
243 | */ |
||
244 | public function __construct(array $data = array()) |
||
251 | |||
252 | /** |
||
253 | * Merge in Files from another request data holder. |
||
254 | * |
||
255 | * @param RequestDataHolder $other The other request data holder. |
||
256 | * |
||
257 | * @author David Zülke <[email protected]> |
||
258 | * @since 1.0.0 |
||
259 | */ |
||
260 | public function mergeFiles(RequestDataHolder $other) |
||
266 | } |
||
267 |
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.