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() |
|
| 95 | { |
||
| 96 | 8 | if (!class_exists(Image::class)) { |
|
| 97 | throw new NotSupportedException("Yii2-imagine extension is required to use the UploadImageBehavior"); |
||
| 98 | } |
||
| 99 | |||
| 100 | 8 | parent::init(); |
|
| 101 | |||
| 102 | 8 | if ($this->thumbPath === null) { |
|
| 103 | 8 | $this->thumbPath = $this->path; |
|
|
|
|||
| 104 | } |
||
| 105 | 8 | if ($this->thumbUrl === null) { |
|
| 106 | 8 | $this->thumbUrl = $this->url; |
|
| 107 | } |
||
| 108 | |||
| 109 | 8 | foreach ($this->thumbs as $config) { |
|
| 110 | 8 | $width = ArrayHelper::getValue($config, 'width'); |
|
| 111 | 8 | $height = ArrayHelper::getValue($config, 'height'); |
|
| 112 | 8 | if ($height < 1 && $width < 1) { |
|
| 113 | throw new InvalidConfigException(sprintf( |
||
| 114 | 'Length of either side of thumb cannot be 0 or negative, current size ' . |
||
| 115 | 'is %sx%s', $width, $height |
||
| 116 | )); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | 8 | } |
|
| 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 | 5 | public function getThumbUploadUrl($attribute, $profile = 'thumb') |
|
| 129 | { |
||
| 130 | /** @var BaseActiveRecord $model */ |
||
| 131 | 5 | $model = $this->owner; |
|
| 132 | |||
| 133 | 5 | if ($this->attribute !== $attribute) { |
|
| 134 | 3 | $behaviors = $model->getBehaviors(); |
|
| 135 | |||
| 136 | 3 | foreach ($behaviors as $behavior) { |
|
| 137 | 3 | if ($behavior instanceof UploadImageBehavior) { |
|
| 138 | 3 | if ($behavior->attribute == $attribute) { |
|
| 139 | 2 | return $behavior->getThumbUploadUrl($attribute, $profile); |
|
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | 5 | if (!$model->getAttribute($attribute)) { |
|
| 146 | 3 | if ($this->placeholder) { |
|
| 147 | 3 | return $this->getPlaceholderUrl($profile); |
|
| 148 | } else { |
||
| 149 | return null; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | 4 | $path = $this->getUploadPath($attribute, true); |
|
| 154 | |||
| 155 | //if original file exist - generate profile thumb and generate url to thumb |
||
| 156 | 4 | if (is_file($path) || !$this->deleteOriginalFile) { |
|
| 157 | 4 | if ($this->createThumbsOnRequest) { |
|
| 158 | 4 | $this->createThumbs($profile); |
|
| 159 | } |
||
| 160 | 4 | return $this->getThumbProfileUrl($attribute, $profile, $model); |
|
| 161 | } //if original file is deleted generate url to thumb |
||
| 162 | elseif ($this->deleteOriginalFile) { |
||
| 163 | return $this->getThumbProfileUrl($attribute, $profile, $model); |
||
| 164 | } elseif ($this->placeholder) { |
||
| 165 | return $this->getPlaceholderUrl($profile); |
||
| 166 | } else { |
||
| 167 | return null; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param $profile |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | 3 | protected function getPlaceholderUrl($profile) |
|
| 176 | { |
||
| 177 | 3 | list ($path, $url) = Yii::$app->assetManager->publish($this->placeholder); |
|
| 178 | 3 | $filename = basename($path); |
|
| 179 | 3 | $thumb = $this->getThumbFileName($filename, $profile); |
|
| 180 | 3 | $thumbPath = dirname($path) . DIRECTORY_SEPARATOR . $thumb; |
|
| 181 | 3 | $thumbUrl = dirname($url) . '/' . $thumb; |
|
| 182 | |||
| 183 | 3 | if (!is_file($thumbPath)) { |
|
| 184 | 1 | $this->generateImageThumb($this->thumbs[$profile], $path, $thumbPath); |
|
| 185 | } |
||
| 186 | |||
| 187 | 3 | return $thumbUrl; |
|
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param $attribute |
||
| 192 | * @param $profile |
||
| 193 | * @param BaseActiveRecord $model |
||
| 194 | * @return bool|string |
||
| 195 | */ |
||
| 196 | 4 | protected function getThumbProfileUrl($attribute, $profile, BaseActiveRecord $model) |
|
| 197 | { |
||
| 198 | 4 | $url = $this->resolvePath($this->thumbUrl); |
|
| 199 | 4 | $fileName = $model->getOldAttribute($attribute); |
|
| 200 | 4 | $thumbName = $this->getThumbFileName($fileName, $profile); |
|
| 201 | |||
| 202 | 4 | return Yii::getAlias($url . '/' . $thumbName); |
|
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @inheritdoc |
||
| 207 | */ |
||
| 208 | 5 | 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 | 4 | 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 | 4 | public function getThumbUploadPath($attribute, $profile = 'thumb', $old = false) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @param $filename |
||
| 268 | * @param string $profile |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | 5 | protected function getThumbFileName($filename, $profile = 'thumb') |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param $config |
||
| 278 | * @param $path |
||
| 279 | * @param $thumbPath |
||
| 280 | */ |
||
| 281 | 5 | protected function generateImageThumb($config, $path, $thumbPath) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * @inheritdoc |
||
| 309 | */ |
||
| 310 | 3 | 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..