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 |
||
| 20 | class FileBehavior extends Behavior |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | public $attributes = []; |
||
| 26 | /** |
||
| 27 | * @var rkit\filemanager\behaviors\FileBind |
||
| 28 | */ |
||
| 29 | private static $bind; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @internal |
||
| 33 | */ |
||
| 34 | 49 | public function init() |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @internal |
||
| 44 | * @return void |
||
| 45 | */ |
||
| 46 | 49 | public function setBind() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @inheritdoc |
||
| 53 | * @internal |
||
| 54 | */ |
||
| 55 | 49 | public function events() |
|
| 65 | |||
| 66 | /** |
||
| 67 | * @internal |
||
| 68 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 69 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
| 70 | */ |
||
| 71 | 49 | public function beforeSave($insert) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @internal |
||
| 84 | */ |
||
| 85 | 23 | public function afterSave() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @internal |
||
| 109 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
| 110 | */ |
||
| 111 | 1 | public function beforeDelete() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Prepare the path of the file |
||
| 122 | * |
||
| 123 | * @param array $data |
||
| 124 | * @param string $attribute |
||
| 125 | * @param Strorage $storage |
||
| 126 | * @param int $ownerId |
||
| 127 | * @param int $ownerType |
||
| 128 | * @param mixed $fileId |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | 23 | private function binding($data, $attribute, $storage, $ownerId, $ownerType, $fileId) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Prepare the path of the file |
||
| 152 | * |
||
| 153 | * @param mixed $file |
||
| 154 | * @param mixed $oldValue |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | 14 | private function prepareFilePath($file, $oldValue) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Prepare the id of the file |
||
| 170 | * |
||
| 171 | * @param mixed $file |
||
| 172 | * @param mixed $oldValue |
||
| 173 | * @return int |
||
| 174 | */ |
||
| 175 | 6 | private function prepareFileId($file, $oldValue) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Get the path to the upload directory |
||
| 188 | * |
||
| 189 | * @param string $attribute Attribute of a model |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | 25 | public function uploadDir($attribute) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Get the type of the owner in as string |
||
| 202 | * |
||
| 203 | * @param string $attribute Attribute of a model |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | 36 | private function getStringOwnerType($attribute) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Get the type of the owner |
||
| 213 | * |
||
| 214 | * @param string $attribute Attribute of a model |
||
| 215 | * @return int |
||
| 216 | */ |
||
| 217 | 36 | public function getFileOwnerType($attribute) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Get files |
||
| 224 | * |
||
| 225 | * @param string $attribute Attribute of a model |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | 3 | public function getFiles($attribute) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Get the file |
||
| 240 | * |
||
| 241 | * @param string $attribute Attribute of a model |
||
| 242 | * @return File|null |
||
| 243 | */ |
||
| 244 | 1 | public function getFile($attribute) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Check whether the upload of multiple files |
||
| 253 | * |
||
| 254 | * @param string $attribute Attribute of a model |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | 23 | public function isMultiple($attribute) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Checks whether the file is protected |
||
| 264 | * |
||
| 265 | * @param string $attribute Attribute of a model |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | 35 | public function isFileProtected($attribute) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Get rules |
||
| 275 | * |
||
| 276 | * @param string $attribute Attribute of a model |
||
| 277 | * @param bool $onlyCoreValidators Only core validators |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | 28 | public function getFileRules($attribute, $onlyCoreValidators = false) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Get the presets of the file |
||
| 292 | * |
||
| 293 | * @param string $attribute Attribute of a model |
||
| 294 | * @return array |
||
| 295 | */ |
||
| 296 | 6 | public function getFilePreset($attribute) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Get the presets of the file for apply after upload |
||
| 303 | * |
||
| 304 | * @param string $attribute Attribute of a model |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | 25 | public function getFilePresetAfterUpload($attribute) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Get the storage of the file |
||
| 321 | * |
||
| 322 | * @param string $attribute Attribute of a model |
||
| 323 | * @return Storage |
||
| 324 | * @throws InvalidParamException |
||
| 325 | */ |
||
| 326 | 35 | public function getFileStorage($attribute) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Generate a thumb name |
||
| 338 | * |
||
| 339 | * @param string $path The path of the file |
||
| 340 | * @param string $prefix Prefix for name of the file |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | 25 | public function generateThumbName($path, $prefix) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Resize image |
||
| 351 | * |
||
| 352 | * @param string $attribute Attribute of a model |
||
| 353 | * @param string $preset The name of the preset |
||
| 354 | * @param string $pathToFile Use this path to the file |
||
| 355 | * @param bool $returnRealPath Return the real path to the file |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | 25 | public function thumb($attribute, $preset, $pathToFile = null, $returnRealPath = false) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Create a file |
||
| 378 | * |
||
| 379 | * @param string $attribute Attribute of a model |
||
| 380 | * @param string $path The path of the file |
||
| 381 | * @param string $title The title of file |
||
| 382 | * @param bool $temporary The file is temporary |
||
| 383 | * @return rkit\filemanager\models\File |
||
| 384 | */ |
||
| 385 | 35 | public function createFile($attribute, $path, $title, $temporary) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Get a description of the validation rules in as text |
||
| 405 | * |
||
| 406 | * Example |
||
| 407 | * |
||
| 408 | * ```php |
||
| 409 | * $form->field($model, $attribute)->hint($model->getFileRulesDescription($attribute) |
||
| 410 | * ``` |
||
| 411 | * |
||
| 412 | * Output |
||
| 413 | * |
||
| 414 | * ``` |
||
| 415 | * Min. size of image: 300x300px |
||
| 416 | * File types: JPG, JPEG, PNG |
||
| 417 | * Max. file size: 1.049 MB |
||
| 418 | * ``` |
||
| 419 | * |
||
| 420 | * @param string $attribute Attribute of a model |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | 9 | public function getFileRulesDescription($attribute) |
|
| 427 | } |
||
| 428 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..