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 |
||
| 19 | class FileBehavior extends Behavior |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | public $attributes = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @internal |
||
| 28 | */ |
||
| 29 | public function init() |
||
| 35 | |||
| 36 | 49 | /** |
|
| 37 | * @inheritdoc |
||
| 38 | 49 | * @internal |
|
| 39 | 49 | */ |
|
| 40 | 49 | public function events() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @internal |
||
| 53 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 54 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
| 55 | 49 | */ |
|
| 56 | public function beforeSave($insert) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @internal |
||
| 69 | */ |
||
| 70 | public function afterSave() |
||
| 95 | 23 | ||
| 96 | 23 | /** |
|
| 97 | * @internal |
||
| 98 | 23 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
|
| 99 | 2 | */ |
|
| 100 | 2 | public function beforeDelete() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Delete current files |
||
| 109 | * |
||
| 110 | * @param Storage $storage |
||
| 111 | 1 | * @param rkit\filemanager\models\File[] $exceptFiles |
|
| 112 | */ |
||
| 113 | 1 | private function deleteFiles($attribute, $files) |
|
| 125 | |||
| 126 | private function bindFiles($attribute, $files) |
||
| 205 | |||
| 206 | 36 | /** |
|
| 207 | * Get the path of the file |
||
| 208 | 36 | * |
|
| 209 | * @param mixed $file |
||
| 210 | * @param mixed $oldValue |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | private function saveFilePathInAttribute($attribute, $file, $defaultValue) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get the path of the file |
||
| 228 | 3 | * |
|
| 229 | * @param mixed $file |
||
| 230 | 3 | * @param mixed $oldValue |
|
| 231 | 3 | * @return string |
|
| 232 | 3 | */ |
|
| 233 | 3 | private function saveFileIdInAttribute($attribute, $file, $defaultValue) |
|
| 243 | |||
| 244 | 1 | /** |
|
| 245 | * Generate a thumb |
||
| 246 | 1 | * |
|
| 247 | 1 | * @param string $attribute Attribute name of the model |
|
| 248 | 1 | * @param string $preset The name of the preset |
|
| 249 | * @param string $path File path |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | private function generateThumb($attribute, $preset, $path) |
||
| 268 | 35 | ||
| 269 | /** |
||
| 270 | 35 | * Get file option |
|
| 271 | * |
||
| 272 | * @param string $attribute Attribute name of the model |
||
| 273 | * @param string $option Option name |
||
| 274 | * @param mixed $defaultValue Default value |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | public function getFileOption($attribute, $option, $defaultValue = null) |
||
| 281 | |||
| 282 | 28 | /** |
|
| 283 | 28 | * Get the storage of the file |
|
| 284 | 24 | * |
|
| 285 | 24 | * @param string $attribute Attribute name of the model |
|
| 286 | 24 | * @return Storage |
|
| 287 | 28 | * @throws InvalidParamException |
|
| 288 | */ |
||
| 289 | public function getFileStorage($attribute) |
||
| 297 | |||
| 298 | 6 | /** |
|
| 299 | * Get file path |
||
| 300 | * |
||
| 301 | * @param string $attribute Attribute name of the model |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | public function getTemplatePath($attribute, $file = null) |
||
| 316 | 3 | ||
| 317 | /** |
||
| 318 | * Get file path |
||
| 319 | * |
||
| 320 | * @param string $attribute Attribute name of the model |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | public function getFilePath($attribute, $file = null) |
||
| 328 | 35 | ||
| 329 | 35 | /** |
|
| 330 | 35 | * Get file url |
|
| 331 | * |
||
| 332 | * @param string $attribute Attribute name of the model |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public function getFileUrl($attribute, $file = null) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Get files |
||
| 343 | 25 | * |
|
| 344 | * @param string $attribute Attribute name of the model |
||
| 345 | 25 | * @return array |
|
| 346 | 25 | */ |
|
| 347 | public function getFiles($attribute) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get the file |
||
| 355 | * |
||
| 356 | * @param string $attribute Attribute name of the model |
||
| 357 | * @return File|null |
||
| 358 | 25 | */ |
|
| 359 | public function getFile($attribute) |
||
| 364 | 25 | ||
| 365 | 25 | /** |
|
| 366 | 25 | * Get rules |
|
| 367 | 25 | * |
|
| 368 | 25 | * @param string $attribute Attribute name of the model |
|
| 369 | 25 | * @param bool $onlyCoreValidators Only core validators |
|
| 370 | 25 | * @return array |
|
| 371 | 25 | */ |
|
| 372 | public function getFileRules($attribute, $onlyCoreValidators = false) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get the presets of the file for apply after upload |
||
| 384 | * |
||
| 385 | 35 | * @param string $attribute Attribute name of the model |
|
| 386 | * @return array |
||
| 387 | 35 | */ |
|
| 388 | 35 | public function getFilePresetAfterUpload($attribute) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Get a description of the validation rules in as text |
||
| 402 | * |
||
| 403 | * Example |
||
| 404 | * |
||
| 405 | * ```php |
||
| 406 | * $form->field($model, $attribute)->hint($model->getFileRulesDescription($attribute) |
||
| 407 | * ``` |
||
| 408 | * |
||
| 409 | * Output |
||
| 410 | * |
||
| 411 | * ``` |
||
| 412 | * Min. size of image: 300x300px |
||
| 413 | * File types: JPG, JPEG, PNG |
||
| 414 | * Max. file size: 1.049 MB |
||
| 415 | * ``` |
||
| 416 | * |
||
| 417 | * @param string $attribute Attribute name of the model |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public function getFileRulesDescription($attribute) |
||
| 424 | |||
| 425 | 9 | /** |
|
| 426 | * Generate a thumb name |
||
| 427 | * |
||
| 428 | * @param string $path The path of the file |
||
| 429 | * @param string $prefix Prefix for name of the file |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | public function generateThumbName($path, $prefix) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Create a thumb |
||
| 440 | * |
||
| 441 | * @param string $attribute Attribute name of the model |
||
| 442 | * @param string $preset The name of the preset |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | public function thumb($attribute, $preset, $file = null) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Create a thumb and return full path |
||
| 453 | * |
||
| 454 | * @param string $attribute Attribute name of the model |
||
| 455 | * @param string $preset The name of the preset |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | public function thumbFullPath($attribute, $preset, $file = null) |
||
| 465 | } |
||
| 466 |
This check looks for function calls that miss required arguments.