Complex classes like FileBehavior 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 FileBehavior, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class FileBehavior extends Behavior |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | public $attributes = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var ActiveQuery |
||
| 27 | */ |
||
| 28 | private $relation; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var FileBind |
||
| 32 | */ |
||
| 33 | private $fileBind; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected static $classPathMap = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string name of application component that represents `user` |
||
| 42 | */ |
||
| 43 | public $userComponent = 'user'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @since 5.6.0 |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | protected $markedLinked = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @internal |
||
| 53 | 31 | */ |
|
| 54 | public function init() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @inheritdoc |
||
| 65 | * @internal |
||
| 66 | 31 | */ |
|
| 67 | public function events() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @internal |
||
| 80 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 81 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
| 82 | 2 | */ |
|
| 83 | public function beforeSave($insert) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @internal |
||
| 96 | 2 | */ |
|
| 97 | public function afterSave() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @internal |
||
| 144 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
| 145 | */ |
||
| 146 | 26 | public function beforeDelete() |
|
| 155 | |||
| 156 | 1 | protected function getUser() |
|
| 163 | |||
| 164 | public function clearState($attribute, $files) |
||
| 187 | |||
| 188 | private function setState($attribute, $file) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * for models with single upload only |
||
| 201 | * @param $attribute |
||
| 202 | * @param $file |
||
| 203 | * @param $defaultValue |
||
| 204 | */ |
||
| 205 | private function setValue($attribute, $file, $defaultValue) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Generate a thumb |
||
| 226 | * |
||
| 227 | * @param string $attribute The attribute name |
||
| 228 | * @param string $preset The preset name |
||
| 229 | * @param string $path The file path |
||
| 230 | * @return string The thumb path |
||
| 231 | */ |
||
| 232 | private function generateThumb($attribute, $preset, $path) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Generate file path by template |
||
| 248 | * |
||
| 249 | * @param string $attribute The attribute name |
||
| 250 | * @param ActiveRecord $file The file model |
||
| 251 | * @return string The file path |
||
| 252 | */ |
||
| 253 | private function templatePath($attribute, $file = null) |
||
| 273 | 2 | ||
| 274 | /** |
||
| 275 | 2 | * Get relation |
|
| 276 | * |
||
| 277 | * @param string $attribute The attribute name |
||
| 278 | * @return \ActiveQuery |
||
| 279 | */ |
||
| 280 | public function fileRelation($attribute) |
||
| 287 | |||
| 288 | 30 | /** |
|
| 289 | * Get file option |
||
| 290 | * |
||
| 291 | * @param string $attribute The attribute name |
||
| 292 | * @param string $option Option name |
||
| 293 | * @param mixed $defaultValue Default value |
||
| 294 | * @return mixed |
||
| 295 | */ |
||
| 296 | public function fileOption($attribute, $option, $defaultValue = null) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get file storage |
||
| 303 | * |
||
| 304 | * @param string $attribute The attribute name |
||
| 305 | * @return \Flysystem |
||
| 306 | */ |
||
| 307 | public function fileStorage($attribute) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get file path |
||
| 314 | * |
||
| 315 | * @param string $attribute The attribute name |
||
| 316 | * @param ActiveRecord $file Use this file model |
||
| 317 | * @return string The file path |
||
| 318 | */ |
||
| 319 | public function filePath($attribute, $file = null) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get file url |
||
| 329 | * |
||
| 330 | * @param string $attribute The attribute name |
||
| 331 | * @param ActiveRecord $file Use this file model |
||
| 332 | * @return string The file url |
||
| 333 | */ |
||
| 334 | public function fileUrl($attribute, $file = null) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get extra fields of file |
||
| 342 | * |
||
| 343 | * @param string $attribute The attribute name |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | public function fileExtraFields($attribute) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Get files |
||
| 360 | 1 | * |
|
| 361 | * @param string $attribute The attribute name |
||
| 362 | 1 | * @return \ActiveRecord[] The file models |
|
| 363 | */ |
||
| 364 | public function files($attribute) |
||
| 371 | |||
| 372 | 29 | /** |
|
| 373 | * Get the file |
||
| 374 | 29 | * |
|
| 375 | 29 | * @param string $attribute The attribute name |
|
| 376 | 28 | * @return \ActiveRecord The file model |
|
| 377 | 28 | */ |
|
| 378 | public function file($attribute) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get rules |
||
| 388 | * |
||
| 389 | * @param string $attribute The attribute name |
||
| 390 | * @param bool $onlyCoreValidators Only core validators |
||
| 391 | * @return array |
||
| 392 | */ |
||
| 393 | public function fileRules($attribute, $onlyCoreValidators = false) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Get file state |
||
| 405 | * |
||
| 406 | * @param string $attribute The attribute name |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | public function fileState($attribute) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get the presets of the file for apply after upload |
||
| 433 | * |
||
| 434 | * @param string $attribute The attribute name |
||
| 435 | * @return array |
||
| 436 | */ |
||
| 437 | public function filePresetAfterUpload($attribute) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Create a thumb and return url |
||
| 448 | * |
||
| 449 | * @param string $attribute The attribute name |
||
| 450 | * @param string $preset The preset name |
||
| 451 | * @param ActiveRecord $file Use this file model |
||
| 452 | * @return string The file url |
||
| 453 | */ |
||
| 454 | public function thumbUrl($attribute, $preset, $file = null) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Create a thumb and return full path |
||
| 464 | * |
||
| 465 | 25 | * @param string $attribute The attribute name |
|
| 466 | * @param string $preset The preset name |
||
| 467 | 25 | * @param ActiveRecord $file Use this file model |
|
| 468 | 25 | * @return string The file path |
|
| 469 | 25 | */ |
|
| 470 | 25 | public function thumbPath($attribute, $preset, $file = null) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Create a file |
||
| 480 | * |
||
| 481 | * @param string $attribute The attribute name |
||
| 482 | * @param string $path The file path |
||
| 483 | * @param string $name The file name |
||
| 484 | * @return \ActiveRecord The file model |
||
| 485 | */ |
||
| 486 | public function createFile($attribute, $path, $name) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Create a file from remote URL |
||
| 511 | * |
||
| 512 | * @author Sergii Gamaiunov <[email protected]> |
||
| 513 | * |
||
| 514 | * @param string $attribute The attribute name |
||
| 515 | * @param \igogo5yo\uploadfromurl\UploadFromUrl $remoteFile |
||
| 516 | * @param string $name The file name |
||
| 517 | * @return \ActiveRecord The file model |
||
| 518 | */ |
||
| 519 | public function createRemoteFile($attribute, $remoteFile, $name) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Add class alias to be able to upload files for different versions of a model to a single API endpoint |
||
| 546 | * |
||
| 547 | * Example: |
||
| 548 | * ``` |
||
| 549 | * class OldCar extends Car |
||
| 550 | * { |
||
| 551 | * public function init() |
||
| 552 | * { |
||
| 553 | * parent::init(); |
||
| 554 | * $this->car_type = 'old; |
||
| 555 | * FileBehavior::addClassAlias(get_class($this), Car::className()); |
||
| 556 | * } |
||
| 557 | * |
||
| 558 | * public function formName() { |
||
| 559 | * return 'Car'; |
||
| 560 | * } |
||
| 561 | * } |
||
| 562 | * ``` |
||
| 563 | * @param $source |
||
| 564 | * @param $mapTo |
||
| 565 | */ |
||
| 566 | public static function addClassAlias($source, $mapTo) { |
||
| 569 | |||
| 570 | protected static function getClass($source) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Mark current upload session as already linked (e.g. file is linked during `createFile`) to avoid duplicate links |
||
| 578 | * @return $this |
||
| 579 | * @since 5.6.0 |
||
| 580 | */ |
||
| 581 | public function markLinked() { |
||
| 585 | } |
||
| 586 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.