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 |
||
| 7 | class Captcha |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * 存储在session中的key |
||
| 11 | */ |
||
| 12 | const SESSION_NAME = '_captcha'; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * 验证码配置 |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected $config = [ |
||
| 19 | /** |
||
| 20 | * 调试模型 |
||
| 21 | */ |
||
| 22 | 'debug' => false, |
||
| 23 | /** |
||
| 24 | * 默认验证码长度 |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | 'length' => 4, |
||
| 28 | /** |
||
| 29 | * 验证码字符集 |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | 'charset' => 'abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789', |
||
| 33 | /** |
||
| 34 | * 是否开启严格模式(区分大小写) |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | 'strict' => false, |
||
| 38 | /** |
||
| 39 | * 默认验证码宽度 |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | 'width' => 150, |
||
| 43 | /** |
||
| 44 | * 默认验证码高度 |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | 'height' => 40, |
||
| 48 | /** |
||
| 49 | * 指定文字颜色 |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | 'textColor' => null, |
||
| 53 | /** |
||
| 54 | * 文字字体文件 |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | 'textFont' => null, |
||
| 58 | /** |
||
| 59 | * 指定图片背景色 |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | 'backgroundColor' => null, |
||
| 63 | /** |
||
| 64 | * 开启失真模式 |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | 'distortion' => true, |
||
| 68 | /** |
||
| 69 | * 最大前景线条数 |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | 'maxFrontLines' => null, |
||
| 73 | /** |
||
| 74 | * 最大背景线条数 |
||
| 75 | * @val int |
||
| 76 | */ |
||
| 77 | 'maxBehindLines' => null, |
||
| 78 | /** |
||
| 79 | * 文字最大角度 |
||
| 80 | * @var int |
||
| 81 | */ |
||
| 82 | 'maxAngle' => 8, |
||
| 83 | /** |
||
| 84 | * 文字最大偏移量 |
||
| 85 | * @var int |
||
| 86 | */ |
||
| 87 | 'maxOffset' => 5 |
||
| 88 | ]; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Captcha constructor. |
||
| 92 | * @param array $config |
||
| 93 | */ |
||
| 94 | public function __construct(array $config = []) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * 设置验证码配置 |
||
| 101 | * |
||
| 102 | * @param array|string $config 配置数组或配置项key |
||
| 103 | * @param mixed $value 配置项值 |
||
| 104 | * @return $this |
||
| 105 | */ |
||
| 106 | public function setConfig($config, $value = null) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * 获取配置 |
||
| 124 | * |
||
| 125 | * @param string|null $key 配置项key |
||
| 126 | * @return string|number|array |
||
| 127 | */ |
||
| 128 | public function getConfig($key = null) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * 生成验证码 |
||
| 139 | * |
||
| 140 | * @return Image |
||
| 141 | */ |
||
| 142 | public function make() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * 仅测试正确性, 不删除验证码 |
||
| 159 | * |
||
| 160 | * @param string $input |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public function test($input) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * 检测正确性,并删除验证码 |
||
| 184 | * |
||
| 185 | * @param string $input |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | public function check($input) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * 生成验证码 |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | protected function generate() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * 创建验证码图片 |
||
| 220 | * |
||
| 221 | * @param string $code |
||
| 222 | * @return resource |
||
| 223 | */ |
||
| 224 | protected function build($code) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * 创建失真 |
||
| 286 | * |
||
| 287 | * @param resource $image |
||
| 288 | * @param int $width |
||
| 289 | * @param int $height |
||
| 290 | * @param int $backgroundColor |
||
| 291 | * @return resource |
||
| 292 | */ |
||
| 293 | protected function createDistortion($image, $width, $height, $backgroundColor) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * 获取一个字体 |
||
| 333 | * |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | protected function getTextFont() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * 写入验证码到图片中 |
||
| 348 | * |
||
| 349 | * @param resource $image |
||
| 350 | * @param string $phrase |
||
| 351 | * @param string $font |
||
| 352 | * @return int |
||
| 353 | */ |
||
| 354 | protected function renderText($image, $phrase, $font) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * 画线 |
||
| 391 | * |
||
| 392 | * @param resource $image |
||
| 393 | * @param int $width |
||
| 394 | * @param int $height |
||
| 395 | * @param int|null $color |
||
| 396 | */ |
||
| 397 | protected function renderLine($image, $width, $height, $color = null) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * 画线 |
||
| 420 | * |
||
| 421 | * @param resource $image |
||
| 422 | * @param int $max |
||
| 423 | * @param int|null $color |
||
| 424 | */ |
||
| 425 | protected function drawLines($image, $max, $color = null) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * 获取颜色 |
||
| 450 | * |
||
| 451 | * @param resource $image |
||
| 452 | * @param int $width |
||
| 453 | * @param int $height |
||
| 454 | * @param int $background |
||
| 455 | * @return int |
||
| 456 | */ |
||
| 457 | protected function getColor($image, $width, $height, $background) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param string $name |
||
| 470 | * @param array $arguments |
||
| 471 | * @return $this |
||
| 472 | */ |
||
| 473 | public function __call($name, $arguments) |
||
| 481 | } |