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 | /** |
||
25 | * @var ActiveQuery |
||
26 | */ |
||
27 | private $relation; |
||
28 | |||
29 | /** |
||
30 | * @var FileBind |
||
31 | */ |
||
32 | private $fileBind; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected static $classPathMap = []; |
||
38 | |||
39 | /** |
||
40 | * @var string name of application component that represents `user` |
||
41 | */ |
||
42 | public $userComponent = 'user'; |
||
43 | |||
44 | /** |
||
45 | * @internal |
||
46 | */ |
||
47 | 31 | public function init() |
|
55 | |||
56 | /** |
||
57 | * @inheritdoc |
||
58 | * @internal |
||
59 | */ |
||
60 | 31 | public function events() |
|
70 | |||
71 | /** |
||
72 | * @internal |
||
73 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
74 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
75 | */ |
||
76 | 10 | public function beforeSave($insert) |
|
86 | |||
87 | /** |
||
88 | * @internal |
||
89 | */ |
||
90 | 2 | public function afterSave() |
|
128 | |||
129 | /** |
||
130 | * @internal |
||
131 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
132 | */ |
||
133 | public function beforeDelete() |
||
139 | |||
140 | 26 | protected function getUser() |
|
147 | |||
148 | 1 | public function clearState($attribute, $files) |
|
171 | |||
172 | 25 | private function setState($attribute, $file) |
|
182 | |||
183 | /** |
||
184 | * for models with single upload only |
||
185 | * @param $attribute |
||
186 | * @param $file |
||
187 | * @param $defaultValue |
||
188 | */ |
||
189 | private function setValue($attribute, $file, $defaultValue) |
||
207 | |||
208 | /** |
||
209 | * Generate a thumb |
||
210 | * |
||
211 | * @param string $attribute The attribute name |
||
212 | * @param string $preset The preset name |
||
213 | * @param string $path The file path |
||
214 | * @return string The thumb path |
||
215 | */ |
||
216 | 1 | private function generateThumb($attribute, $preset, $path) |
|
229 | |||
230 | /** |
||
231 | * Generate file path by template |
||
232 | * |
||
233 | * @param string $attribute The attribute name |
||
234 | * @param ActiveRecord $file The file model |
||
235 | * @return string The file path |
||
236 | */ |
||
237 | private function templatePath($attribute, $file = null) |
||
257 | |||
258 | /** |
||
259 | * Get relation |
||
260 | * |
||
261 | * @param string $attribute The attribute name |
||
262 | * @return \ActiveQuery |
||
263 | */ |
||
264 | 2 | public function fileRelation($attribute) |
|
271 | |||
272 | /** |
||
273 | * Get file option |
||
274 | * |
||
275 | * @param string $attribute The attribute name |
||
276 | * @param string $option Option name |
||
277 | * @param mixed $defaultValue Default value |
||
278 | * @return mixed |
||
279 | */ |
||
280 | 30 | public function fileOption($attribute, $option, $defaultValue = null) |
|
284 | |||
285 | /** |
||
286 | * Get file storage |
||
287 | * |
||
288 | * @param string $attribute The attribute name |
||
289 | * @return \Flysystem |
||
290 | */ |
||
291 | 26 | public function fileStorage($attribute) |
|
295 | |||
296 | /** |
||
297 | * Get file path |
||
298 | * |
||
299 | * @param string $attribute The attribute name |
||
300 | * @param ActiveRecord $file Use this file model |
||
301 | * @return string The file path |
||
302 | */ |
||
303 | public function filePath($attribute, $file = null) |
||
308 | |||
309 | /** |
||
310 | * Get file url |
||
311 | * |
||
312 | * @param string $attribute The attribute name |
||
313 | * @param ActiveRecord $file Use this file model |
||
314 | * @return string The file url |
||
315 | */ |
||
316 | public function fileUrl($attribute, $file = null) |
||
321 | |||
322 | /** |
||
323 | * Get extra fields of file |
||
324 | * |
||
325 | * @param string $attribute The attribute name |
||
326 | * @return array |
||
327 | */ |
||
328 | public function fileExtraFields($attribute) |
||
336 | |||
337 | /** |
||
338 | * Get files |
||
339 | * |
||
340 | * @param string $attribute The attribute name |
||
341 | * @return \ActiveRecord[] The file models |
||
342 | */ |
||
343 | 2 | public function files($attribute) |
|
347 | |||
348 | /** |
||
349 | * Get the file |
||
350 | * |
||
351 | * @param string $attribute The attribute name |
||
352 | * @return \ActiveRecord The file model |
||
353 | */ |
||
354 | 1 | public function file($attribute) |
|
358 | |||
359 | /** |
||
360 | * Get rules |
||
361 | * |
||
362 | * @param string $attribute The attribute name |
||
363 | * @param bool $onlyCoreValidators Only core validators |
||
364 | * @return array |
||
365 | */ |
||
366 | 29 | public function fileRules($attribute, $onlyCoreValidators = false) |
|
375 | |||
376 | /** |
||
377 | * Get file state |
||
378 | * |
||
379 | * @param string $attribute The attribute name |
||
380 | * @return array |
||
381 | */ |
||
382 | public function fileState($attribute) |
||
403 | |||
404 | /** |
||
405 | * Get the presets of the file for apply after upload |
||
406 | * |
||
407 | * @param string $attribute The attribute name |
||
408 | * @return array |
||
409 | */ |
||
410 | public function filePresetAfterUpload($attribute) |
||
418 | |||
419 | /** |
||
420 | * Create a thumb and return url |
||
421 | * |
||
422 | * @param string $attribute The attribute name |
||
423 | * @param string $preset The preset name |
||
424 | * @param ActiveRecord $file Use this file model |
||
425 | * @return string The file url |
||
426 | */ |
||
427 | public function thumbUrl($attribute, $preset, $file = null) |
||
434 | |||
435 | /** |
||
436 | * Create a thumb and return full path |
||
437 | * |
||
438 | * @param string $attribute The attribute name |
||
439 | * @param string $preset The preset name |
||
440 | * @param ActiveRecord $file Use this file model |
||
441 | * @return string The file path |
||
442 | */ |
||
443 | public function thumbPath($attribute, $preset, $file = null) |
||
450 | |||
451 | /** |
||
452 | * Create a file |
||
453 | * |
||
454 | * @param string $attribute The attribute name |
||
455 | * @param string $path The file path |
||
456 | * @param string $name The file name |
||
457 | * @return \ActiveRecord The file model |
||
458 | */ |
||
459 | 25 | public function createFile($attribute, $path, $name) |
|
475 | |||
476 | /** |
||
477 | * Create a file from remote URL |
||
478 | * |
||
479 | * @author Sergii Gamaiunov <[email protected]> |
||
480 | * |
||
481 | * @param string $attribute The attribute name |
||
482 | * @param \igogo5yo\uploadfromurl\UploadFromUrl $remoteFile |
||
483 | * @param string $name The file name |
||
484 | * @return \ActiveRecord The file model |
||
485 | */ |
||
486 | public function createRemoteFile($attribute, $remoteFile, $name) |
||
508 | |||
509 | /** |
||
510 | * Add class alias to be able to upload files for different versions of a model to a single API endpoint |
||
511 | * |
||
512 | * Example: |
||
513 | * ``` |
||
514 | * class OldCar extends Car |
||
515 | * { |
||
516 | * public function init() |
||
517 | * { |
||
518 | * parent::init(); |
||
519 | * $this->car_type = 'old; |
||
520 | * FileBehavior::addClassAlias(get_class($this), Car::className()); |
||
521 | * } |
||
522 | * |
||
523 | * public function formName() { |
||
524 | * return 'Car'; |
||
525 | * } |
||
526 | * } |
||
527 | * ``` |
||
528 | * @param $source |
||
529 | * @param $mapTo |
||
530 | */ |
||
531 | public static function addClassAlias($source, $mapTo) { |
||
534 | |||
535 | protected static function getClass($source) { |
||
540 | } |
||
541 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.