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 | 44 | public function init() |
|
| 35 | |||
| 36 | /** |
||
| 37 | * @inheritdoc |
||
| 38 | * @internal |
||
| 39 | */ |
||
| 40 | 44 | public function events() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @internal |
||
| 53 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 54 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
| 55 | */ |
||
| 56 | 15 | public function beforeSave($insert) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @internal |
||
| 69 | */ |
||
| 70 | 15 | public function afterSave() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @internal |
||
| 104 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
| 105 | */ |
||
| 106 | 1 | public function beforeDelete() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Delete files |
||
| 115 | * |
||
| 116 | * @param string $attribute Attribute name of the model |
||
| 117 | * @param int $ownerId The id of the owner |
||
| 118 | * @param int $ownerType The type of the owner |
||
| 119 | */ |
||
| 120 | 4 | private function deleteFiles($attribute, $ownerId, $ownerType) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Delete current files |
||
| 135 | * |
||
| 136 | * @param Storage $storage |
||
|
|
|||
| 137 | * @param int $ownerId The id of the owner |
||
| 138 | * @param int $ownerType The type of the owner |
||
| 139 | * @param rkit\filemanager\models\File[] $exceptFiles |
||
| 140 | */ |
||
| 141 | 12 | private function deleteCurrentFiles($attribute, $ownerId, $ownerType, $exceptFiles = []) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Bind the file to the with owner |
||
| 159 | * @internal |
||
| 160 | * |
||
| 161 | * @param Storage $storage The storage for the file |
||
| 162 | * @param int $ownerId The id of the owner |
||
| 163 | * @param int $ownerType The type of the owner |
||
| 164 | * @param int $fileId The id of the file |
||
| 165 | * @return rkit\filemanager\models\File|bool |
||
| 166 | */ |
||
| 167 | 9 | private function bindSingle($attribute, $ownerId, $ownerType, $fileId) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Bind files to the with owner |
||
| 184 | * |
||
| 185 | * @param Storage $storage The storage for the files |
||
| 186 | * @param int $ownerId The id of the owner |
||
| 187 | * @param int $ownerType The type of the owner |
||
| 188 | * @param array $files Array of ids |
||
| 189 | * @return rkit\filemanager\models\File[]|bool |
||
| 190 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 191 | */ |
||
| 192 | 4 | private function bindMultiple($attribute, $ownerId, $ownerType, $files) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Get the path of the file |
||
| 218 | * |
||
| 219 | * @param mixed $file |
||
| 220 | * @param mixed $oldValue |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | 9 | private function getFilePathValue($file, $defaultValue) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Get the storage of the file |
||
| 235 | * |
||
| 236 | * @param string $attribute Attribute name of the model |
||
| 237 | * @return Storage |
||
| 238 | * @throws InvalidParamException |
||
| 239 | */ |
||
| 240 | 28 | private function getFileStorage($attribute) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Generate a thumb |
||
| 252 | * |
||
| 253 | * @param string $attribute Attribute name of the model |
||
| 254 | * @param string $preset The name of the preset |
||
| 255 | * @param string $path File path |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | 18 | private function generateThumb($attribute, $preset, $path) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Prepare a new file |
||
| 277 | * |
||
| 278 | * @param string $attribute Attribute name of the model |
||
| 279 | * @param string $path File path |
||
| 280 | * @param string $title File title |
||
| 281 | * @return File |
||
| 282 | */ |
||
| 283 | 26 | private function prepareFile($attribute, $path, $title) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Get file option |
||
| 304 | * |
||
| 305 | * @param string $attribute Attribute name of the model |
||
| 306 | * @param string $option Option name |
||
| 307 | * @param mixed $defaultValue Default value |
||
| 308 | * @return mixed |
||
| 309 | */ |
||
| 310 | 41 | public function getFileOption($attribute, $option, $defaultValue = null) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Get file path |
||
| 317 | * |
||
| 318 | * @param string $attribute Attribute name of the model |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | 22 | public function getFilePath($attribute) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Get file url |
||
| 328 | * |
||
| 329 | * @param string $attribute Attribute name of the model |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | 4 | public function getFileUrl($attribute) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Get the type of the owner |
||
| 339 | * |
||
| 340 | * @param string $attribute Attribute name of the model |
||
| 341 | * @return int |
||
| 342 | */ |
||
| 343 | 29 | public function getFileOwnerType($attribute) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Get files |
||
| 350 | * |
||
| 351 | * @param string $attribute Attribute name of the model |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | 4 | public function getFiles($attribute) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Get the file |
||
| 361 | * |
||
| 362 | * @param string $attribute Attribute name of the model |
||
| 363 | * @return File|null |
||
| 364 | */ |
||
| 365 | 8 | public function getFile($attribute) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Get rules |
||
| 372 | * |
||
| 373 | * @param string $attribute Attribute name of the model |
||
| 374 | * @param bool $onlyCoreValidators Only core validators |
||
| 375 | * @return array |
||
| 376 | */ |
||
| 377 | 21 | public function getFileRules($attribute, $onlyCoreValidators = false) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Get the presets of the file for apply after upload |
||
| 389 | * |
||
| 390 | * @param string $attribute Attribute name of the model |
||
| 391 | * @return array |
||
| 392 | */ |
||
| 393 | 18 | public function getFilePresetAfterUpload($attribute) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Get a description of the validation rules in as text |
||
| 407 | * |
||
| 408 | * Example |
||
| 409 | * |
||
| 410 | * ```php |
||
| 411 | * $form->field($model, $attribute)->hint($model->getFileRulesDescription($attribute) |
||
| 412 | * ``` |
||
| 413 | * |
||
| 414 | * Output |
||
| 415 | * |
||
| 416 | * ``` |
||
| 417 | * Min. size of image: 300x300px |
||
| 418 | * File types: JPG, JPEG, PNG |
||
| 419 | * Max. file size: 1.049 MB |
||
| 420 | * ``` |
||
| 421 | * |
||
| 422 | * @param string $attribute Attribute name of the model |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | 9 | public function getFileRulesDescription($attribute) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Generate a thumb name |
||
| 432 | * |
||
| 433 | * @param string $path The path of the file |
||
| 434 | * @param string $prefix Prefix for name of the file |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | 18 | public function generateThumbName($path, $prefix) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Create a thumb |
||
| 445 | * |
||
| 446 | * @param string $attribute Attribute name of the model |
||
| 447 | * @param string $preset The name of the preset |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | 18 | public function thumb($attribute, $preset) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Create a thumb and return full path |
||
| 457 | * |
||
| 458 | * @param string $attribute Attribute name of the model |
||
| 459 | * @param string $preset The name of the preset |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | 2 | public function thumbFullPath($attribute, $preset) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Create a file |
||
| 471 | * |
||
| 472 | * @param string $attribute Attribute name of the model |
||
| 473 | * @param string $path File path |
||
| 474 | * @param string $title File title |
||
| 475 | * @return rkit\filemanager\models\File |
||
| 476 | */ |
||
| 477 | 27 | public function createFile($attribute, $path, $title = null) |
|
| 494 | } |
||
| 495 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.