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 | /** | ||
| 54 | * @var array the scenarios in which the behavior will be triggered | ||
| 55 | */ | ||
| 56 | public $scenarios = []; | ||
| 57 | |||
| 58 | /** | ||
| 59 | * @var string the base path or path alias to the directory in which to save files. | ||
| 60 | */ | ||
| 61 | public $path; | ||
| 62 | |||
| 63 | /** | ||
| 64 | * @var string the base URL or path alias for this file | ||
| 65 | */ | ||
| 66 | public $url; | ||
| 67 | |||
| 68 | /** | ||
| 69 | * @var bool Getting file instance by name | ||
| 70 | */ | ||
| 71 | public $instanceByName = false; | ||
| 72 | |||
| 73 | /** | ||
| 74 | * @var boolean|callable generate a new unique name for the file | ||
| 75 | * set true or anonymous function takes the old filename and returns a new name. | ||
| 76 | * @see self::generateFileName() | ||
| 77 | */ | ||
| 78 | public $generateNewName = true; | ||
| 79 | |||
| 80 | /** | ||
| 81 | * @var boolean If `true` current attribute file will be deleted | ||
| 82 | */ | ||
| 83 | public $unlinkOnSave = true; | ||
| 84 | |||
| 85 | /** | ||
| 86 | * @var boolean If `true` current attribute file will be deleted after model deletion. | ||
| 87 | */ | ||
| 88 | public $unlinkOnDelete = true; | ||
| 89 | |||
| 90 | /** | ||
| 91 | * @var boolean $deleteTempFile whether to delete the temporary file after saving. | ||
| 92 | */ | ||
| 93 | public $deleteTempFile = true; | ||
| 94 | |||
| 95 | /** | ||
| 96 | * @var UploadedFile the uploaded file instance. | ||
| 97 | */ | ||
| 98 | private $_file; | ||
| 99 | |||
| 100 | /** | ||
| 101 | * @inheritdoc | ||
| 102 | * @throws InvalidConfigException | ||
| 103 | */ | ||
| 104 | public function init() | ||
| 118 | |||
| 119 | /** | ||
| 120 | * @inheritdoc | ||
| 121 | */ | ||
| 122 | public function events() | ||
| 133 | |||
| 134 | /** | ||
| 135 | * This method is invoked before validation starts. | ||
| 136 | */ | ||
| 137 | public function beforeValidate() | ||
| 157 | |||
| 158 | /** | ||
| 159 | * This method is called at the beginning of inserting or updating a record. | ||
| 160 | */ | ||
| 161 | public function beforeSave() | ||
| 185 | |||
| 186 | /** | ||
| 187 | * This method is called at the end of inserting or updating a record. | ||
| 188 | * @throws \yii\base\InvalidArgumentException | ||
| 189 | * @throws \yii\base\Exception | ||
| 190 | */ | ||
| 191 | public function afterSave() | ||
| 205 | |||
| 206 | /** | ||
| 207 | * This method is invoked after deleting a record. | ||
| 208 | */ | ||
| 209 | public function afterDelete() | ||
| 216 | |||
| 217 | /** | ||
| 218 | * Returns file path for the attribute. | ||
| 219 | * @param string $attribute | ||
| 220 | * @param boolean $old | ||
| 221 | * @return string|null the file path. | ||
| 222 | */ | ||
| 223 | public function getUploadPath($attribute, $old = false) | ||
| 232 | |||
| 233 | /** | ||
| 234 | * Returns file url for the attribute. | ||
| 235 | * @param string $attribute | ||
| 236 | * @return string|null | ||
| 237 | */ | ||
| 238 | public function getUploadUrl($attribute) | ||
| 247 | |||
| 248 | /** | ||
| 249 | * Returns the UploadedFile instance. | ||
| 250 | * @return UploadedFile | ||
| 251 | */ | ||
| 252 | protected function getUploadedFile() | ||
| 256 | |||
| 257 | /** | ||
| 258 | * Replaces all placeholders in path variable with corresponding values. | ||
| 259 | */ | ||
| 260 | protected function resolvePath($path) | ||
| 274 | |||
| 275 | /** | ||
| 276 | * Saves the uploaded file. | ||
| 277 | * @param UploadedFile $file the uploaded file instance | ||
| 278 | * @param string $path the file path used to save the uploaded file | ||
| 279 | * @return boolean true whether the file is saved successfully | ||
| 280 | */ | ||
| 281 | protected function save($file, $path) | ||
| 285 | |||
| 286 | /** | ||
| 287 | * Deletes old file. | ||
| 288 | * @param string $attribute | ||
| 289 | * @param boolean $old | ||
| 290 | */ | ||
| 291 | protected function delete($attribute, $old = false) | ||
| 298 | |||
| 299 | /** | ||
| 300 | * @param UploadedFile $file | ||
| 301 | * @return string | ||
| 302 | */ | ||
| 303 | protected function getFileName($file) | ||
| 313 | |||
| 314 | /** | ||
| 315 | * Replaces characters in strings that are illegal/unsafe for filename. | ||
| 316 | * | ||
| 317 | * #my* unsaf<e>&file:name?".png | ||
| 318 | * | ||
| 319 | * @param string $filename the source filename to be "sanitized" | ||
| 320 | * @return boolean string the sanitized filename | ||
| 321 | */ | ||
| 322 | public static function sanitize($filename) | ||
| 326 | |||
| 327 | /** | ||
| 328 | * Generates random filename. | ||
| 329 | * @param UploadedFile $file | ||
| 330 | * @return string | ||
| 331 | */ | ||
| 332 | protected function generateFileName($file) | ||
| 336 | |||
| 337 | /** | ||
| 338 | * This method is invoked after uploading a file. | ||
| 339 | * The default implementation raises the [[EVENT_AFTER_UPLOAD]] event. | ||
| 340 | * You may override this method to do postprocessing after the file is uploaded. | ||
| 341 | * Make sure you call the parent implementation so that the event is raised properly. | ||
| 342 | */ | ||
| 343 | protected function afterUpload() | ||
| 347 | } | ||
| 348 |