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 |
||
41 | class UploadBehavior extends Behavior |
||
42 | { |
||
43 | /** |
||
44 | * @event Event an event that is triggered after a file is uploaded. |
||
45 | */ |
||
46 | const EVENT_AFTER_UPLOAD = 'afterUpload'; |
||
47 | |||
48 | /** |
||
49 | * @var string the attribute which holds the attachment. |
||
50 | */ |
||
51 | public $attribute; |
||
52 | /** |
||
53 | * @var array the scenarios in which the behavior will be triggered |
||
54 | */ |
||
55 | public $scenarios = []; |
||
56 | /** |
||
57 | * @var string the base path or path alias to the directory in which to save files. |
||
58 | */ |
||
59 | public $path; |
||
60 | /** |
||
61 | * @var string the base URL or path alias for this file |
||
62 | */ |
||
63 | public $url; |
||
64 | /** |
||
65 | * @var bool Getting file instance by name |
||
66 | */ |
||
67 | public $instanceByName = false; |
||
68 | /** |
||
69 | * @var boolean|callable generate a new unique name for the file |
||
70 | * set true or anonymous function takes the old filename and returns a new name. |
||
71 | * @see self::generateFileName() |
||
72 | */ |
||
73 | public $generateNewName = true; |
||
74 | /** |
||
75 | * @var boolean If `true` current attribute file will be deleted |
||
76 | */ |
||
77 | public $unlinkOnSave = true; |
||
78 | /** |
||
79 | * @var boolean If `true` current attribute file will be deleted after model deletion. |
||
80 | */ |
||
81 | public $unlinkOnDelete = true; |
||
82 | /** |
||
83 | * @var boolean $deleteTempFile whether to delete the temporary file after saving. |
||
84 | */ |
||
85 | public $deleteTempFile = true; |
||
86 | /** |
||
87 | * @var boolean $deleteEmptyDir whether to delete the empty directory after model deletion. |
||
88 | */ |
||
89 | public $deleteEmptyDir = false; |
||
90 | |||
91 | /** |
||
92 | * @var UploadedFile the uploaded file instance. |
||
93 | */ |
||
94 | private $_file; |
||
95 | |||
96 | |||
97 | /** |
||
98 | * @inheritdoc |
||
99 | */ |
||
100 | public function init() |
||
114 | |||
115 | /** |
||
116 | * @inheritdoc |
||
117 | */ |
||
118 | public function events() |
||
129 | |||
130 | /** |
||
131 | * This method is invoked before validation starts. |
||
132 | */ |
||
133 | public function beforeValidate() |
||
153 | |||
154 | /** |
||
155 | * This method is called at the beginning of inserting or updating a record. |
||
156 | */ |
||
157 | public function beforeSave() |
||
181 | |||
182 | /** |
||
183 | * This method is called at the end of inserting or updating a record. |
||
184 | * @throws \yii\base\InvalidArgumentException |
||
185 | */ |
||
186 | public function afterSave() |
||
198 | |||
199 | /** |
||
200 | * This method is invoked after deleting a record. |
||
201 | */ |
||
202 | public function afterDelete() |
||
209 | |||
210 | /** |
||
211 | * Returns file path for the attribute. |
||
212 | * @param string $attribute |
||
213 | * @param boolean $old |
||
214 | * @return string|null the file path. |
||
215 | */ |
||
216 | public function getUploadPath($attribute, $old = false) |
||
225 | |||
226 | /** |
||
227 | * Returns file url for the attribute. |
||
228 | * @param string $attribute |
||
229 | * @return string|null |
||
230 | */ |
||
231 | public function getUploadUrl($attribute) |
||
240 | |||
241 | /** |
||
242 | * Returns the UploadedFile instance. |
||
243 | * @return UploadedFile |
||
244 | */ |
||
245 | protected function getUploadedFile() |
||
249 | |||
250 | /** |
||
251 | * Replaces all placeholders in path variable with corresponding values. |
||
252 | */ |
||
253 | protected function resolvePath($path) |
||
267 | |||
268 | /** |
||
269 | * Saves the uploaded file. |
||
270 | * @param UploadedFile $file the uploaded file instance |
||
271 | * @param string $path the file path used to save the uploaded file |
||
272 | * @return boolean true whether the file is saved successfully |
||
273 | */ |
||
274 | protected function save($file, $path) |
||
278 | |||
279 | /** |
||
280 | * Deletes old file. |
||
281 | * @param string $attribute |
||
282 | * @param boolean $old |
||
283 | */ |
||
284 | protected function delete($attribute, $old = false) |
||
300 | |||
301 | /** |
||
302 | * @param UploadedFile $file |
||
303 | * @return string |
||
304 | */ |
||
305 | protected function getFileName($file) |
||
315 | |||
316 | /** |
||
317 | * Replaces characters in strings that are illegal/unsafe for filename. |
||
318 | * |
||
319 | * #my* unsaf<e>&file:name?".png |
||
320 | * |
||
321 | * @param string $filename the source filename to be "sanitized" |
||
322 | * @return boolean string the sanitized filename |
||
323 | */ |
||
324 | public static function sanitize($filename) |
||
328 | |||
329 | /** |
||
330 | * Generates random filename. |
||
331 | * @param UploadedFile $file |
||
332 | * @return string |
||
333 | */ |
||
334 | protected function generateFileName($file) |
||
338 | |||
339 | /** |
||
340 | * This method is invoked after uploading a file. |
||
341 | * The default implementation raises the [[EVENT_AFTER_UPLOAD]] event. |
||
342 | * You may override this method to do postprocessing after the file is uploaded. |
||
343 | * Make sure you call the parent implementation so that the event is raised properly. |
||
344 | */ |
||
345 | protected function afterUpload() |
||
349 | } |
||
350 |