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:
Complex classes like UploadBehavior often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UploadBehavior, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class UploadBehavior extends Behavior |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * Default config. |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $_defaultConfig = [ |
||
19 | 'root' => WWW_ROOT, |
||
20 | 'suffix' => '_file', |
||
21 | 'fields' => [] |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * Overwrite all file on upload. |
||
26 | * |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $_overwrite = true; |
||
30 | |||
31 | /** |
||
32 | * The prefix of the file. |
||
33 | * |
||
34 | * @var bool|string |
||
35 | */ |
||
36 | protected $_prefix = false; |
||
37 | |||
38 | /** |
||
39 | * The default file of the field. |
||
40 | * |
||
41 | * @var bool|string |
||
42 | */ |
||
43 | protected $_defaultFile = false; |
||
44 | |||
45 | /** |
||
46 | * Check if there is some files to upload and modify the entity before |
||
47 | * it is saved. |
||
48 | * |
||
49 | * At the end, for each files to upload, unset their "virtual" property. |
||
50 | * |
||
51 | * @param Event $event The beforeSave event that was fired. |
||
52 | * @param Entity $entity The entity that is going to be saved. |
||
53 | * |
||
54 | * @throws \LogicException When the path configuration is not set. |
||
55 | * @throws \ErrorException When the function to get the upload path failed. |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | public function beforeSave(Event $event, Entity $entity) |
||
109 | |||
110 | /** |
||
111 | * Trigger upload errors. |
||
112 | * |
||
113 | * @param array $file The file to check. |
||
114 | * |
||
115 | * @return string|int|void |
||
116 | */ |
||
117 | protected function _triggerErrors($file) |
||
156 | |||
157 | /** |
||
158 | * Move the temporary source file to the destination file. |
||
159 | * |
||
160 | * @param \Cake\ORM\Entity $entity The entity that is going to be saved. |
||
161 | * @param bool|string $source The temporary source file to copy. |
||
162 | * @param bool|string $destination The destination file to copy. |
||
163 | * @param bool|string $field The current field to process. |
||
164 | * @param array $options The configuration options defined by the user. |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | protected function _moveFile(Entity $entity, $source = false, $destination = false, $field = false, array $options = []) |
||
190 | |||
191 | /** |
||
192 | * Delete the old upload file before to save the new file. |
||
193 | * |
||
194 | * We can not just rely on the copy file with the overwrite, because if you use |
||
195 | * an identifier like :md5 (Who use a different name for each file), the copy |
||
196 | * function will not delete the old file. |
||
197 | * |
||
198 | * @param \Cake\ORM\Entity $entity The entity that is going to be saved. |
||
199 | * @param bool|string $field The current field to process. |
||
200 | * @param bool|string $newFile The new file path. |
||
201 | * @param array $options The configuration options defined by the user. |
||
202 | * |
||
203 | * @return bool |
||
204 | */ |
||
205 | protected function _deleteOldUpload(Entity $entity, $field = false, $newFile = false, array $options = []) |
||
236 | |||
237 | /** |
||
238 | * Get the path formatted without its identifiers to upload the file. |
||
239 | * |
||
240 | * Identifiers : |
||
241 | * :id : Id of the Entity. |
||
242 | * :md5 : A random and unique identifier with 32 characters. |
||
243 | * :y : Based on the current year. |
||
244 | * :m : Based on the current month. |
||
245 | * |
||
246 | * i.e : upload/:id/:md5 -> upload/2/5e3e0d0f163196cb9526d97be1b2ce26.jpg |
||
247 | * |
||
248 | * @param \Cake\ORM\Entity $entity The entity that is going to be saved. |
||
249 | * @param bool|string $path The path to upload the file with its identifiers. |
||
250 | * @param bool|string $extension The extension of the file. |
||
251 | * |
||
252 | * @return bool|string |
||
253 | */ |
||
254 | protected function _getUploadPath(Entity $entity, $path = false, $extension = false) |
||
271 | } |
||
272 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.