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 | /** |
||
| 88 | * @var UploadedFile the uploaded file instance. |
||
| 89 | */ |
||
| 90 | private $_file; |
||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * @inheritdoc |
||
| 95 | */ |
||
| 96 | public function init() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @inheritdoc |
||
| 113 | */ |
||
| 114 | public function events() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * This method is invoked before validation starts. |
||
| 128 | */ |
||
| 129 | public function beforeValidate() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * This method is called at the beginning of inserting or updating a record. |
||
| 152 | */ |
||
| 153 | public function beforeSave() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * This method is called at the end of inserting or updating a record. |
||
| 180 | * @throws \yii\base\InvalidArgumentException |
||
| 181 | */ |
||
| 182 | public function afterSave() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * This method is invoked after deleting a record. |
||
| 199 | */ |
||
| 200 | public function afterDelete() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Returns file path for the attribute. |
||
| 210 | * @param string $attribute |
||
| 211 | * @param boolean $old |
||
| 212 | * @return string|null the file path. |
||
| 213 | */ |
||
| 214 | public function getUploadPath($attribute, $old = false) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Returns file url for the attribute. |
||
| 226 | * @param string $attribute |
||
| 227 | * @return string|null |
||
| 228 | */ |
||
| 229 | public function getUploadUrl($attribute) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Returns the UploadedFile instance. |
||
| 241 | * @return UploadedFile |
||
| 242 | */ |
||
| 243 | protected function getUploadedFile() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Replaces all placeholders in path variable with corresponding values. |
||
| 250 | */ |
||
| 251 | protected function resolvePath($path) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Saves the uploaded file. |
||
| 268 | * @param UploadedFile $file the uploaded file instance |
||
| 269 | * @param string $path the file path used to save the uploaded file |
||
| 270 | * @return boolean true whether the file is saved successfully |
||
| 271 | */ |
||
| 272 | protected function save($file, $path) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Deletes old file. |
||
| 279 | * @param string $attribute |
||
| 280 | * @param boolean $old |
||
| 281 | */ |
||
| 282 | protected function delete($attribute, $old = false) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param UploadedFile $file |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | protected function getFileName($file) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Replaces characters in strings that are illegal/unsafe for filename. |
||
| 307 | * |
||
| 308 | * #my* unsaf<e>&file:name?".png |
||
| 309 | * |
||
| 310 | * @param string $filename the source filename to be "sanitized" |
||
| 311 | * @return boolean string the sanitized filename |
||
| 312 | */ |
||
| 313 | public static function sanitize($filename) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Generates random filename. |
||
| 320 | * @param UploadedFile $file |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | protected function generateFileName($file) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * This method is invoked after uploading a file. |
||
| 330 | * The default implementation raises the [[EVENT_AFTER_UPLOAD]] event. |
||
| 331 | * You may override this method to do postprocessing after the file is uploaded. |
||
| 332 | * Make sure you call the parent implementation so that the event is raised properly. |
||
| 333 | */ |
||
| 334 | protected function afterUpload() |
||
| 338 | } |
||
| 339 |