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 |
||
10 | class Upload |
||
11 | { |
||
12 | /** @var string|boolean real file path */ |
||
13 | private $filePath; |
||
14 | |||
15 | /** @var string Name of uploaded file */ |
||
16 | private $realName; |
||
17 | |||
18 | /** @var string Generated file name */ |
||
19 | private $fileName; |
||
20 | |||
21 | /** @var string File MIME type */ |
||
22 | private $mimeType; |
||
23 | |||
24 | /** @var string extension */ |
||
25 | private $extension; |
||
26 | |||
27 | /** @var int File size */ |
||
28 | private $size; |
||
29 | |||
30 | /** Supported file extensions */ |
||
31 | protected $extensions = array(); |
||
32 | |||
33 | /** @var array Parameters for callable handlers */ |
||
34 | protected $relPathParameters = array(); |
||
35 | |||
36 | /** @var bool Method of uploading */ |
||
37 | protected $async = true; |
||
38 | |||
39 | /** @var UploadController Pointer to module controller */ |
||
40 | public $config; |
||
41 | |||
42 | /** Upload server path */ |
||
43 | public $uploadDir = 'upload/'; |
||
44 | |||
45 | /** @var iHandler Handler for processing file option requests */ |
||
46 | public $handler; |
||
47 | |||
48 | public $files = array(); |
||
49 | |||
50 | /** |
||
51 | * Init module main fields |
||
52 | * @param array $extensions Allowed file types |
||
53 | * @param null $relPathParameters Parameters for callback functions |
||
54 | */ |
||
55 | protected function initParams($extensions = array(), $relPathParameters = null) |
||
56 | { |
||
57 | // Set additional relative path parameters |
||
58 | $this->relPathParameters = !is_array($relPathParameters) ? array($relPathParameters) : $relPathParameters; |
||
59 | |||
60 | // Set file extension limitations, form array if isn't an array |
||
61 | $this->extensions = is_array($extensions) ? $extensions : array($extensions); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Try to create unique file name using external callback handler |
||
66 | */ |
||
67 | protected function setName() |
||
80 | |||
81 | /** |
||
82 | * Make file uploading |
||
83 | * @param string $postName |
||
84 | * @return bool Upload status |
||
85 | */ |
||
86 | protected function createUpload($postName = '') |
||
113 | |||
114 | /** |
||
115 | * Asynchronous uploading method |
||
116 | * @return bool |
||
117 | */ |
||
118 | View Code Duplication | protected function asyncUploading() |
|
132 | |||
133 | /** |
||
134 | * Synchronous uploading method |
||
135 | * @return bool |
||
136 | */ |
||
137 | protected function syncUploading() |
||
155 | |||
156 | /** |
||
157 | * Add uploaded file to files array |
||
158 | * @param $postName string Name of file in users form |
||
159 | */ |
||
160 | protected function addFile($postName) |
||
169 | |||
170 | /** |
||
171 | * Constructor |
||
172 | * @param mixed $extensions Collection or single excepted extension |
||
173 | * @param mixed $relPathParameters Data to be passed to external rel. path builder |
||
174 | * @param mixed $config External configuration class |
||
175 | */ |
||
176 | public function __construct($extensions = array(), $relPathParameters = null, $config = null, $handler = null) |
||
190 | |||
191 | /** |
||
192 | * Perform file uploading logic |
||
193 | * @param string $filePath Uploaded file path |
||
194 | * @param string $uploadName Uploaded file name real name to return on success upload |
||
195 | * @param string $fileName Uploaded file name on server to return on success upload |
||
196 | * @return boolean True if file successfully uploaded |
||
197 | */ |
||
198 | public function upload(& $filePath = '', & $uploadName = '', & $fileName = '') |
||
210 | |||
211 | public function async($async = true) |
||
216 | |||
217 | /** @return string Full path to file */ |
||
218 | public function path() |
||
222 | |||
223 | /** @return string Full path to file with file name */ |
||
224 | public function fullPath() |
||
228 | |||
229 | /** |
||
230 | * Returns uploaded file name |
||
231 | * @return string File name |
||
232 | */ |
||
233 | public function realName() |
||
237 | |||
238 | /** |
||
239 | * Returns stored file name |
||
240 | * @return string File name |
||
241 | */ |
||
242 | public function name() |
||
246 | |||
247 | /** |
||
248 | * Returns MIME type of uploaded file |
||
249 | * @return string MIME type |
||
250 | */ |
||
251 | public function mimeType() |
||
255 | |||
256 | /** |
||
257 | * If $extension is set, tries to compare file extension to input extension and return a result |
||
258 | * Otherwise returns file extension |
||
259 | * @param string $extension Supposed file extension |
||
260 | * @return bool|string Result of extension comparison or extension by itself. |
||
261 | */ |
||
262 | public function extension($extension = null) |
||
266 | |||
267 | /** |
||
268 | * Returns file size |
||
269 | * @return int File size |
||
270 | */ |
||
271 | public function size() |
||
275 | } |
||
276 |
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.