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 | 41 | public function init() |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Set Decoder |
||
| 41 | * |
||
| 42 | * @return void |
||
| 43 | */ |
||
| 44 | 41 | public function setBind() |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @inheritdoc |
||
| 51 | */ |
||
| 52 | 41 | public function events() |
|
| 62 | |||
| 63 | 26 | public function beforeSave($insert) |
|
| 73 | |||
| 74 | 41 | public function afterSave() |
|
| 101 | |||
| 102 | 1 | public function beforeDelete() |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Prepare the path of the file |
||
| 113 | * |
||
| 114 | * @param mixed $file |
||
| 115 | * @param mixed $oldValue |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | 12 | private function prepareFilePath($file, $oldValue) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Prepare the id of the file |
||
| 131 | * |
||
| 132 | * @param mixed $file |
||
| 133 | * @param mixed $oldValue |
||
| 134 | * @return int |
||
| 135 | */ |
||
| 136 | 5 | private function prepareFileId($file, $oldValue) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Get the path to the upload directory |
||
| 149 | * |
||
| 150 | * @param string $attribute |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 30 | public function uploadDir($attribute) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Get the type of the owner in as string |
||
| 164 | * |
||
| 165 | * @param string $attribute |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | 30 | private function getStringOwnerType($attribute) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Get the type of the owner |
||
| 175 | * |
||
| 176 | * @param string $attribute |
||
| 177 | * @return int |
||
| 178 | */ |
||
| 179 | 30 | public function getFileOwnerType($attribute) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Get files |
||
| 186 | * |
||
| 187 | * @param string $attribute |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | 9 | public function getFiles($attribute) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Get the file |
||
| 202 | * |
||
| 203 | * @param string $attribute |
||
| 204 | * @return rkit\filemanager\models\File[] |
||
| 205 | */ |
||
| 206 | 1 | public function getFile($attribute) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * The file is protected |
||
| 215 | * |
||
| 216 | * @param string $attribute |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | 30 | public function isFileProtected($attribute) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Get rules |
||
| 226 | * |
||
| 227 | * @param string $attribute |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | 32 | public function getFileRules($attribute) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Get the presets of the file |
||
| 237 | * |
||
| 238 | * @param string $attribute |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | 5 | public function getFilePreset($attribute) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Get the presets of the file for apply after upload |
||
| 248 | * |
||
| 249 | * @param string $attribute |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | 30 | public function getFilePresetAfterUpload($attribute) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Get the storage of the file |
||
| 266 | * |
||
| 267 | * @param string $attribute |
||
| 268 | * @return Storage |
||
| 269 | */ |
||
| 270 | 30 | public function getFileStorage($attribute) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Generate a thumb name |
||
| 282 | * |
||
| 283 | * @param string $path |
||
| 284 | * @param string $prefix |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | 30 | public function generateThumbName($path, $prefix) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Resize image |
||
| 295 | * |
||
| 296 | * @param string $attribute |
||
| 297 | * @param string $preset |
||
| 298 | * @param string $pathToFile Use this path to the file |
||
| 299 | * @param bool $returnRealPath Return the real path to the file |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | 30 | public function thumb($attribute, $preset, $pathToFile = null, $returnRealPath = false) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Get description the rules of the file in as text |
||
| 322 | * |
||
| 323 | * @param string $attribute |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | 9 | public function getFileRulesDescription($attribute) |
|
| 330 | } |
||
| 331 |
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..