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 | /** |
||
| 33 | * @internal |
||
| 34 | */ |
||
| 35 | 31 | public function init() |
|
| 43 | |||
| 44 | /** |
||
| 45 | * @inheritdoc |
||
| 46 | * @internal |
||
| 47 | */ |
||
| 48 | 31 | public function events() |
|
| 58 | |||
| 59 | /** |
||
| 60 | * @internal |
||
| 61 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 62 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
| 63 | */ |
||
| 64 | 2 | public function beforeSave($insert) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @internal |
||
| 77 | */ |
||
| 78 | 2 | public function afterSave() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @internal |
||
| 119 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
| 120 | */ |
||
| 121 | public function beforeDelete() |
||
| 127 | |||
| 128 | 1 | public function clearState($attribute, $files) |
|
| 148 | |||
| 149 | 25 | private function setState($attribute, $file) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * for models with single upload only |
||
| 162 | * @param $attribute |
||
| 163 | * @param $file |
||
| 164 | * @param $defaultValue |
||
| 165 | */ |
||
| 166 | private function setValue($attribute, $file, $defaultValue) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Generate a thumb |
||
| 187 | * |
||
| 188 | * @param string $attribute The attribute name |
||
| 189 | * @param string $preset The preset name |
||
| 190 | * @param string $path The file path |
||
| 191 | * @return string The thumb path |
||
| 192 | */ |
||
| 193 | private function generateThumb($attribute, $preset, $path) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Generate file path by template |
||
| 209 | * |
||
| 210 | * @param string $attribute The attribute name |
||
| 211 | * @param ActiveRecord $file The file model |
||
| 212 | * @return string The file path |
||
| 213 | */ |
||
| 214 | private function templatePath($attribute, $file = null) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get relation |
||
| 237 | * |
||
| 238 | * @param string $attribute The attribute name |
||
| 239 | * @return \ActiveQuery |
||
| 240 | */ |
||
| 241 | 2 | public function fileRelation($attribute) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Get file option |
||
| 251 | * |
||
| 252 | * @param string $attribute The attribute name |
||
| 253 | * @param string $option Option name |
||
| 254 | * @param mixed $defaultValue Default value |
||
| 255 | * @return mixed |
||
| 256 | */ |
||
| 257 | 30 | public function fileOption($attribute, $option, $defaultValue = null) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Get file storage |
||
| 264 | * |
||
| 265 | * @param string $attribute The attribute name |
||
| 266 | * @return \Flysystem |
||
| 267 | */ |
||
| 268 | 26 | public function fileStorage($attribute) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Get file path |
||
| 275 | * |
||
| 276 | * @param string $attribute The attribute name |
||
| 277 | * @param ActiveRecord $file Use this file model |
||
| 278 | * @return string The file path |
||
| 279 | */ |
||
| 280 | public function filePath($attribute, $file = null) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get file url |
||
| 288 | * |
||
| 289 | * @param string $attribute The attribute name |
||
| 290 | * @param ActiveRecord $file Use this file model |
||
| 291 | * @return string The file url |
||
| 292 | */ |
||
| 293 | public function fileUrl($attribute, $file = null) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get extra fields of file |
||
| 301 | * |
||
| 302 | * @param string $attribute The attribute name |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | public function fileExtraFields($attribute) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get files |
||
| 316 | * |
||
| 317 | * @param string $attribute The attribute name |
||
| 318 | * @return \ActiveRecord[] The file models |
||
| 319 | */ |
||
| 320 | 1 | public function files($attribute) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Get the file |
||
| 327 | * |
||
| 328 | * @param string $attribute The attribute name |
||
| 329 | * @return \ActiveRecord The file model |
||
| 330 | */ |
||
| 331 | 1 | public function file($attribute) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Get rules |
||
| 338 | * |
||
| 339 | * @param string $attribute The attribute name |
||
| 340 | * @param bool $onlyCoreValidators Only core validators |
||
| 341 | * @return array |
||
| 342 | */ |
||
| 343 | 29 | public function fileRules($attribute, $onlyCoreValidators = false) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Get file state |
||
| 355 | * |
||
| 356 | * @param string $attribute The attribute name |
||
| 357 | * @return array |
||
| 358 | */ |
||
| 359 | public function fileState($attribute) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Get the presets of the file for apply after upload |
||
| 380 | * |
||
| 381 | * @param string $attribute The attribute name |
||
| 382 | * @return array |
||
| 383 | */ |
||
| 384 | public function filePresetAfterUpload($attribute) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Create a thumb and return url |
||
| 395 | * |
||
| 396 | * @param string $attribute The attribute name |
||
| 397 | * @param string $preset The preset name |
||
| 398 | * @param ActiveRecord $file Use this file model |
||
| 399 | * @return string The file url |
||
| 400 | */ |
||
| 401 | public function thumbUrl($attribute, $preset, $file = null) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Create a thumb and return full path |
||
| 411 | * |
||
| 412 | * @param string $attribute The attribute name |
||
| 413 | * @param string $preset The preset name |
||
| 414 | * @param ActiveRecord $file Use this file model |
||
| 415 | * @return string The file path |
||
| 416 | */ |
||
| 417 | public function thumbPath($attribute, $preset, $file = null) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Create a file |
||
| 427 | * |
||
| 428 | * @param string $attribute The attribute name |
||
| 429 | * @param string $path The file path |
||
| 430 | * @param string $name The file name |
||
| 431 | * @return \ActiveRecord The file model |
||
| 432 | */ |
||
| 433 | 25 | public function createFile($attribute, $path, $name) |
|
| 449 | } |
||
| 450 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.