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 | 42 | public function init() |
|
38 | |||
39 | /** |
||
40 | * Set Decoder |
||
41 | * |
||
42 | * @return void |
||
43 | */ |
||
44 | 42 | public function setBind() |
|
48 | |||
49 | /** |
||
50 | * @inheritdoc |
||
51 | */ |
||
52 | 42 | public function events() |
|
62 | |||
63 | 27 | public function beforeSave($insert) |
|
73 | |||
74 | 42 | public function afterSave() |
|
95 | |||
96 | 1 | public function beforeDelete() |
|
104 | |||
105 | 27 | private function binding($data, $attribute, $storage, $ownerId, $ownerType, $fileId) |
|
123 | |||
124 | /** |
||
125 | * Prepare the path of the file |
||
126 | * |
||
127 | * @param mixed $file |
||
128 | * @param mixed $oldValue |
||
129 | * @return string |
||
130 | */ |
||
131 | 13 | private function prepareFilePath($file, $oldValue) |
|
141 | |||
142 | /** |
||
143 | * Prepare the id of the file |
||
144 | * |
||
145 | * @param mixed $file |
||
146 | * @param mixed $oldValue |
||
147 | * @return int |
||
148 | */ |
||
149 | 5 | private function prepareFileId($file, $oldValue) |
|
159 | |||
160 | /** |
||
161 | * Get the path to the upload directory |
||
162 | * |
||
163 | * @param string $attribute |
||
164 | * @return string |
||
165 | */ |
||
166 | 31 | public function uploadDir($attribute) |
|
174 | |||
175 | /** |
||
176 | * Get the type of the owner in as string |
||
177 | * |
||
178 | * @param string $attribute |
||
179 | * @return string |
||
180 | */ |
||
181 | 31 | private function getStringOwnerType($attribute) |
|
185 | |||
186 | /** |
||
187 | * Get the type of the owner |
||
188 | * |
||
189 | * @param string $attribute |
||
190 | * @return int |
||
191 | */ |
||
192 | 31 | public function getFileOwnerType($attribute) |
|
196 | |||
197 | /** |
||
198 | * Get files |
||
199 | * |
||
200 | * @param string $attribute |
||
201 | * @return array |
||
202 | */ |
||
203 | 9 | public function getFiles($attribute) |
|
212 | |||
213 | /** |
||
214 | * Get the file |
||
215 | * |
||
216 | * @param string $attribute |
||
217 | * @return File|null |
||
218 | */ |
||
219 | 1 | public function getFile($attribute) |
|
225 | |||
226 | /** |
||
227 | * Multiple files |
||
228 | * |
||
229 | * @param string $attribute |
||
230 | * @return bool |
||
231 | */ |
||
232 | 27 | public function isMultiple($attribute) |
|
236 | |||
237 | /** |
||
238 | * The file is protected |
||
239 | * |
||
240 | * @param string $attribute |
||
241 | * @return bool |
||
242 | */ |
||
243 | 31 | public function isFileProtected($attribute) |
|
247 | |||
248 | /** |
||
249 | * Get rules |
||
250 | * |
||
251 | * @param string $attribute |
||
252 | * @return array |
||
253 | */ |
||
254 | 33 | public function getFileRules($attribute) |
|
258 | |||
259 | /** |
||
260 | * Get the presets of the file |
||
261 | * |
||
262 | * @param string $attribute |
||
263 | * @return array |
||
264 | */ |
||
265 | 5 | public function getFilePreset($attribute) |
|
269 | |||
270 | /** |
||
271 | * Get the presets of the file for apply after upload |
||
272 | * |
||
273 | * @param string $attribute |
||
274 | * @return array |
||
275 | */ |
||
276 | 31 | public function getFilePresetAfterUpload($attribute) |
|
287 | |||
288 | /** |
||
289 | * Get the storage of the file |
||
290 | * |
||
291 | * @param string $attribute |
||
292 | * @return Storage |
||
293 | */ |
||
294 | 31 | public function getFileStorage($attribute) |
|
303 | |||
304 | /** |
||
305 | * Generate a thumb name |
||
306 | * |
||
307 | * @param string $path |
||
308 | * @param string $prefix |
||
309 | * @return string |
||
310 | */ |
||
311 | 31 | public function generateThumbName($path, $prefix) |
|
316 | |||
317 | /** |
||
318 | * Resize image |
||
319 | * |
||
320 | * @param string $attribute |
||
321 | * @param string $preset |
||
322 | * @param string $pathToFile Use this path to the file |
||
323 | * @param bool $returnRealPath Return the real path to the file |
||
324 | * @return string |
||
325 | */ |
||
326 | 31 | public function thumb($attribute, $preset, $pathToFile = null, $returnRealPath = false) |
|
343 | |||
344 | /** |
||
345 | * Get description the rules of the file in as text |
||
346 | * |
||
347 | * @param string $attribute |
||
348 | * @return string |
||
349 | */ |
||
350 | 9 | public function getFileRulesDescription($attribute) |
|
354 | } |
||
355 |
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..