Complex classes like Captcha 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 Captcha, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Captcha |
||
| 29 | { |
||
| 30 | |||
| 31 | protected $config = [ |
||
| 32 | /** |
||
| 33 | * 默认验证码长度 |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | 'length' => 4, |
||
| 37 | /** |
||
| 38 | * 验证码字符集 |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | 'charset' => 'abcdefghijklmnpqrstuvwxyz123456789', |
||
| 42 | /** |
||
| 43 | * 默认验证码宽度 |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | 'width' => 150, |
||
| 47 | /** |
||
| 48 | * 默认验证码高度 |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | 'height' => 40, |
||
| 52 | /** |
||
| 53 | * 指定文字颜色 |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | 'textColor' => null, |
||
| 57 | /** |
||
| 58 | * 文字字体文件 |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | 'textFont' => null, |
||
| 62 | /** |
||
| 63 | * 指定图片背景色 |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | 'backgroundColor' => null, |
||
| 67 | /** |
||
| 68 | * 开启失真模式 |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | 'distortion' => true, |
||
| 72 | /** |
||
| 73 | * 最大前景线条数 |
||
| 74 | * @var int |
||
| 75 | */ |
||
| 76 | 'maxFrontLines' => null, |
||
| 77 | /** |
||
| 78 | * 最大背景线条数 |
||
| 79 | * @val int |
||
| 80 | */ |
||
| 81 | 'maxBehindLines' => null, |
||
| 82 | /** |
||
| 83 | * 文字最大角度 |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | 'maxAngle' => 8, |
||
| 87 | /** |
||
| 88 | * 文字最大偏移量 |
||
| 89 | * @var int |
||
| 90 | */ |
||
| 91 | 'maxOffset' => 5 |
||
| 92 | ]; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * 存储驱动 |
||
| 96 | * @var Session |
||
| 97 | */ |
||
| 98 | protected $store; |
||
| 99 | |||
| 100 | |||
| 101 | public function __construct(array $config = [], Session $store = null) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * 设置验证码配置 |
||
| 115 | * |
||
| 116 | * @param array|string $config 配置数组或配置项key |
||
| 117 | * @param mixed $value 配置项值 |
||
| 118 | * @return $this |
||
| 119 | */ |
||
| 120 | public function setConfig($config, $value = null) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * 获取配置 |
||
| 137 | * |
||
| 138 | * @param string|null $key 配置项key |
||
| 139 | * @return string|number|array |
||
| 140 | */ |
||
| 141 | public function getConfig($key = null) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * 生成验证码 |
||
| 152 | * |
||
| 153 | * @return Image |
||
| 154 | */ |
||
| 155 | public function make() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * 仅测试正确性, 不删除验证码 |
||
| 169 | * |
||
| 170 | * @param string $input |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | public function test($input) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * 检测正确性,并删除验证码 |
||
| 186 | * |
||
| 187 | * @param string $input |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | public function check($input) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * 生成验证码 |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | protected function generate() |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * 加密字符串 |
||
| 220 | * |
||
| 221 | * @param string $value |
||
| 222 | * @return bool|string |
||
| 223 | */ |
||
| 224 | protected function hash($value) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * 返回存储到session中的键全名 |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | protected function getStoreName() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * 是否有存储的验证码 |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | protected function has() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * 存储验证码 |
||
| 257 | * |
||
| 258 | * @param $code |
||
| 259 | */ |
||
| 260 | protected function store($code) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * 从存储中获取验证码 |
||
| 267 | * |
||
| 268 | * @return mixed |
||
| 269 | */ |
||
| 270 | protected function get() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * 从存储中删除验证码 |
||
| 277 | * |
||
| 278 | * @return mixed |
||
| 279 | */ |
||
| 280 | protected function remove() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * 创建验证码图片 |
||
| 287 | * |
||
| 288 | * @param string $code |
||
| 289 | * @return resource |
||
| 290 | */ |
||
| 291 | protected function build($code) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * 获取一个字体 |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | protected function getTextFont() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * 写入验证码到图片中 |
||
| 370 | * |
||
| 371 | * @param resource $image |
||
| 372 | * @param string $phrase |
||
| 373 | * @param string $font |
||
| 374 | * @return int |
||
| 375 | */ |
||
| 376 | protected function renderText($image, $phrase, $font) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * 画线 |
||
| 413 | * |
||
| 414 | * @param resource $image |
||
| 415 | * @param int $width |
||
| 416 | * @param int $height |
||
| 417 | * @param int|null $color |
||
| 418 | */ |
||
| 419 | protected function renderLine($image, $width, $height, $color = null) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * 画线 |
||
| 442 | * |
||
| 443 | * @param resource $image |
||
| 444 | * @param int $max |
||
| 445 | * @param int|null $color |
||
| 446 | */ |
||
| 447 | protected function drawLines($image, $max, $color = null) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * 创建失真 |
||
| 472 | * |
||
| 473 | * @param resource $image |
||
| 474 | * @param int $width |
||
| 475 | * @param int $height |
||
| 476 | * @param int $bg |
||
| 477 | * @return resource |
||
| 478 | */ |
||
| 479 | protected function distort($image, $width, $height, $bg) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * 获取颜色 |
||
| 517 | * |
||
| 518 | * @param resource $image |
||
| 519 | * @param int $x |
||
| 520 | * @param int $y |
||
| 521 | * @param int $background |
||
| 522 | * @return int |
||
| 523 | */ |
||
| 524 | protected function getColor($image, $x, $y, $background) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @param string $name |
||
| 537 | * @param array $arguments |
||
| 538 | * @return $this |
||
| 539 | */ |
||
| 540 | public function __call($name, $arguments) |
||
| 548 | } |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: