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 boolean flag for not generate new filename on import |
||
| 149 | */ |
||
| 150 | private $_import; |
||
| 151 | /** |
||
| 152 | * @var string filename |
||
| 153 | */ |
||
| 154 | private $_temp_file_path; |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * @inheritdoc |
||
| 159 | */ |
||
| 160 | 20 | public function init() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @inheritdoc |
||
| 177 | */ |
||
| 178 | 20 | public function events() |
|
| 179 | { |
||
| 180 | return [ |
||
| 181 | 20 | BaseActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate', |
|
| 182 | BaseActiveRecord::EVENT_AFTER_VALIDATE => 'afterValidate', |
||
| 183 | BaseActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave', |
||
| 184 | BaseActiveRecord::EVENT_BEFORE_UPDATE => 'beforeSave', |
||
| 185 | BaseActiveRecord::EVENT_AFTER_INSERT => 'afterSave', |
||
| 186 | BaseActiveRecord::EVENT_AFTER_UPDATE => 'afterSave', |
||
| 187 | BaseActiveRecord::EVENT_AFTER_DELETE => 'afterDelete', |
||
| 188 | ]; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * This method is invoked before validation starts. |
||
| 193 | */ |
||
| 194 | 12 | public function beforeValidate() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @param UploadedFile $file |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | 12 | protected function getFileName($file) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Generates random filename. |
||
| 232 | * @param UploadedFile $file |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | 1 | protected function generateFileName($file) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Replaces characters in strings that are illegal/unsafe for filename. |
||
| 242 | * |
||
| 243 | * #my* unsaf<e>&file:name?".png |
||
| 244 | * |
||
| 245 | * @param string $filename the source filename to be "sanitized" |
||
| 246 | * @return boolean string the sanitized filename |
||
| 247 | */ |
||
| 248 | 12 | public static function sanitize($filename) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * This method is called at the beginning of inserting or updating a record. |
||
| 255 | */ |
||
| 256 | 11 | public function beforeSave() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Deletes old file. |
||
| 283 | * @param string $attribute |
||
| 284 | * @param boolean $old |
||
| 285 | */ |
||
| 286 | 6 | protected function delete($attribute, $old = false) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Returns file path for the attribute. |
||
| 302 | * |
||
| 303 | * @param string $attribute |
||
| 304 | * @param boolean $old |
||
| 305 | * @return string|null the file path. |
||
| 306 | * @throws \yii\base\InvalidConfigException |
||
| 307 | */ |
||
| 308 | 10 | public function getUploadPath($attribute, $old = false) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Replaces all placeholders in path variable with corresponding values. |
||
| 320 | */ |
||
| 321 | 11 | protected function resolvePath($path) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Delete file from path |
||
| 346 | * @param string $path |
||
| 347 | */ |
||
| 348 | 7 | protected function deleteFile($path) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * This method is called at the end of inserting or updating a record. |
||
| 358 | * @throws \yii\base\Exception |
||
| 359 | */ |
||
| 360 | 11 | public function afterSave() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Saves the uploaded file. |
||
| 378 | * @param UploadedFile $file the uploaded file instance |
||
| 379 | * @param string $path the file path used to save the uploaded file |
||
| 380 | * @return boolean true whether the file is saved successfully |
||
| 381 | */ |
||
| 382 | 10 | protected function save($file, $path) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Remove temp file if exist |
||
| 389 | */ |
||
| 390 | 11 | protected function deleteTempFile() |
|
| 397 | |||
| 398 | /** |
||
| 399 | * This method is invoked after uploading a file. |
||
| 400 | * The default implementation raises the [[EVENT_AFTER_UPLOAD]] event. |
||
| 401 | * You may override this method to do postprocessing after the file is uploaded. |
||
| 402 | * Make sure you call the parent implementation so that the event is raised properly. |
||
| 403 | */ |
||
| 404 | 11 | protected function afterUpload() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * This method is invoked after deleting a record. |
||
| 411 | */ |
||
| 412 | 1 | public function afterDelete() |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Returns file url for the attribute. |
||
| 422 | * |
||
| 423 | * @param string $attribute |
||
| 424 | * @return string|null |
||
| 425 | * @throws \yii\base\InvalidConfigException |
||
| 426 | */ |
||
| 427 | 1 | public function getUploadUrl($attribute) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Set old attribute value if has attribute validation error |
||
| 439 | */ |
||
| 440 | 12 | public function afterValidate() |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Set attribute by filename or file content with auto set file extension and validation by mime type |
||
| 454 | * |
||
| 455 | * @param string $attribute |
||
| 456 | * @param string $filePath |
||
| 457 | * @param string $fileContent |
||
| 458 | * @throws InvalidConfigException |
||
| 459 | * @throws \yii\base\Exception |
||
| 460 | */ |
||
| 461 | 3 | protected function setAttributeByImportFile($attribute, $filePath, $fileContent = null) |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Upload file from url |
||
| 526 | * |
||
| 527 | * @param $attribute string name of attribute with attached UploadBehavior |
||
| 528 | * @param $url string |
||
| 529 | * @throws InvalidConfigException |
||
| 530 | * @throws \yii\base\Exception |
||
| 531 | * @throws \yii\httpclient\Exception |
||
| 532 | */ |
||
| 533 | 2 | public function uploadFromUrl($attribute, $url) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Import file from local storage |
||
| 555 | * |
||
| 556 | * @param $attribute string name of attribute with attached UploadBehavior |
||
| 557 | * @param $filename |
||
| 558 | * @throws InvalidConfigException |
||
| 559 | * @throws \yii\base\Exception |
||
| 560 | */ |
||
| 561 | 3 | public function uploadFromFile($attribute, $filename) |
|
| 574 | } |
||
| 575 |
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.