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) |
||
293 | |||
294 | /** |
||
295 | * 创建失真 |
||
296 | * |
||
297 | * @param resource $image |
||
298 | * @param int $width |
||
299 | * @param int $height |
||
300 | * @param int $backgroundColor |
||
301 | * @return resource |
||
302 | */ |
||
303 | protected function createDistortion($image, $width, $height, $backgroundColor) |
||
341 | |||
342 | /** |
||
343 | * 获取一个字体 |
||
344 | * |
||
345 | * @return string |
||
346 | */ |
||
347 | protected function getTextFont() |
||
356 | |||
357 | /** |
||
358 | * 写入验证码到图片中 |
||
359 | * |
||
360 | * @param resource $image |
||
361 | * @param string $phrase |
||
362 | * @param string $font |
||
363 | * @return int |
||
364 | */ |
||
365 | protected function renderText($image, $phrase, $font) |
||
399 | |||
400 | /** |
||
401 | * 画线 |
||
402 | * |
||
403 | * @param resource $image |
||
404 | * @param int $width |
||
405 | * @param int $height |
||
406 | * @param int|null $color |
||
407 | */ |
||
408 | protected function renderLine($image, $width, $height, $color = null) |
||
428 | |||
429 | /** |
||
430 | * 画线 |
||
431 | * |
||
432 | * @param resource $image |
||
433 | * @param int $max |
||
434 | * @param int|null $color |
||
435 | */ |
||
436 | protected function drawLines($image, $max, $color = null) |
||
458 | |||
459 | /** |
||
460 | * 获取颜色 |
||
461 | * |
||
462 | * @param resource $image |
||
463 | * @param int $x |
||
464 | * @param int $y |
||
465 | * @param int $background |
||
466 | * @return int |
||
467 | */ |
||
468 | protected function getColor($image, $x, $y, $background) |
||
478 | |||
479 | /** |
||
480 | * @param string $name |
||
481 | * @param array $arguments |
||
482 | * @return $this |
||
483 | */ |
||
484 | public function __call($name, $arguments) |
||
492 | } |