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 |
||
42 | class UploadBehavior extends Behavior |
||
43 | { |
||
44 | /** |
||
45 | * @event Event an event that is triggered after a file is uploaded. |
||
46 | */ |
||
47 | const EVENT_AFTER_UPLOAD = 'afterUpload'; |
||
48 | |||
49 | /** |
||
50 | * @var string the attribute which holds the attachment. |
||
51 | */ |
||
52 | public $attribute; |
||
53 | /** |
||
54 | * @var array the scenarios in which the behavior will be triggered |
||
55 | */ |
||
56 | public $scenarios = []; |
||
57 | /** |
||
58 | * @var string|callable|array Base path or path alias to the directory in which to save files, |
||
59 | * or callable for setting up your custom path generation logic. |
||
60 | * If $path is callable, callback signature should be as follow and return a string: |
||
61 | * |
||
62 | * ```php |
||
63 | * function (\yii\db\ActiveRecord $model) |
||
64 | * { |
||
65 | * // do something... |
||
66 | * return $string; |
||
67 | * } |
||
68 | * ``` |
||
69 | * If this property is set up as array, it should be, for example, like as follow ['\app\models\UserProfile', 'buildAvatarPath'], |
||
70 | * where first element is class name, while second is its static method that should be called for path generation. |
||
71 | * |
||
72 | * Example: |
||
73 | * ```php |
||
74 | * public static function buildAvatarPath(\yii\db\ActiveRecord $model) |
||
75 | * { |
||
76 | * $basePath = '@webroot/upload/avatars/'; |
||
77 | * $suffix = implode('/', array_slice(str_split(md5($model->id), 2), 0, 2)); |
||
78 | * return $basePath . $suffix; |
||
79 | * } |
||
80 | * ``` |
||
81 | */ |
||
82 | public $path; |
||
83 | /** |
||
84 | * @var string|callable|array Base URL or path alias for this file, |
||
85 | * or callable for setting up your custom URL generation logic. |
||
86 | * If $url is callable, callback signature should be as follow and return a string: |
||
87 | * |
||
88 | * ```php |
||
89 | * function (\yii\db\ActiveRecord $model) |
||
90 | * { |
||
91 | * // do something... |
||
92 | * return $string; |
||
93 | * } |
||
94 | * ``` |
||
95 | * If this property is set up as array, it should be, for example, like as follow ['\app\models\UserProfile', 'buildAvatarUrl'], |
||
96 | * where first element is class name, while second is its static method that should be called for URL generation. |
||
97 | * |
||
98 | * Example: |
||
99 | * ```php |
||
100 | * public static function buildAvatarUrl(\yii\db\ActiveRecord $model) |
||
101 | * { |
||
102 | * $baseUrl = '@web/upload/avatars/'; |
||
103 | * $suffix = implode('/', array_slice(str_split(md5($model->id), 2), 0, 2)); |
||
104 | * return $baseUrl . $suffix; |
||
105 | * } |
||
106 | * ``` |
||
107 | */ |
||
108 | public $url; |
||
109 | /** |
||
110 | * @var bool Getting file instance by name |
||
111 | */ |
||
112 | public $instanceByName = false; |
||
113 | /** |
||
114 | * @var boolean|callable generate a new unique name for the file |
||
115 | * set true or anonymous function takes the old filename and returns a new name. |
||
116 | * @see self::generateFileName() |
||
117 | */ |
||
118 | public $generateNewName = true; |
||
119 | /** |
||
120 | * @var boolean If `true` current attribute file will be deleted |
||
121 | */ |
||
122 | public $unlinkOnSave = true; |
||
123 | /** |
||
124 | * @var boolean If `true` current attribute file will be deleted after model deletion. |
||
125 | */ |
||
126 | public $unlinkOnDelete = true; |
||
127 | /** |
||
128 | * @var boolean $deleteTempFile whether to delete the temporary file after saving. |
||
129 | */ |
||
130 | public $deleteTempFile = true; |
||
131 | /** |
||
132 | * @var boolean $deleteEmptyDir whether to delete the empty directory after model deletion. |
||
133 | */ |
||
134 | public $deleteEmptyDir = true; |
||
135 | /** |
||
136 | * @var bool restore old value after fail attribute validation |
||
137 | */ |
||
138 | public $restoreValueAfterFailValidation = true; |
||
139 | /** |
||
140 | * @var string temporary folder name |
||
141 | */ |
||
142 | public $tempFolder = '@runtime/'; |
||
143 | /** |
||
144 | * @var UploadedFile the uploaded file instance. |
||
145 | */ |
||
146 | private $_file; |
||
147 | /** |
||
148 | * @var import flag for not generate new filename on import |
||
149 | */ |
||
150 | private $_import; |
||
151 | /** |
||
152 | * @var temporary filename |
||
153 | */ |
||
154 | private $_temp_file_path; |
||
155 | |||
156 | |||
157 | /** |
||
158 | * @inheritdoc |
||
159 | */ |
||
160 | 17 | public function init() |
|
174 | |||
175 | /** |
||
176 | * @inheritdoc |
||
177 | */ |
||
178 | 17 | public function events() |
|
190 | |||
191 | /** |
||
192 | * This method is invoked before validation starts. |
||
193 | */ |
||
194 | 10 | public function beforeValidate() |
|
214 | |||
215 | /** |
||
216 | * This method is called at the beginning of inserting or updating a record. |
||
217 | */ |
||
218 | 9 | public function beforeSave() |
|
242 | |||
243 | /** |
||
244 | * This method is called at the end of inserting or updating a record. |
||
245 | * @throws \yii\base\Exception |
||
246 | */ |
||
247 | 9 | public function afterSave() |
|
262 | |||
263 | /** |
||
264 | * This method is invoked after deleting a record. |
||
265 | */ |
||
266 | 1 | public function afterDelete() |
|
273 | |||
274 | /** |
||
275 | * Returns file path for the attribute. |
||
276 | * @param string $attribute |
||
277 | * @param boolean $old |
||
278 | * @return string|null the file path. |
||
279 | * @throws \yii\base\InvalidConfigException |
||
280 | */ |
||
281 | 8 | public function getUploadPath($attribute, $old = false) |
|
290 | |||
291 | /** |
||
292 | * Returns file url for the attribute. |
||
293 | * @param string $attribute |
||
294 | * @return string|null |
||
295 | * @throws \yii\base\InvalidConfigException |
||
296 | */ |
||
297 | 1 | public function getUploadUrl($attribute) |
|
298 | { |
||
299 | /** @var BaseActiveRecord $model */ |
||
300 | 1 | $model = $this->owner; |
|
301 | 1 | $url = $this->resolvePath($this->url); |
|
302 | 1 | $fileName = $model->getOldAttribute($attribute); |
|
303 | |||
304 | 1 | return $fileName ? Yii::getAlias($url . '/' . $fileName) : null; |
|
305 | } |
||
306 | |||
307 | /** |
||
308 | * Returns the UploadedFile instance. |
||
309 | * @return UploadedFile |
||
310 | */ |
||
311 | protected function getUploadedFile() |
||
315 | |||
316 | /** |
||
317 | * Replaces all placeholders in path variable with corresponding values. |
||
318 | */ |
||
319 | 9 | protected function resolvePath($path) |
|
341 | |||
342 | /** |
||
343 | * Saves the uploaded file. |
||
344 | * @param UploadedFile $file the uploaded file instance |
||
345 | * @param string $path the file path used to save the uploaded file |
||
346 | * @return boolean true whether the file is saved successfully |
||
347 | */ |
||
348 | 8 | protected function save($file, $path) |
|
352 | |||
353 | /** |
||
354 | * Deletes old file. |
||
355 | * @param string $attribute |
||
356 | * @param boolean $old |
||
357 | */ |
||
358 | 4 | protected function delete($attribute, $old = false) |
|
371 | |||
372 | /** |
||
373 | * @param UploadedFile $file |
||
374 | * @return string |
||
375 | */ |
||
376 | 10 | protected function getFileName($file) |
|
386 | |||
387 | /** |
||
388 | * Replaces characters in strings that are illegal/unsafe for filename. |
||
389 | * |
||
390 | * #my* unsaf<e>&file:name?".png |
||
391 | * |
||
392 | * @param string $filename the source filename to be "sanitized" |
||
393 | * @return boolean string the sanitized filename |
||
394 | */ |
||
395 | 10 | public static function sanitize($filename) |
|
399 | |||
400 | /** |
||
401 | * Generates random filename. |
||
402 | * @param UploadedFile $file |
||
403 | * @return string |
||
404 | */ |
||
405 | 1 | protected function generateFileName($file) |
|
409 | |||
410 | /** |
||
411 | * This method is invoked after uploading a file. |
||
412 | * The default implementation raises the [[EVENT_AFTER_UPLOAD]] event. |
||
413 | * You may override this method to do postprocessing after the file is uploaded. |
||
414 | * Make sure you call the parent implementation so that the event is raised properly. |
||
415 | */ |
||
416 | 8 | protected function afterUpload() |
|
420 | |||
421 | /** |
||
422 | * Delete file from path |
||
423 | * @param string $path |
||
424 | */ |
||
425 | 5 | protected function deleteFile($path) |
|
432 | |||
433 | /** |
||
434 | * Set old attribute value if has attribute validation error |
||
435 | */ |
||
436 | 10 | public function afterValidate() |
|
446 | |||
447 | /** |
||
448 | * Upload file from url |
||
449 | * |
||
450 | * @param $attribute string name of attribute with attached UploadBehavior |
||
451 | * @param $url string |
||
452 | * @throws InvalidConfigException |
||
453 | * @throws \yii\base\Exception |
||
454 | * @throws \yii\httpclient\Exception |
||
455 | */ |
||
456 | 2 | public function uploadFromUrl($attribute, $url) { |
|
477 | |||
478 | /** |
||
479 | * Upload file from local storage |
||
480 | * |
||
481 | * @param $attribute string name of attribute with attached UploadBehavior |
||
482 | * @param $filename |
||
483 | * @throws InvalidConfigException |
||
484 | * @throws \yii\base\Exception |
||
485 | */ |
||
486 | 3 | public function uploadFromFile($attribute, $filename) { |
|
500 | |||
501 | /** |
||
502 | * @param $attribute |
||
503 | * @param $url |
||
504 | * @param string $fileContent |
||
505 | * @throws InvalidConfigException |
||
506 | * @throws \yii\base\Exception |
||
507 | */ |
||
508 | 3 | protected function setAttributeFile($attribute, $filePath, $fileContent = null) |
|
571 | |||
572 | /** |
||
573 | * remove temp file |
||
574 | */ |
||
575 | 9 | protected function deleteTempFile() |
|
582 | } |
||
583 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.