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 | * 存储在session中的key |
||
| 32 | */ |
||
| 33 | const NAME = 'captcha'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * 验证码配置 |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $config = [ |
||
| 40 | /** |
||
| 41 | * 默认验证码长度 |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | 'length' => 4, |
||
| 45 | /** |
||
| 46 | * 验证码字符集 |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | 'charset' => 'abcdefghijklmnpqrstuvwxyz123456789', |
||
| 50 | /** |
||
| 51 | * 默认验证码宽度 |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | 'width' => 150, |
||
| 55 | /** |
||
| 56 | * 默认验证码高度 |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | 'height' => 40, |
||
| 60 | /** |
||
| 61 | * 指定文字颜色 |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | 'textColor' => null, |
||
| 65 | /** |
||
| 66 | * 文字字体文件 |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | 'textFont' => null, |
||
| 70 | /** |
||
| 71 | * 指定图片背景色 |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | 'backgroundColor' => null, |
||
| 75 | /** |
||
| 76 | * 开启失真模式 |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | 'distortion' => true, |
||
| 80 | /** |
||
| 81 | * 最大前景线条数 |
||
| 82 | * @var int |
||
| 83 | */ |
||
| 84 | 'maxFrontLines' => null, |
||
| 85 | /** |
||
| 86 | * 最大背景线条数 |
||
| 87 | * @val int |
||
| 88 | */ |
||
| 89 | 'maxBehindLines' => null, |
||
| 90 | /** |
||
| 91 | * 文字最大角度 |
||
| 92 | * @var int |
||
| 93 | */ |
||
| 94 | 'maxAngle' => 8, |
||
| 95 | /** |
||
| 96 | * 文字最大偏移量 |
||
| 97 | * @var int |
||
| 98 | */ |
||
| 99 | 'maxOffset' => 5 |
||
| 100 | ]; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * 存储驱动 |
||
| 104 | * @var Session |
||
| 105 | */ |
||
| 106 | protected $store; |
||
| 107 | |||
| 108 | |||
| 109 | public function __construct(array $config = []) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * 设置验证码配置 |
||
| 119 | * |
||
| 120 | * @param array|string $config 配置数组或配置项key |
||
| 121 | * @param mixed $value 配置项值 |
||
| 122 | * @return $this |
||
| 123 | */ |
||
| 124 | public function setConfig($config, $value = null) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * 获取配置 |
||
| 141 | * |
||
| 142 | * @param string|null $key 配置项key |
||
| 143 | * @return string|number|array |
||
| 144 | */ |
||
| 145 | public function getConfig($key = null) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * 生成验证码 |
||
| 156 | * |
||
| 157 | * @return Image |
||
| 158 | */ |
||
| 159 | public function make() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * 仅测试正确性, 不删除验证码 |
||
| 177 | * |
||
| 178 | * @param string $input |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | public function test($input) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * 检测正确性,并删除验证码 |
||
| 194 | * |
||
| 195 | * @param string $input |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | public function check($input) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * 生成验证码 |
||
| 209 | * |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | protected function generate() |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * 创建验证码图片 |
||
| 228 | * |
||
| 229 | * @param string $code |
||
| 230 | * @return resource |
||
| 231 | */ |
||
| 232 | protected function build($code) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * 获取一个字体 |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | protected function getTextFont() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * 写入验证码到图片中 |
||
| 345 | * |
||
| 346 | * @param resource $image |
||
| 347 | * @param string $phrase |
||
| 348 | * @param string $font |
||
| 349 | * @return int |
||
| 350 | */ |
||
| 351 | protected function renderText($image, $phrase, $font) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * 画线 |
||
| 388 | * |
||
| 389 | * @param resource $image |
||
| 390 | * @param int $width |
||
| 391 | * @param int $height |
||
| 392 | * @param int|null $color |
||
| 393 | */ |
||
| 394 | protected function renderLine($image, $width, $height, $color = null) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * 画线 |
||
| 417 | * |
||
| 418 | * @param resource $image |
||
| 419 | * @param int $max |
||
| 420 | * @param int|null $color |
||
| 421 | */ |
||
| 422 | protected function drawLines($image, $max, $color = null) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * 获取颜色 |
||
| 447 | * |
||
| 448 | * @param resource $image |
||
| 449 | * @param int $x |
||
| 450 | * @param int $y |
||
| 451 | * @param int $background |
||
| 452 | * @return int |
||
| 453 | */ |
||
| 454 | protected function getColor($image, $x, $y, $background) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @param string $name |
||
| 467 | * @param array $arguments |
||
| 468 | * @return $this |
||
| 469 | */ |
||
| 470 | public function __call($name, $arguments) |
||
| 478 | } |