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 |
||
40 | class UploadBehavior extends Behavior |
||
41 | { |
||
42 | /** |
||
43 | * @event Event an event that is triggered after a file is uploaded. |
||
44 | */ |
||
45 | const EVENT_AFTER_UPLOAD = 'afterUpload'; |
||
46 | |||
47 | /** |
||
48 | * @var string the attribute which holds the attachment. |
||
49 | */ |
||
50 | public $attribute; |
||
51 | /** |
||
52 | * @var array the scenarios in which the behavior will be triggered |
||
53 | */ |
||
54 | public $scenarios = []; |
||
55 | /** |
||
56 | * @var string the base path or path alias to the directory in which to save files. |
||
57 | */ |
||
58 | public $path; |
||
59 | /** |
||
60 | * @var string the base URL or path alias for this file |
||
61 | */ |
||
62 | public $url; |
||
63 | /** |
||
64 | * @var bool Getting file instance by name |
||
65 | */ |
||
66 | public $instanceByName = false; |
||
67 | /** |
||
68 | * @var boolean|callable generate a new unique name for the file |
||
69 | * set true or anonymous function takes the old filename and returns a new name. |
||
70 | * @see self::generateFileName() |
||
71 | */ |
||
72 | public $generateNewName = true; |
||
73 | /** |
||
74 | * @var boolean If `true` current attribute file will be deleted |
||
75 | */ |
||
76 | public $unlinkOnSave = true; |
||
77 | /** |
||
78 | * @var boolean If `true` current attribute file will be deleted after model deletion. |
||
79 | */ |
||
80 | public $unlinkOnDelete = true; |
||
81 | /** |
||
82 | * @var boolean $deleteTempFile whether to delete the temporary file after saving. |
||
83 | */ |
||
84 | public $deleteTempFile = true; |
||
85 | |||
86 | /** |
||
87 | * @var UploadedFile the uploaded file instance. |
||
88 | */ |
||
89 | private $_file; |
||
90 | |||
91 | |||
92 | /** |
||
93 | * @inheritdoc |
||
94 | */ |
||
95 | 7 | public function init() |
|
109 | |||
110 | /** |
||
111 | * @inheritdoc |
||
112 | */ |
||
113 | 7 | public function events() |
|
124 | |||
125 | /** |
||
126 | * This method is invoked before validation starts. |
||
127 | */ |
||
128 | 4 | public function beforeValidate() |
|
148 | |||
149 | /** |
||
150 | * This method is called at the beginning of inserting or updating a record. |
||
151 | */ |
||
152 | 4 | public function beforeSave() |
|
176 | |||
177 | /** |
||
178 | * This method is called at the end of inserting or updating a record. |
||
179 | * @throws \yii\base\InvalidParamException |
||
180 | */ |
||
181 | 4 | public function afterSave() |
|
193 | |||
194 | /** |
||
195 | * This method is invoked before deleting a record. |
||
196 | */ |
||
197 | 4 | public function beforeDelete() |
|
204 | |||
205 | /** |
||
206 | * Returns file path for the attribute. |
||
207 | * @param string $attribute |
||
208 | * @param boolean $old |
||
209 | * @return string|null the file path. |
||
210 | */ |
||
211 | 4 | public function getUploadPath($attribute, $old = false) |
|
220 | |||
221 | /** |
||
222 | * Returns file url for the attribute. |
||
223 | * @param string $attribute |
||
224 | * @return string|null |
||
225 | */ |
||
226 | public function getUploadUrl($attribute) |
||
235 | |||
236 | /** |
||
237 | * Replaces all placeholders in path variable with corresponding values. |
||
238 | */ |
||
239 | 4 | protected function resolvePath($path) |
|
253 | |||
254 | /** |
||
255 | * Saves the uploaded file. |
||
256 | * @param UploadedFile $file the uploaded file instance |
||
257 | * @param string $path the file path used to save the uploaded file |
||
258 | * @return boolean true whether the file is saved successfully |
||
259 | */ |
||
260 | 4 | protected function save($file, $path) |
|
264 | |||
265 | /** |
||
266 | * Deletes old file. |
||
267 | * @param string $attribute |
||
268 | * @param boolean $old |
||
269 | */ |
||
270 | 1 | protected function delete($attribute, $old = false) |
|
277 | |||
278 | /** |
||
279 | * @param UploadedFile $file |
||
280 | * @return string |
||
281 | */ |
||
282 | 4 | protected function getFileName($file) |
|
292 | |||
293 | /** |
||
294 | * Replaces characters in strings that are illegal/unsafe for filename. |
||
295 | * |
||
296 | * #my* unsaf<e>&file:name?".png |
||
297 | * |
||
298 | * @param string $filename the source filename to be "sanitized" |
||
299 | * @return boolean string the sanitized filename |
||
300 | */ |
||
301 | 1 | public static function sanitize($filename) |
|
305 | |||
306 | /** |
||
307 | * Generates random filename. |
||
308 | * @param UploadedFile $file |
||
309 | * @return string |
||
310 | */ |
||
311 | 4 | protected function generateFileName($file) |
|
315 | |||
316 | /** |
||
317 | * This method is invoked after uploading a file. |
||
318 | * The default implementation raises the [[EVENT_AFTER_UPLOAD]] event. |
||
319 | * You may override this method to do postprocessing after the file is uploaded. |
||
320 | * Make sure you call the parent implementation so that the event is raised properly. |
||
321 | */ |
||
322 | 4 | protected function afterUpload() |
|
326 | } |
||
327 |