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 | 2 | public function afterSave() |
|
115 | |||
116 | /** |
||
117 | * @internal |
||
118 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
119 | */ |
||
120 | public function beforeDelete() |
||
126 | |||
127 | 1 | public function clearState($attribute, $files) |
|
147 | |||
148 | 25 | private function setState($attribute, $file) |
|
158 | |||
159 | private function setValue($attribute, $file, $defaultValue) |
||
177 | |||
178 | /** |
||
179 | * Generate a thumb |
||
180 | * |
||
181 | * @param string $attribute The attribute name |
||
182 | * @param string $preset The preset name |
||
183 | * @param string $path The file path |
||
184 | * @return string The thumb path |
||
185 | */ |
||
186 | private function generateThumb($attribute, $preset, $path) |
||
199 | |||
200 | /** |
||
201 | * Generate file path by template |
||
202 | * |
||
203 | * @param string $attribute The attribute name |
||
204 | * @param ActiveRecord $file The file model |
||
205 | * @return string The file path |
||
206 | */ |
||
207 | private function templatePath($attribute, $file = null) |
||
227 | |||
228 | /** |
||
229 | * Get relation |
||
230 | * |
||
231 | * @param string $attribute The attribute name |
||
232 | * @return \ActiveQuery |
||
233 | */ |
||
234 | 2 | public function fileRelation($attribute) |
|
241 | |||
242 | /** |
||
243 | * Get file option |
||
244 | * |
||
245 | * @param string $attribute The attribute name |
||
246 | * @param string $option Option name |
||
247 | * @param mixed $defaultValue Default value |
||
248 | * @return mixed |
||
249 | */ |
||
250 | 30 | public function fileOption($attribute, $option, $defaultValue = null) |
|
254 | |||
255 | /** |
||
256 | * Get file storage |
||
257 | * |
||
258 | * @param string $attribute The attribute name |
||
259 | * @return \Flysystem |
||
260 | */ |
||
261 | 26 | public function fileStorage($attribute) |
|
265 | |||
266 | /** |
||
267 | * Get file path |
||
268 | * |
||
269 | * @param string $attribute The attribute name |
||
270 | * @param ActiveRecord $file Use this file model |
||
271 | * @return string The file path |
||
272 | */ |
||
273 | public function filePath($attribute, $file = null) |
||
278 | |||
279 | /** |
||
280 | * Get file url |
||
281 | * |
||
282 | * @param string $attribute The attribute name |
||
283 | * @param ActiveRecord $file Use this file model |
||
284 | * @return string The file url |
||
285 | */ |
||
286 | public function fileUrl($attribute, $file = null) |
||
291 | |||
292 | /** |
||
293 | * Get extra fields of file |
||
294 | * |
||
295 | * @param string $attribute The attribute name |
||
296 | * @return array |
||
297 | */ |
||
298 | public function fileExtraFields($attribute) |
||
306 | |||
307 | /** |
||
308 | * Get files |
||
309 | * |
||
310 | * @param string $attribute The attribute name |
||
311 | * @return \ActiveRecord[] The file models |
||
312 | */ |
||
313 | 1 | public function files($attribute) |
|
317 | |||
318 | /** |
||
319 | * Get the file |
||
320 | * |
||
321 | * @param string $attribute The attribute name |
||
322 | * @return \ActiveRecord The file model |
||
323 | */ |
||
324 | 1 | public function file($attribute) |
|
328 | |||
329 | /** |
||
330 | * Get rules |
||
331 | * |
||
332 | * @param string $attribute The attribute name |
||
333 | * @param bool $onlyCoreValidators Only core validators |
||
334 | * @return array |
||
335 | */ |
||
336 | 29 | public function fileRules($attribute, $onlyCoreValidators = false) |
|
345 | |||
346 | /** |
||
347 | * Get file state |
||
348 | * |
||
349 | * @param string $attribute The attribute name |
||
350 | * @return array |
||
351 | */ |
||
352 | public function fileState($attribute) |
||
371 | |||
372 | /** |
||
373 | * Get the presets of the file for apply after upload |
||
374 | * |
||
375 | * @param string $attribute The attribute name |
||
376 | * @return array |
||
377 | */ |
||
378 | public function filePresetAfterUpload($attribute) |
||
386 | |||
387 | /** |
||
388 | * Create a thumb and return url |
||
389 | * |
||
390 | * @param string $attribute The attribute name |
||
391 | * @param string $preset The preset name |
||
392 | * @param ActiveRecord $file Use this file model |
||
393 | * @return string The file url |
||
394 | */ |
||
395 | public function thumbUrl($attribute, $preset, $file = null) |
||
402 | |||
403 | /** |
||
404 | * Create a thumb and return full path |
||
405 | * |
||
406 | * @param string $attribute The attribute name |
||
407 | * @param string $preset The preset name |
||
408 | * @param ActiveRecord $file Use this file model |
||
409 | * @return string The file path |
||
410 | */ |
||
411 | public function thumbPath($attribute, $preset, $file = null) |
||
418 | |||
419 | /** |
||
420 | * Create a file |
||
421 | * |
||
422 | * @param string $attribute The attribute name |
||
423 | * @param string $path The file path |
||
424 | * @param string $name The file name |
||
425 | * @return \ActiveRecord The file model |
||
426 | */ |
||
427 | 25 | public function createFile($attribute, $path, $name) |
|
443 | } |
||
444 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.