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 | 44 | public function init() |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @internal |
||
| 44 | * @return void |
||
| 45 | */ |
||
| 46 | 44 | public function setBind() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @inheritdoc |
||
| 53 | * @internal |
||
| 54 | */ |
||
| 55 | 44 | public function events() |
|
| 65 | |||
| 66 | /** |
||
| 67 | * @internal |
||
| 68 | */ |
||
| 69 | 44 | public function beforeSave($insert) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @internal |
||
| 82 | */ |
||
| 83 | 22 | public function afterSave() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @internal |
||
| 107 | */ |
||
| 108 | 1 | public function beforeDelete() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Prepare the path of the file |
||
| 119 | * |
||
| 120 | * @param array $data |
||
| 121 | * @param string $attribute |
||
| 122 | * @param Strorage $storage |
||
| 123 | * @param int $ownerId |
||
| 124 | * @param int $ownerType |
||
| 125 | * @param mixed $fileId |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | 22 | private function binding($data, $attribute, $storage, $ownerId, $ownerType, $fileId) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Prepare the path of the file |
||
| 149 | * |
||
| 150 | * @param mixed $file |
||
| 151 | * @param mixed $oldValue |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | 13 | private function prepareFilePath($file, $oldValue) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Prepare the id of the file |
||
| 167 | * |
||
| 168 | * @param mixed $file |
||
| 169 | * @param mixed $oldValue |
||
| 170 | * @return int |
||
| 171 | */ |
||
| 172 | 6 | private function prepareFileId($file, $oldValue) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Get the path to the upload directory |
||
| 185 | * |
||
| 186 | * @param string $attribute Attribute of a model |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | 24 | public function uploadDir($attribute) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Get the type of the owner in as string |
||
| 200 | * |
||
| 201 | * @param string $attribute Attribute of a model |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | 33 | private function getStringOwnerType($attribute) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Get the type of the owner |
||
| 211 | * |
||
| 212 | * @param string $attribute Attribute of a model |
||
| 213 | * @return int |
||
| 214 | */ |
||
| 215 | 33 | public function getFileOwnerType($attribute) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Get files |
||
| 222 | * |
||
| 223 | * @param string $attribute Attribute of a model |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | 3 | public function getFiles($attribute) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Get the file |
||
| 238 | * |
||
| 239 | * @param string $attribute Attribute of a model |
||
| 240 | * @return File|null |
||
| 241 | */ |
||
| 242 | 1 | public function getFile($attribute) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Check whether the upload of multiple files |
||
| 251 | * |
||
| 252 | * @param string $attribute Attribute of a model |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | 22 | public function isMultiple($attribute) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Checks whether the file is protected |
||
| 262 | * |
||
| 263 | * @param string $attribute Attribute of a model |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | 32 | public function isFileProtected($attribute) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Get rules |
||
| 273 | * |
||
| 274 | * @param string $attribute Attribute of a model |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | 26 | public function getFileRules($attribute) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Get the presets of the file |
||
| 284 | * |
||
| 285 | * @param string $attribute Attribute of a model |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | 6 | public function getFilePreset($attribute) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Get the presets of the file for apply after upload |
||
| 295 | * |
||
| 296 | * @param string $attribute Attribute of a model |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | 24 | public function getFilePresetAfterUpload($attribute) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Get the storage of the file |
||
| 313 | * |
||
| 314 | * @param string $attribute Attribute of a model |
||
| 315 | * @return Storage |
||
| 316 | * @throws InvalidParamException |
||
| 317 | */ |
||
| 318 | 32 | public function getFileStorage($attribute) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Generate a thumb name |
||
| 330 | * |
||
| 331 | * @param string $path The path of the file |
||
| 332 | * @param string $prefix Prefix for name of the file |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | 24 | public function generateThumbName($path, $prefix) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Resize image |
||
| 343 | * |
||
| 344 | * @param string $attribute Attribute of a model |
||
| 345 | * @param string $preset The name of the preset |
||
| 346 | * @param string $pathToFile Use this path to the file |
||
| 347 | * @param bool $returnRealPath Return the real path to the file |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | 24 | public function thumb($attribute, $preset, $pathToFile = null, $returnRealPath = false) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Create a file |
||
| 370 | * |
||
| 371 | * @param string $attribute Attribute of a model |
||
| 372 | * @param string $path The path of the file |
||
| 373 | * @param string $title The title of file |
||
| 374 | * @param bool $temporary The file is temporary |
||
| 375 | * @return rkit\filemanager\models\File |
||
| 376 | */ |
||
| 377 | 32 | public function createFile($attribute, $path, $title, $temporary) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Get a description of the validation rules in as text |
||
| 397 | * |
||
| 398 | * Example |
||
| 399 | * |
||
| 400 | * ```php |
||
| 401 | * $form->field($model, $attribute)->hint($model->getFileRulesDescription($attribute) |
||
| 402 | * ``` |
||
| 403 | * |
||
| 404 | * Output |
||
| 405 | * |
||
| 406 | * ``` |
||
| 407 | * Min. size of image: 300x300px |
||
| 408 | * File types: JPG, JPEG, PNG |
||
| 409 | * Max. file size: 1.049 MB |
||
| 410 | * ``` |
||
| 411 | * |
||
| 412 | * @param string $attribute Attribute of a model |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | 9 | public function getFileRulesDescription($attribute) |
|
| 419 | } |
||
| 420 |
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..