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|callable|array Base path or path alias to the directory in which to save files, |
||
| 58 | * or callable for setting up your custom path generation logic. |
||
| 59 | * If $path is callable, callback signature should be as follow and return a string: |
||
| 60 | * |
||
| 61 | * ```php |
||
| 62 | * function (\yii\db\ActiveRecord $model) |
||
| 63 | * { |
||
| 64 | * // do something... |
||
| 65 | * return $string; |
||
| 66 | * } |
||
| 67 | * ``` |
||
| 68 | * If this property is set up as array, it should be, for example, like as follow ['\app\models\UserProfile', 'buildAvatarPath'], |
||
| 69 | * where first element is class name, while second is its static method that should be called for path generation. |
||
| 70 | * |
||
| 71 | * Example: |
||
| 72 | * ```php |
||
| 73 | * public static function buildAvatarPath(\yii\db\ActiveRecord $model) |
||
| 74 | * { |
||
| 75 | * $basePath = '@webroot/upload/avatars/'; |
||
| 76 | * $suffix = implode('/', array_slice(str_split(md5($model->id), 2), 0, 2)); |
||
| 77 | * return $basePath . $suffix; |
||
| 78 | * } |
||
| 79 | * ``` |
||
| 80 | */ |
||
| 81 | public $path; |
||
| 82 | /** |
||
| 83 | * @var string|callable|array Base URL or path alias for this file, |
||
| 84 | * or callable for setting up your custom URL generation logic. |
||
| 85 | * If $url is callable, callback signature should be as follow and return a string: |
||
| 86 | * |
||
| 87 | * ```php |
||
| 88 | * function (\yii\db\ActiveRecord $model) |
||
| 89 | * { |
||
| 90 | * // do something... |
||
| 91 | * return $string; |
||
| 92 | * } |
||
| 93 | * ``` |
||
| 94 | * If this property is set up as array, it should be, for example, like as follow ['\app\models\UserProfile', 'buildAvatarUrl'], |
||
| 95 | * where first element is class name, while second is its static method that should be called for URL generation. |
||
| 96 | * |
||
| 97 | * Example: |
||
| 98 | * ```php |
||
| 99 | * public static function buildAvatarUrl(\yii\db\ActiveRecord $model) |
||
| 100 | * { |
||
| 101 | * $baseUrl = '@web/upload/avatars/'; |
||
| 102 | * $suffix = implode('/', array_slice(str_split(md5($model->id), 2), 0, 2)); |
||
| 103 | * return $baseUrl . $suffix; |
||
| 104 | * } |
||
| 105 | * ``` |
||
| 106 | */ |
||
| 107 | public $url; |
||
| 108 | /** |
||
| 109 | * @var bool Getting file instance by name |
||
| 110 | */ |
||
| 111 | public $instanceByName = false; |
||
| 112 | /** |
||
| 113 | * @var boolean|callable generate a new unique name for the file |
||
| 114 | * set true or anonymous function takes the old filename and returns a new name. |
||
| 115 | * @see self::generateFileName() |
||
| 116 | */ |
||
| 117 | public $generateNewName = true; |
||
| 118 | /** |
||
| 119 | * @var boolean If `true` current attribute file will be deleted |
||
| 120 | */ |
||
| 121 | public $unlinkOnSave = true; |
||
| 122 | /** |
||
| 123 | * @var boolean If `true` current attribute file will be deleted after model deletion. |
||
| 124 | */ |
||
| 125 | public $unlinkOnDelete = true; |
||
| 126 | /** |
||
| 127 | * @var boolean $deleteTempFile whether to delete the temporary file after saving. |
||
| 128 | */ |
||
| 129 | public $deleteTempFile = true; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var UploadedFile the uploaded file instance. |
||
| 133 | */ |
||
| 134 | private $_file; |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * @inheritdoc |
||
| 139 | */ |
||
| 140 | public function init() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @inheritdoc |
||
| 157 | */ |
||
| 158 | public function events() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * This method is invoked before validation starts. |
||
| 172 | */ |
||
| 173 | public function beforeValidate() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * This method is called at the beginning of inserting or updating a record. |
||
| 196 | */ |
||
| 197 | public function beforeSave() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * This method is called at the end of inserting or updating a record. |
||
| 224 | * @throws \yii\base\Exception |
||
| 225 | */ |
||
| 226 | public function afterSave() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * This method is invoked after deleting a record. |
||
| 243 | */ |
||
| 244 | public function afterDelete() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Returns file path for the attribute. |
||
| 254 | * @param string $attribute |
||
| 255 | * @param boolean $old |
||
| 256 | * @return string|null the file path. |
||
| 257 | * @throws \yii\base\InvalidConfigException |
||
| 258 | */ |
||
| 259 | public function getUploadPath($attribute, $old = false) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Returns file url for the attribute. |
||
| 271 | * @param string $attribute |
||
| 272 | * @return string|null |
||
| 273 | * @throws \yii\base\InvalidConfigException |
||
| 274 | */ |
||
| 275 | public function getUploadUrl($attribute) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns the UploadedFile instance. |
||
| 287 | * @return UploadedFile |
||
| 288 | */ |
||
| 289 | protected function getUploadedFile() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Replaces all placeholders in path variable with corresponding values. |
||
| 296 | */ |
||
| 297 | protected function resolvePath($path) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Saves the uploaded file. |
||
| 322 | * @param UploadedFile $file the uploaded file instance |
||
| 323 | * @param string $path the file path used to save the uploaded file |
||
| 324 | * @return boolean true whether the file is saved successfully |
||
| 325 | */ |
||
| 326 | protected function save($file, $path) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Deletes old file. |
||
| 333 | * @param string $attribute |
||
| 334 | * @param boolean $old |
||
| 335 | */ |
||
| 336 | protected function delete($attribute, $old = false) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param UploadedFile $file |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | protected function getFileName($file) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Replaces characters in strings that are illegal/unsafe for filename. |
||
| 361 | * |
||
| 362 | * #my* unsaf<e>&file:name?".png |
||
| 363 | * |
||
| 364 | * @param string $filename the source filename to be "sanitized" |
||
| 365 | * @return boolean string the sanitized filename |
||
| 366 | */ |
||
| 367 | public static function sanitize($filename) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Generates random filename. |
||
| 374 | * @param UploadedFile $file |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | protected function generateFileName($file) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * This method is invoked after uploading a file. |
||
| 384 | * The default implementation raises the [[EVENT_AFTER_UPLOAD]] event. |
||
| 385 | * You may override this method to do postprocessing after the file is uploaded. |
||
| 386 | * Make sure you call the parent implementation so that the event is raised properly. |
||
| 387 | */ |
||
| 388 | protected function afterUpload() |
||
| 392 | } |
||
| 393 |