Complex classes like UploadImageBehavior 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 UploadImageBehavior, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | class UploadImageBehavior extends UploadBehavior |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | public $placeholder; |
||
| 56 | /** |
||
| 57 | * create all thumbs profiles on image upload |
||
| 58 | * @var boolean |
||
| 59 | */ |
||
| 60 | public $createThumbsOnSave = true; |
||
| 61 | /** |
||
| 62 | * create thumb only for profile request by getThumbUploadUrl() method |
||
| 63 | * @var boolean |
||
| 64 | */ |
||
| 65 | public $createThumbsOnRequest = false; |
||
| 66 | /** |
||
| 67 | * Whether delete original uploaded image after thumbs generating. |
||
| 68 | * Defaults to FALSE |
||
| 69 | * @var boolean |
||
| 70 | */ |
||
| 71 | public $deleteOriginalFile = false; |
||
| 72 | /** |
||
| 73 | * @var array the thumbnail profiles |
||
| 74 | * - `width` |
||
| 75 | * - `height` |
||
| 76 | * - `quality` |
||
| 77 | */ |
||
| 78 | public $thumbs = [ |
||
| 79 | 'thumb' => ['width' => 200, 'height' => 200, 'quality' => 90], |
||
| 80 | ]; |
||
| 81 | /** |
||
| 82 | * @var string|null |
||
| 83 | */ |
||
| 84 | public $thumbPath; |
||
| 85 | /** |
||
| 86 | * @var string|null |
||
| 87 | */ |
||
| 88 | public $thumbUrl; |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * @inheritdoc |
||
| 93 | */ |
||
| 94 | 8 | public function init() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $attribute |
||
| 123 | * @param string $profile |
||
| 124 | * @return string|null |
||
| 125 | * @throws \yii\base\Exception |
||
| 126 | * @throws \yii\base\InvalidConfigException |
||
| 127 | */ |
||
| 128 | 1 | public function getThumbUploadUrl($attribute, $profile = 'thumb') |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @param $profile |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | 1 | protected function getPlaceholderUrl($profile) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param $attribute |
||
| 192 | * @param $profile |
||
| 193 | * @param BaseActiveRecord $model |
||
| 194 | * @return bool|string |
||
| 195 | */ |
||
| 196 | protected function getThumbProfileUrl($attribute, $profile, BaseActiveRecord $model) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @inheritdoc |
||
| 207 | */ |
||
| 208 | 1 | protected function afterUpload() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $needed_profile - profile name to create thumb |
||
| 218 | * @throws \yii\base\Exception |
||
| 219 | * @throws \yii\base\InvalidConfigException |
||
| 220 | */ |
||
| 221 | protected function createThumbs($needed_profile = false) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param string $attribute |
||
| 250 | * @param string $profile |
||
| 251 | * @param boolean $old |
||
| 252 | * @return string |
||
| 253 | * @throws \yii\base\InvalidConfigException |
||
| 254 | */ |
||
| 255 | public function getThumbUploadPath($attribute, $profile = 'thumb', $old = false) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param $filename |
||
| 268 | * @param string $profile |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | protected function getThumbFileName($filename, $profile = 'thumb') |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param $config |
||
| 278 | * @param $path |
||
| 279 | * @param $thumbPath |
||
| 280 | */ |
||
| 281 | protected function generateImageThumb($config, $path, $thumbPath) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @inheritdoc |
||
| 309 | */ |
||
| 310 | protected function delete($attribute, $old = false) |
||
| 319 | } |
||
| 320 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..