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 |
||
18 | class FileBehavior extends Behavior |
||
19 | { |
||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | public $attributes = []; |
||
24 | |||
25 | /** |
||
26 | * @var ActiveQuery |
||
27 | */ |
||
28 | private $relation; |
||
29 | |||
30 | /** |
||
31 | * @var FileBind |
||
32 | */ |
||
33 | private $fileBind; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected static $classPathMap = []; |
||
39 | |||
40 | /** |
||
41 | * @var string name of application component that represents `user` |
||
42 | */ |
||
43 | public $userComponent = 'user'; |
||
44 | |||
45 | /** |
||
46 | * @since 5.6.0 |
||
47 | * @var bool |
||
48 | */ |
||
49 | protected $markedLinked = false; |
||
50 | |||
51 | /** |
||
52 | * @internal |
||
53 | 31 | */ |
|
54 | public function init() |
||
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | * @internal |
||
66 | 31 | */ |
|
67 | public function events() |
||
77 | |||
78 | /** |
||
79 | * @internal |
||
80 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
81 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
82 | 2 | */ |
|
83 | public function beforeSave($insert) |
||
93 | |||
94 | /** |
||
95 | * @internal |
||
96 | 2 | */ |
|
97 | public function afterSave() |
||
141 | |||
142 | /** |
||
143 | * @internal |
||
144 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
145 | */ |
||
146 | 26 | public function beforeDelete() |
|
155 | |||
156 | 1 | protected function getUser() |
|
163 | |||
164 | public function clearState($attribute, $files) |
||
187 | |||
188 | private function setState($attribute, $file) |
||
198 | |||
199 | /** |
||
200 | * for models with single upload only |
||
201 | * @param $attribute |
||
202 | * @param $file |
||
203 | * @param $defaultValue |
||
204 | */ |
||
205 | private function setValue($attribute, $file, $defaultValue) |
||
223 | |||
224 | /** |
||
225 | * Generate a thumb |
||
226 | * |
||
227 | * @param string $attribute The attribute name |
||
228 | * @param string $preset The preset name |
||
229 | * @param string $path The file path |
||
230 | * @return string The thumb path |
||
231 | */ |
||
232 | private function generateThumb($attribute, $preset, $path) |
||
245 | |||
246 | /** |
||
247 | * Generate file path by template |
||
248 | * |
||
249 | * @param string $attribute The attribute name |
||
250 | * @param ActiveRecord $file The file model |
||
251 | * @return string The file path |
||
252 | */ |
||
253 | private function templatePath($attribute, $file = null) |
||
274 | |||
275 | 2 | /** |
|
276 | * Get relation |
||
277 | * |
||
278 | * @param string $attribute The attribute name |
||
279 | * @return \ActiveQuery |
||
280 | */ |
||
281 | public function fileRelation($attribute) |
||
288 | 30 | ||
289 | /** |
||
290 | * Get file option |
||
291 | * |
||
292 | * @param string $attribute The attribute name |
||
293 | * @param string $option Option name |
||
294 | * @param mixed $defaultValue Default value |
||
295 | * @return mixed |
||
296 | */ |
||
297 | 26 | public function fileOption($attribute, $option, $defaultValue = null) |
|
301 | |||
302 | /** |
||
303 | * Get file storage |
||
304 | * |
||
305 | * @param string $attribute The attribute name |
||
306 | * @return \Flysystem |
||
307 | */ |
||
308 | public function fileStorage($attribute) |
||
312 | |||
313 | /** |
||
314 | * Get file path |
||
315 | * |
||
316 | * @param string $attribute The attribute name |
||
317 | * @param ActiveRecord $file Use this file model |
||
318 | * @return string The file path |
||
319 | */ |
||
320 | public function filePath($attribute, $file = null) |
||
327 | |||
328 | /** |
||
329 | * Get file url |
||
330 | * |
||
331 | * @param string $attribute The attribute name |
||
332 | * @param ActiveRecord $file Use this file model |
||
333 | * @return string The file url |
||
334 | */ |
||
335 | public function fileUrl($attribute, $file = null) |
||
340 | |||
341 | /** |
||
342 | * Get extra fields of file |
||
343 | * |
||
344 | * @param string $attribute The attribute name |
||
345 | * @return array |
||
346 | */ |
||
347 | public function fileExtraFields($attribute) |
||
358 | |||
359 | /** |
||
360 | 1 | * Get files |
|
361 | * |
||
362 | 1 | * @param string $attribute The attribute name |
|
363 | * @return \ActiveRecord[] The file models |
||
364 | */ |
||
365 | public function files($attribute) |
||
372 | 29 | ||
373 | /** |
||
374 | 29 | * Get the file |
|
375 | 29 | * |
|
376 | 28 | * @param string $attribute The attribute name |
|
377 | 28 | * @return \ActiveRecord The file model |
|
378 | */ |
||
379 | 29 | public function file($attribute) |
|
386 | |||
387 | /** |
||
388 | * Get rules |
||
389 | * |
||
390 | * @param string $attribute The attribute name |
||
391 | * @param bool $onlyCoreValidators Only core validators |
||
392 | * @return array |
||
393 | */ |
||
394 | public function fileRules($attribute, $onlyCoreValidators = false) |
||
403 | |||
404 | /** |
||
405 | * Get file state |
||
406 | * |
||
407 | * @param string $attribute The attribute name |
||
408 | * @return array |
||
409 | */ |
||
410 | public function fileState($attribute) |
||
431 | |||
432 | /** |
||
433 | * Get the presets of the file for apply after upload |
||
434 | * |
||
435 | * @param string $attribute The attribute name |
||
436 | * @return array |
||
437 | */ |
||
438 | public function filePresetAfterUpload($attribute) |
||
446 | |||
447 | /** |
||
448 | * Create a thumb and return url |
||
449 | * |
||
450 | * @param string $attribute The attribute name |
||
451 | * @param string $preset The preset name |
||
452 | * @param ActiveRecord $file Use this file model |
||
453 | * @return string The file url |
||
454 | */ |
||
455 | public function thumbUrl($attribute, $preset, $file = null) |
||
462 | |||
463 | /** |
||
464 | * Create a thumb and return full path |
||
465 | 25 | * |
|
466 | * @param string $attribute The attribute name |
||
467 | 25 | * @param string $preset The preset name |
|
468 | 25 | * @param ActiveRecord $file Use this file model |
|
469 | 25 | * @return string The file path |
|
470 | 25 | */ |
|
471 | 25 | public function thumbPath($attribute, $preset, $file = null) |
|
478 | |||
479 | /** |
||
480 | * Create a file |
||
481 | * |
||
482 | * @param string $attribute The attribute name |
||
483 | * @param string $path The file path |
||
484 | * @param string $name The file name |
||
485 | * @return \ActiveRecord The file model |
||
486 | */ |
||
487 | public function createFile($attribute, $path, $name) |
||
509 | |||
510 | /** |
||
511 | * Create a file from remote URL |
||
512 | * |
||
513 | * @author Sergii Gamaiunov <[email protected]> |
||
514 | * |
||
515 | * @param string $attribute The attribute name |
||
516 | * @param \igogo5yo\uploadfromurl\UploadFromUrl $remoteFile |
||
517 | * @param string $name The file name |
||
518 | * @return \ActiveRecord The file model |
||
519 | */ |
||
520 | public function createRemoteFile($attribute, $remoteFile, $name) |
||
544 | |||
545 | /** |
||
546 | * Add class alias to be able to upload files for different versions of a model to a single API endpoint |
||
547 | * |
||
548 | * Example: |
||
549 | * ``` |
||
550 | * class OldCar extends Car |
||
551 | * { |
||
552 | * public function init() |
||
553 | * { |
||
554 | * parent::init(); |
||
555 | * $this->car_type = 'old; |
||
556 | * FileBehavior::addClassAlias(get_class($this), Car::className()); |
||
557 | * } |
||
558 | * |
||
559 | * public function formName() { |
||
560 | * return 'Car'; |
||
561 | * } |
||
562 | * } |
||
563 | * ``` |
||
564 | * @param $source |
||
565 | * @param $mapTo |
||
566 | */ |
||
567 | public static function addClassAlias($source, $mapTo) { |
||
570 | |||
571 | protected static function getClass($source) { |
||
576 | |||
577 | /** |
||
578 | * Mark current upload session as already linked (e.g. file is linked during `createFile`) to avoid duplicate links |
||
579 | * @return $this |
||
580 | * @since 5.6.0 |
||
581 | */ |
||
582 | public function markLinked() { |
||
586 | } |
||
587 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.