Complex classes like Builder 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 Builder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Builder implements BuilderInterface |
||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * 已生成的图片 |
||
| 18 | * @var Image |
||
| 19 | */ |
||
| 20 | protected $image; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * 验证码存储驱动 |
||
| 24 | * @var Session|SessionInterface |
||
| 25 | */ |
||
| 26 | protected $store; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * 当前使用的验证码生成器驱动 |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $driver; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * 当前验证码的命名空间 |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $name = 'default'; |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * 验证码设置 |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $config = array( |
||
| 46 | /** |
||
| 47 | * 默认验证码长度 |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | 'length' => 4, |
||
| 51 | /** |
||
| 52 | * 默认验证码宽度 |
||
| 53 | * @var int |
||
| 54 | */ |
||
| 55 | 'width' => 150, |
||
| 56 | /** |
||
| 57 | * 默认验证码高度 |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | 'height' => 40, |
||
| 61 | /** |
||
| 62 | * 指定文字颜色 |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | 'textColor' => null, |
||
| 66 | /** |
||
| 67 | * 文字字体文件 |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | 'textFont' => null, |
||
| 71 | /** |
||
| 72 | * 指定图片背景色 |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | 'backgroundColor' => null, |
||
| 76 | /** |
||
| 77 | * 开启失真模式 |
||
| 78 | * @var bool |
||
| 79 | */ |
||
| 80 | 'distortion' => true, |
||
| 81 | /** |
||
| 82 | * 最大前景线条数 |
||
| 83 | * @var int |
||
| 84 | */ |
||
| 85 | 'maxFrontLines' => null, |
||
| 86 | /** |
||
| 87 | * 最大背景线条数 |
||
| 88 | * @val int |
||
| 89 | */ |
||
| 90 | 'maxBehindLines' => null, |
||
| 91 | /** |
||
| 92 | * 文字最大角度 |
||
| 93 | * @var int |
||
| 94 | */ |
||
| 95 | 'maxAngle' => 8, |
||
| 96 | /** |
||
| 97 | * 文字最大偏移量 |
||
| 98 | * @var int |
||
| 99 | */ |
||
| 100 | 'maxOffset' => 5, |
||
| 101 | /** |
||
| 102 | * 验证码内容 |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | 'charset' => 'abcdefghijklmnpqrstuvwxyz123456789' |
||
| 106 | ); |
||
| 107 | |||
| 108 | |||
| 109 | public function __construct(array $config = array()) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * 设置验证码配置 |
||
| 118 | * @param array $config |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | public function setConfig(array $config) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * 获取配置 |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | public function getConfig() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * 设置验证码高度 |
||
| 142 | * @param $height |
||
| 143 | * @return $this |
||
| 144 | */ |
||
| 145 | public function height($height) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * 设置验证码宽度 |
||
| 153 | * @param $width |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | public function width($width) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * 设置验证码长度 |
||
| 164 | * @param $length |
||
| 165 | * @return $this |
||
| 166 | */ |
||
| 167 | public function length($length) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * 设置命名空间 |
||
| 175 | * @param $name |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | public function name($name) |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * 生成验证码 |
||
| 187 | * @param null $code |
||
| 188 | * @return Image |
||
| 189 | */ |
||
| 190 | public function make($code = null) |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * 获取命名空间 |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getName() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * 验证并删除验证码 |
||
| 221 | * @param $input |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function check($input) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * 仅验证验证码正确性 |
||
| 236 | * @param $input |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | protected function test($input) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * 获取存在session中的key名 |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | protected function getFullName() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * 生成验证码 |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | protected function generate() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * 加密字符串 |
||
| 281 | * @param $value |
||
| 282 | * @return bool|string |
||
| 283 | */ |
||
| 284 | private function hash($value) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * 创建验证码图片 |
||
| 297 | * @param $code |
||
| 298 | * @return resource |
||
| 299 | */ |
||
| 300 | protected function build($code) |
||
| 301 | { |
||
| 302 | //随机取一个字体 |
||
| 303 | $font = $this->getTextFont(); |
||
| 304 | |||
| 305 | //根据宽高创建一个背景画布 |
||
| 306 | $image = imagecreatetruecolor($this->config['width'], $this->config['height']); |
||
| 307 | |||
| 308 | if ($this->config['backgroundColor'] == null) { |
||
| 309 | $backgroundColor = imagecolorallocate($image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); |
||
| 310 | } else { |
||
| 311 | $color = $this->config['backgroundColor']; |
||
| 312 | $backgroundColor = imagecolorallocate($image, $color[0], $color[1], $color[2]); |
||
| 313 | } |
||
| 314 | //填充背景色 |
||
| 315 | imagefill($image, 0, 0, $backgroundColor); |
||
| 316 | |||
| 317 | //绘制背景干扰线 |
||
| 318 | $this->drawLines($image, $this->config['maxBehindLines']); |
||
| 319 | |||
| 320 | //写入验证码文字 |
||
| 321 | $color = $this->renderText($image, $code, $font); |
||
| 322 | |||
| 323 | //绘制前景干扰线 |
||
| 324 | $this->drawLines($image, $this->config['maxFrontLines'], $color); |
||
| 325 | |||
| 326 | |||
| 327 | if ($this->config['distortion']) { |
||
| 328 | //创建失真 |
||
| 329 | $image = $this->distort($image, $this->config['width'], $this->config['height'], $backgroundColor); |
||
| 330 | } |
||
| 331 | |||
| 332 | //如果不指定字体颜色和背景颜色,则使用图像过滤器修饰 |
||
| 333 | if (function_exists('imagefilter') && is_null($this->config['backgroundColor']) && is_null($this->config['textColor'])) { |
||
| 334 | //颜色翻转 - 1/2几率 |
||
| 335 | if (mt_rand(0, 1) == 0) { |
||
| 336 | imagefilter($image, IMG_FILTER_NEGATE); |
||
| 337 | } |
||
| 338 | //用边缘检测来突出图像的边缘 - 1/11几率 |
||
| 339 | if (mt_rand(0, 10) == 0) { |
||
| 340 | imagefilter($image, IMG_FILTER_EDGEDETECT); |
||
| 341 | } |
||
| 342 | //改变图像的对比度 |
||
| 343 | imagefilter($image, IMG_FILTER_CONTRAST, mt_rand(-50, 10)); |
||
| 344 | |||
| 345 | if (mt_rand(0, 5) == 0) { |
||
| 346 | //用高斯算法和指定颜色模糊图像 |
||
| 347 | imagefilter($image, IMG_FILTER_COLORIZE, mt_rand(-80, 50), mt_rand(-80, 50), mt_rand(-80, 50)); |
||
| 348 | } |
||
| 349 | } |
||
| 350 | return $image; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * 获取一个字体 |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | protected function getTextFont() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * 写入验证码到图片中 |
||
| 369 | * @param $image |
||
| 370 | * @param $phrase |
||
| 371 | * @param $font |
||
| 372 | * @return int |
||
| 373 | */ |
||
| 374 | protected function renderText($image, $phrase, $font) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * 画线 |
||
| 411 | * @param $image |
||
| 412 | * @param $width |
||
| 413 | * @param $height |
||
| 414 | * @param null $color |
||
| 415 | */ |
||
| 416 | protected function renderLine($image, $width, $height, $color = null) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * 画线 |
||
| 439 | * @param $image |
||
| 440 | * @param $max |
||
| 441 | * @param $color |
||
| 442 | */ |
||
| 443 | protected function drawLines($image, $max, $color = null) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * 创建失真 |
||
| 468 | * @param $image |
||
| 469 | * @param $width |
||
| 470 | * @param $height |
||
| 471 | * @param $bg |
||
| 472 | * @return resource |
||
| 473 | */ |
||
| 474 | protected function distort($image, $width, $height, $bg) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * 获取颜色 |
||
| 512 | * @param $image |
||
| 513 | * @param $x |
||
| 514 | * @param $y |
||
| 515 | * @param $background |
||
| 516 | * @return int |
||
| 517 | */ |
||
| 518 | protected function getColor($image, $x, $y, $background) |
||
| 528 | |||
| 529 | } |