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 |
||
| 52 | class UploadImageBehavior extends UploadBehavior |
||
| 53 | { |
||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | public $placeholder; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var boolean |
||
| 61 | */ |
||
| 62 | public $createThumbsOnSave = true; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var boolean |
||
| 66 | */ |
||
| 67 | public $createThumbsOnRequest = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Rotates an image automatically based on EXIF information. |
||
| 71 | * @var boolean |
||
| 72 | */ |
||
| 73 | public $autorotate = false; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Whether delete original uploaded image after thumbs generating. |
||
| 77 | * Defaults to FALSE |
||
| 78 | * @var boolean |
||
| 79 | */ |
||
| 80 | public $deleteOriginalFile = false; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array the thumbnail profiles |
||
| 84 | * - `width` |
||
| 85 | * - `height` |
||
| 86 | * - `quality` |
||
| 87 | */ |
||
| 88 | public $thumbs = [ |
||
| 89 | 'thumb' => ['width' => 200, 'height' => 200, 'quality' => 90], |
||
| 90 | ]; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var string|null |
||
| 94 | */ |
||
| 95 | public $thumbPath; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var string|null |
||
| 99 | */ |
||
| 100 | public $thumbUrl; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var ImageInterface |
||
| 104 | */ |
||
| 105 | private $originalImage; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @inheritdoc |
||
| 109 | * @throws NotSupportedException |
||
| 110 | * @throws InvalidConfigException |
||
| 111 | */ |
||
| 112 | public function init() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @inheritdoc |
||
| 132 | * @throws InvalidConfigException |
||
| 133 | * @throws \yii\base\Exception |
||
| 134 | */ |
||
| 135 | protected function afterUpload() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param $path |
||
| 152 | * @throws InvalidConfigException |
||
| 153 | * @throws \yii\base\Exception |
||
| 154 | */ |
||
| 155 | protected function createThumbs($path) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $attribute |
||
| 178 | * @param string $profile |
||
| 179 | * @param boolean $old |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function getThumbUploadPath($attribute, $profile = 'thumb', $old = false) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $attribute |
||
| 195 | * @param string $profile |
||
| 196 | * @return string|null |
||
| 197 | * @throws InvalidConfigException |
||
| 198 | * @throws \yii\base\Exception |
||
| 199 | */ |
||
| 200 | public function getThumbUploadUrl($attribute, $profile = 'thumb') |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param $profile |
||
| 224 | * @return string |
||
| 225 | * @throws InvalidConfigException |
||
| 226 | */ |
||
| 227 | protected function getPlaceholderUrl($profile) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @inheritdoc |
||
| 244 | */ |
||
| 245 | protected function delete($attribute, $old = false) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param $filename |
||
| 260 | * @param string $profile |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | protected function getThumbFileName($filename, $profile = 'thumb') |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param array $config |
||
| 270 | * @param string $path |
||
| 271 | * @param string $thumbPath |
||
| 272 | * @throws InvalidConfigException |
||
| 273 | */ |
||
| 274 | protected function generateImageThumb($config, $path, $thumbPath) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param array $config |
||
| 317 | * @param string $attribute |
||
| 318 | * @param mixed|null $default |
||
| 319 | * @return mixed |
||
| 320 | */ |
||
| 321 | private function getConfigValue($config, $attribute, $default = null) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @return bool|string |
||
| 332 | */ |
||
| 333 | private function getPathOriginalImage() |
||
| 341 | } |
||
| 342 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.