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 |
||
| 35 | class Captcha |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var Filesystem |
||
| 39 | */ |
||
| 40 | protected $files; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Repository |
||
| 44 | */ |
||
| 45 | protected $config; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var ImageManager |
||
| 49 | */ |
||
| 50 | protected $imageManager; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var Session |
||
| 54 | */ |
||
| 55 | protected $session; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var Hasher |
||
| 59 | */ |
||
| 60 | protected $hasher; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var Str |
||
| 64 | */ |
||
| 65 | protected $str; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var ImageManager->canvas |
||
| 69 | */ |
||
| 70 | protected $canvas; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var Image |
||
| 74 | */ |
||
| 75 | protected $image; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $backgrounds = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $fonts = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | protected $fontColors = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var int |
||
| 94 | */ |
||
| 95 | protected $length = 5; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var int |
||
| 99 | */ |
||
| 100 | protected $width = 120; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var int |
||
| 104 | */ |
||
| 105 | protected $height = 36; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var int |
||
| 109 | */ |
||
| 110 | protected $angle = 15; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var int |
||
| 114 | */ |
||
| 115 | protected $lines = 3; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var string |
||
| 119 | */ |
||
| 120 | protected $characters; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | protected $text; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var int |
||
| 129 | */ |
||
| 130 | protected $contrast = 0; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var int |
||
| 134 | */ |
||
| 135 | protected $quality = 90; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var int |
||
| 139 | */ |
||
| 140 | protected $sharpen = 0; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var int |
||
| 144 | */ |
||
| 145 | protected $blur = 0; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var bool |
||
| 149 | */ |
||
| 150 | protected $bgImage = true; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @var string |
||
| 154 | */ |
||
| 155 | protected $bgColor = '#ffffff'; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var bool |
||
| 159 | */ |
||
| 160 | protected $invert = false; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @var bool |
||
| 164 | */ |
||
| 165 | protected $sensitive = false; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @var bool |
||
| 169 | */ |
||
| 170 | protected $math = false; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @var int |
||
| 174 | */ |
||
| 175 | protected $textLeftPadding = 4; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @var string |
||
| 179 | */ |
||
| 180 | protected $fontsDirectory; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @var int |
||
| 184 | */ |
||
| 185 | protected $expire = 60; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @var bool |
||
| 189 | */ |
||
| 190 | protected $encrypt = true; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Constructor |
||
| 194 | * |
||
| 195 | * @param Filesystem $files |
||
| 196 | * @param Repository $config |
||
| 197 | * @param ImageManager $imageManager |
||
| 198 | * @param Session $session |
||
| 199 | * @param Hasher $hasher |
||
| 200 | * @param Str $str |
||
| 201 | * @throws Exception |
||
| 202 | * @internal param Validator $validator |
||
| 203 | */ |
||
| 204 | public function __construct( |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param string $config |
||
| 224 | * @return void |
||
| 225 | */ |
||
| 226 | protected function configure($config) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Create captcha image |
||
| 237 | * |
||
| 238 | * @param string $config |
||
| 239 | * @param bool $api |
||
| 240 | * @return array|mixed |
||
| 241 | * @throws Exception |
||
| 242 | */ |
||
| 243 | public function create(string $config = 'default', bool $api = false) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Image backgrounds |
||
| 309 | * |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | protected function background(): string |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Generate captcha text |
||
| 319 | * |
||
| 320 | * @return array |
||
| 321 | * @throws Exception |
||
| 322 | */ |
||
| 323 | protected function generate(): array |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Writing captcha text |
||
| 361 | * |
||
| 362 | * @return void |
||
| 363 | */ |
||
| 364 | protected function text(): void |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Image fonts |
||
| 390 | * |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | protected function font(): string |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Random font size |
||
| 400 | * |
||
| 401 | * @return int |
||
| 402 | */ |
||
| 403 | protected function fontSize(): int |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Random font color |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | protected function fontColor(): string |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Angle |
||
| 426 | * |
||
| 427 | * @return int |
||
| 428 | */ |
||
| 429 | protected function angle(): int |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Random image lines |
||
| 436 | * |
||
| 437 | * @return Image|ImageManager |
||
| 438 | */ |
||
| 439 | protected function lines() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Captcha check |
||
| 459 | * |
||
| 460 | * @param string $value |
||
| 461 | * @return bool |
||
| 462 | */ |
||
| 463 | public function check(string $value): bool |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Returns the md5 short version of the key for cache |
||
| 489 | * |
||
| 490 | * @param string $key |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | protected function get_cache_key($key) { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Captcha check |
||
| 499 | * |
||
| 500 | * @param string $value |
||
| 501 | * @param string $key |
||
| 502 | * @param string $config |
||
| 503 | * @return bool |
||
| 504 | */ |
||
| 505 | public function check_api($value, $key, $config = 'default'): bool |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Generate captcha image source |
||
| 520 | * |
||
| 521 | * @param string $config |
||
| 522 | * @return string |
||
| 523 | */ |
||
| 524 | public function src(string $config = 'default'): string |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Generate captcha image html tag |
||
| 531 | * |
||
| 532 | * @param string $config |
||
| 533 | * @param array $attrs |
||
| 534 | * $attrs -> HTML attributes supplied to the image tag where key is the attribute and the value is the attribute value |
||
| 535 | * @return string |
||
| 536 | */ |
||
| 537 | public function img(string $config = 'default', array $attrs = []): string |
||
| 550 | } |
||
| 551 |