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() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * 创建验证码图片 |
||
| 216 | * |
||
| 217 | * @param string $code |
||
| 218 | * @return resource |
||
| 219 | */ |
||
| 220 | protected function build($code) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * 创建失真 |
||
| 282 | * |
||
| 283 | * @param resource $image |
||
| 284 | * @param int $width |
||
| 285 | * @param int $height |
||
| 286 | * @param int $backgroundColor |
||
| 287 | * @return resource |
||
| 288 | */ |
||
| 289 | protected function createDistortion($image, $width, $height, $backgroundColor) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * 获取一个字体 |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | protected function getTextFont() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * 写入验证码到图片中 |
||
| 344 | * |
||
| 345 | * @param resource $image |
||
| 346 | * @param string $phrase |
||
| 347 | * @param string $font |
||
| 348 | * @return int |
||
| 349 | */ |
||
| 350 | protected function renderText($image, $phrase, $font) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * 画线 |
||
| 387 | * |
||
| 388 | * @param resource $image |
||
| 389 | * @param int $width |
||
| 390 | * @param int $height |
||
| 391 | * @param int|null $color |
||
| 392 | */ |
||
| 393 | protected function renderLine($image, $width, $height, $color = null) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * 画线 |
||
| 416 | * |
||
| 417 | * @param resource $image |
||
| 418 | * @param int $max |
||
| 419 | * @param int|null $color |
||
| 420 | */ |
||
| 421 | protected function drawLines($image, $max, $color = null) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * 获取颜色 |
||
| 446 | * |
||
| 447 | * @param resource $image |
||
| 448 | * @param int $width |
||
| 449 | * @param int $height |
||
| 450 | * @param int $background |
||
| 451 | * @return int |
||
| 452 | */ |
||
| 453 | protected function getColor($image, $width, $height, $background) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param string $name |
||
| 466 | * @param array $arguments |
||
| 467 | * @return $this |
||
| 468 | */ |
||
| 469 | public function __call($name, $arguments) |
||
| 477 | } |