Complex classes like Behavior 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 Behavior, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
57 | class Behavior extends \yii\base\Behavior |
||
58 | { |
||
59 | /** |
||
60 | * @var array list of attribute as attributeName => options. Options: |
||
61 | * $width image width |
||
62 | * $height image height |
||
63 | * $savePathAlias @see \maxmirazh33\image\Behavior::$savePathAlias |
||
64 | * $crop @see \maxmirazh33\image\Behavior::$crop |
||
65 | * $urlPrefix @see \maxmirazh33\image\Behavior::$urlPrefix |
||
66 | * $thumbnails - array of thumbnails as prefix => options. Options: |
||
67 | * $width thumbnail width |
||
68 | * $height thumbnail height |
||
69 | * $savePathAlias @see \maxmirazh33\image\Behavior::$savePathAlias |
||
70 | * $urlPrefix @see \maxmirazh33\image\Behavior::$urlPrefix |
||
71 | */ |
||
72 | public $attributes = []; |
||
73 | /** |
||
74 | * @var string. Default '@frontend/web/images/%className%/' or '@app/web/images/%className%/' |
||
75 | */ |
||
76 | public $savePathAlias; |
||
77 | /** |
||
78 | * @var bool enable/disable crop. |
||
79 | */ |
||
80 | public $crop = true; |
||
81 | /** |
||
82 | * @var string part of url for image without hostname. Default '/images/%className%/' |
||
83 | */ |
||
84 | public $urlPrefix; |
||
85 | |||
86 | /** |
||
87 | * @inheritdoc |
||
88 | */ |
||
89 | public function events() |
||
98 | |||
99 | /** |
||
100 | * function for EVENT_BEFORE_VALIDATE |
||
101 | */ |
||
102 | public function beforeValidate() |
||
112 | |||
113 | /** |
||
114 | * function for EVENT_BEFORE_INSERT and EVENT_BEFORE_UPDATE |
||
115 | */ |
||
116 | public function beforeSave() |
||
154 | |||
155 | /** |
||
156 | * Crop image |
||
157 | * @param UploadedFile $file |
||
158 | * @param array $coords |
||
159 | * @param array $options |
||
160 | * @return ManipulatorInterface |
||
161 | */ |
||
162 | private function crop($file, array $coords, array $options) |
||
181 | |||
182 | /** |
||
183 | * @param ActiveRecord $object |
||
184 | * @return string |
||
185 | * @throws \ReflectionException |
||
186 | */ |
||
187 | private function getShortClassName($object) |
||
192 | |||
193 | /** |
||
194 | * @param string $original path to original image |
||
195 | * @param array $options with width and height |
||
196 | * @return ImageInterface |
||
197 | */ |
||
198 | private function processImage($original, $options) |
||
226 | |||
227 | /** |
||
228 | * @param string $attr name of attribute |
||
229 | * @return bool need crop or not |
||
230 | */ |
||
231 | public function needCrop($attr) |
||
235 | |||
236 | /** |
||
237 | * @param string $attr name of attribute |
||
238 | * @return array|bool false if no coords and array if coords exists |
||
239 | * @throws InvalidConfigException |
||
240 | */ |
||
241 | private function getCoords($attr) |
||
262 | |||
263 | /** |
||
264 | * function for EVENT_BEFORE_DELETE |
||
265 | */ |
||
266 | public function beforeDelete() |
||
273 | |||
274 | /** |
||
275 | * @param string $attr name of attribute |
||
276 | * @param bool|string $tmb false or name of thumbnail |
||
277 | * @param ActiveRecord $object that keep attrribute. Default $this->owner |
||
278 | * @return string url to image |
||
279 | */ |
||
280 | public function getImageUrl($attr, $tmb = false, $object = null) |
||
293 | |||
294 | /** |
||
295 | * @param string $attr name of attribute |
||
296 | * @throws Exception |
||
297 | */ |
||
298 | private function createDirIfNotExists($attr) |
||
305 | |||
306 | /** |
||
307 | * @param string $attr name of attribute |
||
308 | * @param bool|string $tmb name of thumbnail |
||
309 | * @return string save path |
||
310 | * @throws \ReflectionException |
||
311 | */ |
||
312 | private function getSavePath($attr, $tmb = false) |
||
333 | |||
334 | /** |
||
335 | * @param string $attr name of attribute |
||
336 | * @param bool|string $tmb name of thumbnail |
||
337 | * @param ActiveRecord $object for default prefix |
||
338 | * @return string url prefix |
||
339 | * @throws \ReflectionException |
||
340 | */ |
||
341 | private function getUrlPrefix($attr, $tmb = false, $object = null) |
||
357 | |||
358 | /** |
||
359 | * Delete images |
||
360 | * @param string $attr name of attribute |
||
361 | * @throws \ReflectionException |
||
362 | */ |
||
363 | private function deleteFiles($attr) |
||
387 | |||
388 | /** |
||
389 | * @param string $attr name of attribute |
||
390 | * @return bool isset thumbnails or not |
||
391 | */ |
||
392 | private function issetThumbnails($attr) |
||
396 | |||
397 | /** |
||
398 | * Check, isset attribute or not |
||
399 | * @param string $attribute name of attribute |
||
400 | * @throws InvalidArgumentException |
||
401 | */ |
||
402 | private function checkAttrExists($attribute) |
||
412 | |||
413 | /** |
||
414 | * @param $attr |
||
415 | * @param $options |
||
416 | */ |
||
417 | public static function ensureAttribute(&$attr, &$options) |
||
424 | } |
||
425 |