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 | 8 | public function afterSave() |
|
116 | |||
117 | /** |
||
118 | * @internal |
||
119 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
120 | */ |
||
121 | public function beforeDelete() |
||
127 | |||
128 | 1 | public function clearState($attribute, $files) |
|
151 | |||
152 | 25 | private function setState($attribute, $file) |
|
162 | |||
163 | /** |
||
164 | * for models with single upload only |
||
165 | * @param $attribute |
||
166 | * @param $file |
||
167 | * @param $defaultValue |
||
168 | */ |
||
169 | private function setValue($attribute, $file, $defaultValue) |
||
187 | |||
188 | /** |
||
189 | * Generate a thumb |
||
190 | * |
||
191 | * @param string $attribute The attribute name |
||
192 | * @param string $preset The preset name |
||
193 | * @param string $path The file path |
||
194 | * @return string The thumb path |
||
195 | */ |
||
196 | private function generateThumb($attribute, $preset, $path) |
||
209 | |||
210 | /** |
||
211 | * Generate file path by template |
||
212 | * |
||
213 | * @param string $attribute The attribute name |
||
214 | * @param ActiveRecord $file The file model |
||
215 | * @return string The file path |
||
216 | */ |
||
217 | 1 | private function templatePath($attribute, $file = null) |
|
237 | |||
238 | /** |
||
239 | * Get relation |
||
240 | * |
||
241 | * @param string $attribute The attribute name |
||
242 | * @return \ActiveQuery |
||
243 | */ |
||
244 | 2 | public function fileRelation($attribute) |
|
251 | |||
252 | /** |
||
253 | * Get file option |
||
254 | * |
||
255 | * @param string $attribute The attribute name |
||
256 | * @param string $option Option name |
||
257 | * @param mixed $defaultValue Default value |
||
258 | * @return mixed |
||
259 | */ |
||
260 | 30 | public function fileOption($attribute, $option, $defaultValue = null) |
|
264 | |||
265 | /** |
||
266 | * Get file storage |
||
267 | * |
||
268 | * @param string $attribute The attribute name |
||
269 | * @return \Flysystem |
||
270 | */ |
||
271 | 26 | public function fileStorage($attribute) |
|
275 | |||
276 | /** |
||
277 | * Get file path |
||
278 | * |
||
279 | * @param string $attribute The attribute name |
||
280 | * @param ActiveRecord $file Use this file model |
||
281 | * @return string The file path |
||
282 | */ |
||
283 | public function filePath($attribute, $file = null) |
||
288 | |||
289 | /** |
||
290 | * Get file url |
||
291 | * |
||
292 | * @param string $attribute The attribute name |
||
293 | * @param ActiveRecord $file Use this file model |
||
294 | * @return string The file url |
||
295 | */ |
||
296 | public function fileUrl($attribute, $file = null) |
||
301 | |||
302 | /** |
||
303 | * Get extra fields of file |
||
304 | * |
||
305 | * @param string $attribute The attribute name |
||
306 | * @return array |
||
307 | */ |
||
308 | public function fileExtraFields($attribute) |
||
316 | |||
317 | /** |
||
318 | * Get files |
||
319 | * |
||
320 | * @param string $attribute The attribute name |
||
321 | * @return \ActiveRecord[] The file models |
||
322 | */ |
||
323 | 1 | public function files($attribute) |
|
327 | |||
328 | /** |
||
329 | * Get the file |
||
330 | * |
||
331 | * @param string $attribute The attribute name |
||
332 | * @return \ActiveRecord The file model |
||
333 | */ |
||
334 | 1 | public function file($attribute) |
|
338 | |||
339 | /** |
||
340 | * Get rules |
||
341 | * |
||
342 | * @param string $attribute The attribute name |
||
343 | * @param bool $onlyCoreValidators Only core validators |
||
344 | * @return array |
||
345 | */ |
||
346 | 29 | public function fileRules($attribute, $onlyCoreValidators = false) |
|
355 | |||
356 | /** |
||
357 | * Get file state |
||
358 | * |
||
359 | * @param string $attribute The attribute name |
||
360 | * @return array |
||
361 | */ |
||
362 | public function fileState($attribute) |
||
383 | |||
384 | /** |
||
385 | * Get the presets of the file for apply after upload |
||
386 | * |
||
387 | * @param string $attribute The attribute name |
||
388 | * @return array |
||
389 | */ |
||
390 | public function filePresetAfterUpload($attribute) |
||
398 | |||
399 | /** |
||
400 | * Create a thumb and return url |
||
401 | * |
||
402 | * @param string $attribute The attribute name |
||
403 | * @param string $preset The preset name |
||
404 | * @param ActiveRecord $file Use this file model |
||
405 | * @return string The file url |
||
406 | */ |
||
407 | public function thumbUrl($attribute, $preset, $file = null) |
||
414 | |||
415 | /** |
||
416 | * Create a thumb and return full path |
||
417 | * |
||
418 | * @param string $attribute The attribute name |
||
419 | * @param string $preset The preset name |
||
420 | * @param ActiveRecord $file Use this file model |
||
421 | * @return string The file path |
||
422 | */ |
||
423 | public function thumbPath($attribute, $preset, $file = null) |
||
430 | |||
431 | /** |
||
432 | * Create a file |
||
433 | * |
||
434 | * @param string $attribute The attribute name |
||
435 | * @param string $path The file path |
||
436 | * @param string $name The file name |
||
437 | * @return \ActiveRecord The file model |
||
438 | */ |
||
439 | 25 | public function createFile($attribute, $path, $name) |
|
455 | |||
456 | /** |
||
457 | * Create a file from remote URL |
||
458 | * |
||
459 | * @author Sergii Gamaiunov <[email protected]> |
||
460 | * |
||
461 | * @param string $attribute The attribute name |
||
462 | * @param \igogo5yo\uploadfromurl\UploadFromUrl $remoteFile |
||
463 | * @param string $name The file name |
||
464 | * @return \ActiveRecord The file model |
||
465 | */ |
||
466 | public function createRemoteFile($attribute, $remoteFile, $name) |
||
488 | } |
||
489 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.