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 boolean whether to append a timestamp to the URL. Example: `/path/to/image.png?v=timestamp` |
||
| 89 | */ |
||
| 90 | public $appendTimeStamp = false; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var UploadedFile the uploaded file instance. |
||
| 94 | */ |
||
| 95 | private $_file; |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * @inheritdoc |
||
| 100 | */ |
||
| 101 | public function init() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @inheritdoc |
||
| 118 | */ |
||
| 119 | public function events() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * This method is invoked before validation starts. |
||
| 133 | */ |
||
| 134 | public function beforeValidate() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * This method is called at the beginning of inserting or updating a record. |
||
| 157 | */ |
||
| 158 | public function beforeSave() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * This method is called at the end of inserting or updating a record. |
||
| 185 | * @throws \yii\base\InvalidArgumentException |
||
| 186 | */ |
||
| 187 | public function afterSave() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * This method is invoked after deleting a record. |
||
| 204 | */ |
||
| 205 | public function afterDelete() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Returns file path for the attribute. |
||
| 215 | * @param string $attribute |
||
| 216 | * @param boolean $old |
||
| 217 | * @return string|null the file path. |
||
| 218 | */ |
||
| 219 | public function getUploadPath($attribute, $old = false) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Returns file url for the attribute. |
||
| 231 | * @param string $attribute |
||
| 232 | * @return string|null |
||
| 233 | */ |
||
| 234 | public function getUploadUrl($attribute) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Returns the UploadedFile instance. |
||
| 247 | * @return UploadedFile |
||
| 248 | */ |
||
| 249 | protected function getUploadedFile() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Replaces all placeholders in path variable with corresponding values. |
||
| 256 | */ |
||
| 257 | protected function resolvePath($path) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Saves the uploaded file. |
||
| 274 | * @param UploadedFile $file the uploaded file instance |
||
| 275 | * @param string $path the file path used to save the uploaded file |
||
| 276 | * @return boolean true whether the file is saved successfully |
||
| 277 | */ |
||
| 278 | protected function save($file, $path) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Deletes old file. |
||
| 285 | * @param string $attribute |
||
| 286 | * @param boolean $old |
||
| 287 | */ |
||
| 288 | protected function delete($attribute, $old = false) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param UploadedFile $file |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | protected function getFileName($file) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Replaces characters in strings that are illegal/unsafe for filename. |
||
| 313 | * |
||
| 314 | * #my* unsaf<e>&file:name?".png |
||
| 315 | * |
||
| 316 | * @param string $filename the source filename to be "sanitized" |
||
| 317 | * @return boolean string the sanitized filename |
||
| 318 | */ |
||
| 319 | public static function sanitize($filename) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Generates random filename. |
||
| 326 | * @param UploadedFile $file |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | protected function generateFileName($file) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * This method is invoked after uploading a file. |
||
| 336 | * The default implementation raises the [[EVENT_AFTER_UPLOAD]] event. |
||
| 337 | * You may override this method to do postprocessing after the file is uploaded. |
||
| 338 | * Make sure you call the parent implementation so that the event is raised properly. |
||
| 339 | */ |
||
| 340 | protected function afterUpload() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Returns the timestamp of file by given attribute |
||
| 347 | * @param $attribute |
||
| 348 | * @return string|null |
||
| 349 | */ |
||
| 350 | protected function getTimestamp($attribute) { |
||
| 358 | } |
||
| 359 |