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 |
||
| 17 | class FileBehavior extends Behavior |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | public $attributes = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var ActiveQuery |
||
| 26 | */ |
||
| 27 | private $relation; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var FileBind |
||
| 31 | */ |
||
| 32 | private $fileBind; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected static $classPathMap = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string name of application component that represents `user` |
||
| 41 | */ |
||
| 42 | public $userComponent = 'user'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @since 5.6.0 |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | protected $markedLinked = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @internal |
||
| 52 | */ |
||
| 53 | 31 | public function init() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @inheritdoc |
||
| 64 | * @internal |
||
| 65 | */ |
||
| 66 | 31 | public function events() |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @internal |
||
| 79 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 80 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
| 81 | */ |
||
| 82 | 2 | public function beforeSave($insert) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @internal |
||
| 95 | */ |
||
| 96 | 2 | public function afterSave() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @internal |
||
| 137 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
| 138 | */ |
||
| 139 | public function beforeDelete() |
||
| 145 | |||
| 146 | 26 | protected function getUser() |
|
| 153 | |||
| 154 | 1 | public function clearState($attribute, $files) |
|
| 177 | |||
| 178 | 25 | private function setState($attribute, $file) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * for models with single upload only |
||
| 191 | * @param $attribute |
||
| 192 | * @param $file |
||
| 193 | * @param $defaultValue |
||
| 194 | */ |
||
| 195 | private function setValue($attribute, $file, $defaultValue) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Generate a thumb |
||
| 216 | * |
||
| 217 | * @param string $attribute The attribute name |
||
| 218 | * @param string $preset The preset name |
||
| 219 | * @param string $path The file path |
||
| 220 | * @return string The thumb path |
||
| 221 | */ |
||
| 222 | private function generateThumb($attribute, $preset, $path) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Generate file path by template |
||
| 238 | * |
||
| 239 | * @param string $attribute The attribute name |
||
| 240 | * @param ActiveRecord $file The file model |
||
| 241 | * @return string The file path |
||
| 242 | */ |
||
| 243 | private function templatePath($attribute, $file = null) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get relation |
||
| 266 | * |
||
| 267 | * @param string $attribute The attribute name |
||
| 268 | * @return \ActiveQuery |
||
| 269 | */ |
||
| 270 | 2 | public function fileRelation($attribute) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Get file option |
||
| 280 | * |
||
| 281 | * @param string $attribute The attribute name |
||
| 282 | * @param string $option Option name |
||
| 283 | * @param mixed $defaultValue Default value |
||
| 284 | * @return mixed |
||
| 285 | */ |
||
| 286 | 30 | public function fileOption($attribute, $option, $defaultValue = null) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Get file storage |
||
| 293 | * |
||
| 294 | * @param string $attribute The attribute name |
||
| 295 | * @return \Flysystem |
||
| 296 | */ |
||
| 297 | 26 | public function fileStorage($attribute) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Get file path |
||
| 304 | * |
||
| 305 | * @param string $attribute The attribute name |
||
| 306 | * @param ActiveRecord $file Use this file model |
||
| 307 | * @return string The file path |
||
| 308 | */ |
||
| 309 | public function filePath($attribute, $file = null) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get file url |
||
| 317 | * |
||
| 318 | * @param string $attribute The attribute name |
||
| 319 | * @param ActiveRecord $file Use this file model |
||
| 320 | * @return string The file url |
||
| 321 | */ |
||
| 322 | public function fileUrl($attribute, $file = null) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get extra fields of file |
||
| 330 | * |
||
| 331 | * @param string $attribute The attribute name |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | public function fileExtraFields($attribute) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get files |
||
| 345 | * |
||
| 346 | * @param string $attribute The attribute name |
||
| 347 | * @return \ActiveRecord[] The file models |
||
| 348 | */ |
||
| 349 | 2 | public function files($attribute) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Get the file |
||
| 356 | * |
||
| 357 | * @param string $attribute The attribute name |
||
| 358 | * @return \ActiveRecord The file model |
||
| 359 | */ |
||
| 360 | 1 | public function file($attribute) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Get rules |
||
| 367 | * |
||
| 368 | * @param string $attribute The attribute name |
||
| 369 | * @param bool $onlyCoreValidators Only core validators |
||
| 370 | * @return array |
||
| 371 | */ |
||
| 372 | 29 | public function fileRules($attribute, $onlyCoreValidators = false) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Get file state |
||
| 384 | * |
||
| 385 | * @param string $attribute The attribute name |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | public function fileState($attribute) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Get the presets of the file for apply after upload |
||
| 412 | * |
||
| 413 | * @param string $attribute The attribute name |
||
| 414 | * @return array |
||
| 415 | */ |
||
| 416 | public function filePresetAfterUpload($attribute) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Create a thumb and return url |
||
| 427 | * |
||
| 428 | * @param string $attribute The attribute name |
||
| 429 | * @param string $preset The preset name |
||
| 430 | * @param ActiveRecord $file Use this file model |
||
| 431 | * @return string The file url |
||
| 432 | */ |
||
| 433 | public function thumbUrl($attribute, $preset, $file = null) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Create a thumb and return full path |
||
| 443 | * |
||
| 444 | * @param string $attribute The attribute name |
||
| 445 | * @param string $preset The preset name |
||
| 446 | * @param ActiveRecord $file Use this file model |
||
| 447 | * @return string The file path |
||
| 448 | */ |
||
| 449 | public function thumbPath($attribute, $preset, $file = null) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Create a file |
||
| 459 | * |
||
| 460 | * @param string $attribute The attribute name |
||
| 461 | * @param string $path The file path |
||
| 462 | * @param string $name The file name |
||
| 463 | * @return \ActiveRecord The file model |
||
| 464 | */ |
||
| 465 | 25 | public function createFile($attribute, $path, $name) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Create a file from remote URL |
||
| 486 | * |
||
| 487 | * @author Sergii Gamaiunov <[email protected]> |
||
| 488 | * |
||
| 489 | * @param string $attribute The attribute name |
||
| 490 | * @param \igogo5yo\uploadfromurl\UploadFromUrl $remoteFile |
||
| 491 | * @param string $name The file name |
||
| 492 | * @return \ActiveRecord The file model |
||
| 493 | */ |
||
| 494 | public function createRemoteFile($attribute, $remoteFile, $name) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Add class alias to be able to upload files for different versions of a model to a single API endpoint |
||
| 521 | * |
||
| 522 | * Example: |
||
| 523 | * ``` |
||
| 524 | * class OldCar extends Car |
||
| 525 | * { |
||
| 526 | * public function init() |
||
| 527 | * { |
||
| 528 | * parent::init(); |
||
| 529 | * $this->car_type = 'old; |
||
| 530 | * FileBehavior::addClassAlias(get_class($this), Car::className()); |
||
| 531 | * } |
||
| 532 | * |
||
| 533 | * public function formName() { |
||
| 534 | * return 'Car'; |
||
| 535 | * } |
||
| 536 | * } |
||
| 537 | * ``` |
||
| 538 | * @param $source |
||
| 539 | * @param $mapTo |
||
| 540 | */ |
||
| 541 | public static function addClassAlias($source, $mapTo) { |
||
| 544 | |||
| 545 | protected static function getClass($source) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Mark current upload session as already linked (e.g. file is linked during `createFile`) to avoid duplicate links |
||
| 553 | * @return $this |
||
| 554 | * @since 5.6.0 |
||
| 555 | */ |
||
| 556 | public function markLinked() { |
||
| 560 | } |
||
| 561 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.