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 | * @var ActiveQuery |
||
| 25 | */ |
||
| 26 | private $relation; |
||
| 27 | /** |
||
| 28 | * @var FileBind |
||
| 29 | */ |
||
| 30 | private $fileBind; |
||
| 31 | /** |
||
| 32 | * @var string name of application component that represents `user` |
||
| 33 | */ |
||
| 34 | public $userComponent = 'user'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @internal |
||
| 38 | */ |
||
| 39 | 31 | public function init() |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @inheritdoc |
||
| 50 | * @internal |
||
| 51 | */ |
||
| 52 | 31 | public function events() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @internal |
||
| 65 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 66 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
| 67 | */ |
||
| 68 | 10 | public function beforeSave($insert) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @internal |
||
| 81 | */ |
||
| 82 | 2 | public function afterSave() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @internal |
||
| 123 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
| 124 | */ |
||
| 125 | public function beforeDelete() |
||
| 131 | |||
| 132 | 26 | protected function getUser() |
|
| 139 | |||
| 140 | 1 | public function clearState($attribute, $files) |
|
| 163 | |||
| 164 | 25 | private function setState($attribute, $file) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * for models with single upload only |
||
| 177 | * @param $attribute |
||
| 178 | * @param $file |
||
| 179 | * @param $defaultValue |
||
| 180 | */ |
||
| 181 | private function setValue($attribute, $file, $defaultValue) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Generate a thumb |
||
| 202 | * |
||
| 203 | * @param string $attribute The attribute name |
||
| 204 | * @param string $preset The preset name |
||
| 205 | * @param string $path The file path |
||
| 206 | * @return string The thumb path |
||
| 207 | */ |
||
| 208 | private function generateThumb($attribute, $preset, $path) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Generate file path by template |
||
| 224 | * |
||
| 225 | * @param string $attribute The attribute name |
||
| 226 | * @param ActiveRecord $file The file model |
||
| 227 | * @return string The file path |
||
| 228 | */ |
||
| 229 | private function templatePath($attribute, $file = null) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get relation |
||
| 252 | * |
||
| 253 | * @param string $attribute The attribute name |
||
| 254 | * @return \ActiveQuery |
||
| 255 | */ |
||
| 256 | 2 | public function fileRelation($attribute) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Get file option |
||
| 266 | * |
||
| 267 | * @param string $attribute The attribute name |
||
| 268 | * @param string $option Option name |
||
| 269 | * @param mixed $defaultValue Default value |
||
| 270 | * @return mixed |
||
| 271 | */ |
||
| 272 | 30 | public function fileOption($attribute, $option, $defaultValue = null) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Get file storage |
||
| 279 | * |
||
| 280 | * @param string $attribute The attribute name |
||
| 281 | * @return \Flysystem |
||
| 282 | */ |
||
| 283 | 26 | public function fileStorage($attribute) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Get file path |
||
| 290 | * |
||
| 291 | * @param string $attribute The attribute name |
||
| 292 | * @param ActiveRecord $file Use this file model |
||
| 293 | * @return string The file path |
||
| 294 | */ |
||
| 295 | public function filePath($attribute, $file = null) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get file url |
||
| 303 | * |
||
| 304 | * @param string $attribute The attribute name |
||
| 305 | * @param ActiveRecord $file Use this file model |
||
| 306 | * @return string The file url |
||
| 307 | */ |
||
| 308 | public function fileUrl($attribute, $file = null) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get extra fields of file |
||
| 316 | * |
||
| 317 | * @param string $attribute The attribute name |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | public function fileExtraFields($attribute) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get files |
||
| 331 | * |
||
| 332 | * @param string $attribute The attribute name |
||
| 333 | * @return \ActiveRecord[] The file models |
||
| 334 | */ |
||
| 335 | 2 | public function files($attribute) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Get the file |
||
| 342 | * |
||
| 343 | * @param string $attribute The attribute name |
||
| 344 | * @return \ActiveRecord The file model |
||
| 345 | */ |
||
| 346 | 1 | public function file($attribute) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Get rules |
||
| 353 | * |
||
| 354 | * @param string $attribute The attribute name |
||
| 355 | * @param bool $onlyCoreValidators Only core validators |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | 29 | public function fileRules($attribute, $onlyCoreValidators = false) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Get file state |
||
| 370 | * |
||
| 371 | * @param string $attribute The attribute name |
||
| 372 | * @return array |
||
| 373 | */ |
||
| 374 | public function fileState($attribute) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get the presets of the file for apply after upload |
||
| 398 | * |
||
| 399 | * @param string $attribute The attribute name |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | public function filePresetAfterUpload($attribute) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Create a thumb and return url |
||
| 413 | * |
||
| 414 | * @param string $attribute The attribute name |
||
| 415 | * @param string $preset The preset name |
||
| 416 | * @param ActiveRecord $file Use this file model |
||
| 417 | * @return string The file url |
||
| 418 | */ |
||
| 419 | public function thumbUrl($attribute, $preset, $file = null) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Create a thumb and return full path |
||
| 429 | * |
||
| 430 | * @param string $attribute The attribute name |
||
| 431 | * @param string $preset The preset name |
||
| 432 | * @param ActiveRecord $file Use this file model |
||
| 433 | * @return string The file path |
||
| 434 | */ |
||
| 435 | public function thumbPath($attribute, $preset, $file = null) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Create a file |
||
| 445 | * |
||
| 446 | * @param string $attribute The attribute name |
||
| 447 | * @param string $path The file path |
||
| 448 | * @param string $name The file name |
||
| 449 | * @return \ActiveRecord The file model |
||
| 450 | */ |
||
| 451 | 25 | public function createFile($attribute, $path, $name) |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Create a file from remote URL |
||
| 470 | * |
||
| 471 | * @author Sergii Gamaiunov <[email protected]> |
||
| 472 | * |
||
| 473 | * @param string $attribute The attribute name |
||
| 474 | * @param \igogo5yo\uploadfromurl\UploadFromUrl $remoteFile |
||
| 475 | * @param string $name The file name |
||
| 476 | * @return \ActiveRecord The file model |
||
| 477 | */ |
||
| 478 | public function createRemoteFile($attribute, $remoteFile, $name) |
||
| 500 | } |
||
| 501 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.