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 |
||
16 | class FileBehavior extends Behavior |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | public $attributes = []; |
||
22 | /** |
||
23 | * @var ActiveQuery |
||
24 | */ |
||
25 | private $relation; |
||
26 | /** |
||
27 | * @var FileBind |
||
28 | */ |
||
29 | private $fileBind; |
||
30 | |||
31 | /** |
||
32 | * @internal |
||
33 | */ |
||
34 | 31 | public function init() |
|
42 | |||
43 | /** |
||
44 | * @inheritdoc |
||
45 | * @internal |
||
46 | */ |
||
47 | 31 | public function events() |
|
57 | |||
58 | /** |
||
59 | * @internal |
||
60 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
61 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
62 | */ |
||
63 | 22 | public function beforeSave($insert) |
|
73 | |||
74 | /** |
||
75 | * @internal |
||
76 | */ |
||
77 | 24 | public function afterSave() |
|
114 | |||
115 | /** |
||
116 | * @internal |
||
117 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
118 | */ |
||
119 | 1 | public function beforeDelete() |
|
125 | |||
126 | 21 | private function clearState($attribute) |
|
132 | |||
133 | 25 | private function setState($attribute, $file) |
|
142 | |||
143 | 21 | private function setValue($attribute, $file, $defaultValue) |
|
161 | |||
162 | /** |
||
163 | * Generate a thumb |
||
164 | * |
||
165 | * @param string $attribute The attribute name |
||
166 | * @param string $preset The preset name |
||
167 | * @param string $path The file path |
||
168 | * @return string The thumb path |
||
169 | */ |
||
170 | 25 | private function generateThumb($attribute, $preset, $path) |
|
183 | |||
184 | /** |
||
185 | * Generate file path by template |
||
186 | * |
||
187 | * @param string $attribute The attribute name |
||
188 | * @param ActiveRecord $file The file model |
||
189 | * @return string The file path |
||
190 | */ |
||
191 | 25 | private function templatePath($attribute, $file = null) |
|
192 | { |
||
193 | 25 | $value = $this->owner->{$attribute}; |
|
194 | |||
195 | 25 | $saveFilePath = $this->fileOption($attribute, 'saveFilePathInAttribute'); |
|
196 | 25 | $isFilledPath = $saveFilePath && !empty($value); |
|
197 | |||
198 | 25 | $saveFileId = $this->fileOption($attribute, 'saveFileIdInAttribute'); |
|
199 | 25 | $isFilledId = $saveFileId && is_numeric($value) && $value; |
|
200 | |||
201 | 25 | if (($isFilledPath || $isFilledId) && $file === null) { |
|
202 | 8 | $file = $this->file($attribute); |
|
203 | 8 | } |
|
204 | |||
205 | 25 | if ($file !== null) { |
|
206 | 25 | $handlerTemplatePath = $this->fileOption($attribute, 'templatePath'); |
|
207 | 25 | return $handlerTemplatePath($file); |
|
208 | } |
||
209 | return $value; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Get relation |
||
214 | * |
||
215 | * @param string $attribute The attribute name |
||
216 | * @return \ActiveQuery |
||
217 | */ |
||
218 | 22 | public function fileRelation($attribute) |
|
225 | |||
226 | /** |
||
227 | * Get file option |
||
228 | * |
||
229 | * @param string $attribute The attribute name |
||
230 | * @param string $option Option name |
||
231 | * @param mixed $defaultValue Default value |
||
232 | * @return mixed |
||
233 | */ |
||
234 | 30 | public function fileOption($attribute, $option, $defaultValue = null) |
|
238 | |||
239 | /** |
||
240 | * Get file storage |
||
241 | * |
||
242 | * @param string $attribute The attribute name |
||
243 | * @return \Flysystem |
||
244 | */ |
||
245 | 26 | public function fileStorage($attribute) |
|
249 | |||
250 | /** |
||
251 | * Get file path |
||
252 | * |
||
253 | * @param string $attribute The attribute name |
||
254 | * @param ActiveRecord $file Use this file model |
||
255 | * @return string The file path |
||
256 | */ |
||
257 | 11 | public function filePath($attribute, $file = null) |
|
262 | |||
263 | /** |
||
264 | * Get file url |
||
265 | * |
||
266 | * @param string $attribute The attribute name |
||
267 | * @param ActiveRecord $file Use this file model |
||
268 | * @return string The file url |
||
269 | */ |
||
270 | 25 | public function fileUrl($attribute, $file = null) |
|
275 | |||
276 | /** |
||
277 | * Get extra fields of file |
||
278 | * |
||
279 | * @param string $attribute The attribute name |
||
280 | * @return array |
||
281 | */ |
||
282 | 2 | public function fileExtraFields($attribute) |
|
290 | |||
291 | /** |
||
292 | * Get files |
||
293 | * |
||
294 | * @param string $attribute The attribute name |
||
295 | * @return \ActiveRecord[] The file models |
||
296 | */ |
||
297 | 9 | public function files($attribute) |
|
298 | { |
||
299 | 9 | return $this->fileBind->files($this->owner, $attribute); |
|
300 | } |
||
301 | |||
302 | /** |
||
303 | * Get the file |
||
304 | * |
||
305 | * @param string $attribute The attribute name |
||
306 | * @return \ActiveRecord The file model |
||
307 | */ |
||
308 | 12 | public function file($attribute) |
|
312 | |||
313 | /** |
||
314 | * Get rules |
||
315 | * |
||
316 | * @param string $attribute The attribute name |
||
317 | * @param bool $onlyCoreValidators Only core validators |
||
318 | * @return array |
||
319 | */ |
||
320 | 29 | public function fileRules($attribute, $onlyCoreValidators = false) |
|
329 | |||
330 | /** |
||
331 | * Get file state |
||
332 | * |
||
333 | * @param string $attribute The attribute name |
||
334 | * @return array |
||
335 | */ |
||
336 | 20 | public function fileState($attribute) |
|
341 | |||
342 | /** |
||
343 | * Get the presets of the file for apply after upload |
||
344 | * |
||
345 | * @param string $attribute The attribute name |
||
346 | * @return array |
||
347 | */ |
||
348 | 25 | public function filePresetAfterUpload($attribute) |
|
356 | |||
357 | /** |
||
358 | * Create a thumb and return url |
||
359 | * |
||
360 | * @param string $attribute The attribute name |
||
361 | * @param string $preset The preset name |
||
362 | * @param ActiveRecord $file Use this file model |
||
363 | * @return string The file url |
||
364 | */ |
||
365 | 25 | public function thumbUrl($attribute, $preset, $file = null) |
|
372 | |||
373 | /** |
||
374 | * Create a thumb and return full path |
||
375 | * |
||
376 | * @param string $attribute The attribute name |
||
377 | * @param string $preset The preset name |
||
378 | * @param ActiveRecord $file Use this file model |
||
379 | * @return string The file path |
||
380 | */ |
||
381 | 6 | public function thumbPath($attribute, $preset, $file = null) |
|
388 | |||
389 | /** |
||
390 | * Create a file |
||
391 | * |
||
392 | * @param string $attribute The attribute name |
||
393 | * @param string $path The file path |
||
394 | * @param string $name The file name |
||
395 | * @return \ActiveRecord The file model |
||
396 | */ |
||
397 | 25 | public function createFile($attribute, $path, $name) |
|
413 | } |
||
414 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.