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 | /** |
||
| 75 | * @var string. Default '@frontend/web/images/%className%/' or '@app/web/images/%className%/' |
||
| 76 | */ |
||
| 77 | public $savePathAlias; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var bool enable/disable crop. |
||
| 81 | */ |
||
| 82 | public $crop = true; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string part of url for image without hostname. Default '/images/%className%/' |
||
| 86 | */ |
||
| 87 | public $urlPrefix; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @inheritdoc |
||
| 91 | */ |
||
| 92 | public function events() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * function for EVENT_BEFORE_VALIDATE |
||
| 104 | */ |
||
| 105 | public function beforeValidate() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * function for EVENT_BEFORE_INSERT and EVENT_BEFORE_UPDATE |
||
| 118 | */ |
||
| 119 | public function beforeSave() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Crop image |
||
| 160 | * @param UploadedFile $file |
||
| 161 | * @param array $coords |
||
| 162 | * @param array $options |
||
| 163 | * @return ManipulatorInterface |
||
| 164 | */ |
||
| 165 | private function crop($file, array $coords, array $options) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param ActiveRecord $object |
||
| 187 | * @return string |
||
| 188 | * @throws \ReflectionException |
||
| 189 | */ |
||
| 190 | private function getShortClassName($object) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $original path to original image |
||
| 198 | * @param array $options with width and height |
||
| 199 | * @return ImageInterface |
||
| 200 | */ |
||
| 201 | private function processImage($original, $options) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $attr name of attribute |
||
| 232 | * @return bool need crop or not |
||
| 233 | */ |
||
| 234 | public function needCrop($attr) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param string $attr name of attribute |
||
| 241 | * @return array|bool false if no coords and array if coords exists |
||
| 242 | * @throws InvalidConfigException |
||
| 243 | */ |
||
| 244 | private function getCoords($attr) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * function for EVENT_BEFORE_DELETE |
||
| 268 | */ |
||
| 269 | public function beforeDelete() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $attr name of attribute |
||
| 279 | * @param bool|string $tmb false or name of thumbnail |
||
| 280 | * @param ActiveRecord $object that keep attrribute. Default $this->owner |
||
| 281 | * @return string url to image |
||
| 282 | * @throws \ReflectionException |
||
| 283 | */ |
||
| 284 | public function getImageUrl($attr, $tmb = false, $object = null) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $attr name of attribute |
||
| 300 | * @throws Exception|\ReflectionException |
||
| 301 | */ |
||
| 302 | private function createDirIfNotExists($attr) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $attr name of attribute |
||
| 312 | * @param bool|string $tmb name of thumbnail |
||
| 313 | * @return string save path |
||
| 314 | * @throws \ReflectionException |
||
| 315 | */ |
||
| 316 | private function getSavePath($attr, $tmb = false) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $attr name of attribute |
||
| 338 | * @param bool|string $tmb name of thumbnail |
||
| 339 | * @param ActiveRecord $object for default prefix |
||
| 340 | * @return string url prefix |
||
| 341 | * @throws \ReflectionException |
||
| 342 | */ |
||
| 343 | private function getUrlPrefix($attr, $tmb = false, $object = null) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Delete images |
||
| 362 | * @param string $attr name of attribute |
||
| 363 | * @throws \ReflectionException |
||
| 364 | */ |
||
| 365 | private function deleteFiles($attr) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param string $attr name of attribute |
||
| 392 | * @return bool isset thumbnails or not |
||
| 393 | */ |
||
| 394 | private function issetThumbnails($attr) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Check, isset attribute or not |
||
| 401 | * @param string $attribute name of attribute |
||
| 402 | * @throws InvalidArgumentException |
||
| 403 | */ |
||
| 404 | private function checkAttrExists($attribute) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param $attr |
||
| 417 | * @param $options |
||
| 418 | */ |
||
| 419 | public static function ensureAttribute(&$attr, &$options) |
||
| 426 | } |
||
| 427 |