Complex classes like HtmlDiffConfig 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 HtmlDiffConfig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class HtmlDiffConfig |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var array |
||
| 12 | */ |
||
| 13 | protected $specialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string[] |
||
| 17 | */ |
||
| 18 | protected $specialCaseChars = array('.', ',', '(', ')', '\''); |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | protected $groupDiffs = true; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | protected $insertSpaceInReplace = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Whether to keep newlines in the diff |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | protected $keepNewLines = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $encoding = 'UTF-8'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $isolatedDiffTags = array( |
||
| 45 | 'ol' => '[[REPLACE_ORDERED_LIST]]', |
||
| 46 | 'ul' => '[[REPLACE_UNORDERED_LIST]]', |
||
| 47 | 'sub' => '[[REPLACE_SUB_SCRIPT]]', |
||
| 48 | 'sup' => '[[REPLACE_SUPER_SCRIPT]]', |
||
| 49 | 'dl' => '[[REPLACE_DEFINITION_LIST]]', |
||
| 50 | 'table' => '[[REPLACE_TABLE]]', |
||
| 51 | 'strong' => '[[REPLACE_STRONG]]', |
||
| 52 | 'b' => '[[REPLACE_STRONG]]', |
||
| 53 | 'em' => '[[REPLACE_EM]]', |
||
| 54 | 'i' => '[[REPLACE_EM]]', |
||
| 55 | 'a' => '[[REPLACE_A]]', |
||
| 56 | 'img' => '[[REPLACE_IMG]]', |
||
| 57 | 'pre' => '[[REPLACE_PRE]]', |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | protected $matchThreshold = 80; |
||
| 64 | /** |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $specialCaseOpeningTags = array(); |
||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $specialCaseClosingTags = array(); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | protected $useTableDiffing = true; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var null|\Doctrine\Common\Cache\Cache |
||
| 80 | */ |
||
| 81 | protected $cacheProvider; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var bool |
||
| 85 | */ |
||
| 86 | protected $purifierEnabled = true; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var null|string |
||
| 90 | */ |
||
| 91 | protected $purifierCacheLocation = null; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return HtmlDiffConfig |
||
| 95 | */ |
||
| 96 | 17 | public static function create() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * HtmlDiffConfig constructor. |
||
| 103 | */ |
||
| 104 | 17 | public function __construct() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @return int |
||
| 111 | */ |
||
| 112 | 9 | public function getMatchThreshold() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param int $matchThreshold |
||
| 119 | * |
||
| 120 | * @return AbstractDiff |
||
|
|
|||
| 121 | */ |
||
| 122 | public function setMatchThreshold($matchThreshold) |
||
| 128 | |||
| 129 | public function setSpecialCaseChars(array $chars) |
||
| 133 | |||
| 134 | 17 | public function getSpecialCaseChars() : array |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $char |
||
| 141 | * |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | public function addSpecialCaseChar($char) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $char |
||
| 155 | * |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | public function removeSpecialCaseChar($char) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param array $tags |
||
| 170 | * |
||
| 171 | * @return $this |
||
| 172 | */ |
||
| 173 | 17 | public function setSpecialCaseTags(array $tags = array()) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @param string $tag |
||
| 188 | * |
||
| 189 | * @return $this |
||
| 190 | */ |
||
| 191 | 17 | public function addSpecialCaseTag($tag) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @param string $tag |
||
| 212 | * |
||
| 213 | * @return $this |
||
| 214 | */ |
||
| 215 | public function removeSpecialCaseTag($tag) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return array|null |
||
| 236 | */ |
||
| 237 | public function getSpecialCaseTags() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | 17 | public function isGroupDiffs() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * @param bool $groupDiffs |
||
| 252 | * |
||
| 253 | * @return HtmlDiffConfig |
||
| 254 | */ |
||
| 255 | public function setGroupDiffs($groupDiffs) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | 1 | public function getEncoding() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $encoding |
||
| 272 | * |
||
| 273 | * @return HtmlDiffConfig |
||
| 274 | */ |
||
| 275 | 17 | public function setEncoding($encoding) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | public function isInsertSpaceInReplace() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param bool $insertSpaceInReplace |
||
| 292 | * |
||
| 293 | * @return HtmlDiffConfig |
||
| 294 | */ |
||
| 295 | public function setInsertSpaceInReplace($insertSpaceInReplace) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | public function isKeepNewLines() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param bool $keepNewLines |
||
| 312 | */ |
||
| 313 | public function setKeepNewLines($keepNewLines) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | 17 | public function getIsolatedDiffTags() |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param array $isolatedDiffTags |
||
| 328 | * |
||
| 329 | * @return HtmlDiffConfig |
||
| 330 | */ |
||
| 331 | public function setIsolatedDiffTags($isolatedDiffTags) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param string $tag |
||
| 340 | * @param null|string $placeholder |
||
| 341 | * |
||
| 342 | * @return $this |
||
| 343 | */ |
||
| 344 | public function addIsolatedDiffTag($tag, $placeholder = null) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param string $tag |
||
| 372 | * |
||
| 373 | * @return $this |
||
| 374 | */ |
||
| 375 | public function removeIsolatedDiffTag($tag) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param string $tag |
||
| 386 | * |
||
| 387 | * @return bool |
||
| 388 | */ |
||
| 389 | 15 | public function isIsolatedDiffTag($tag) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * @param string $text |
||
| 396 | * |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | 17 | public function isIsolatedDiffTagPlaceholder($text) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * @param string $tag |
||
| 406 | * |
||
| 407 | * @return null|string |
||
| 408 | */ |
||
| 409 | 15 | public function getIsolatedDiffTagPlaceholder($tag) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * @return array |
||
| 416 | */ |
||
| 417 | 7 | public function getSpecialCaseOpeningTags() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * @return array |
||
| 424 | */ |
||
| 425 | 7 | public function getSpecialCaseClosingTags() |
|
| 429 | |||
| 430 | /** |
||
| 431 | * @return bool |
||
| 432 | */ |
||
| 433 | 10 | public function isUseTableDiffing() |
|
| 437 | |||
| 438 | /** |
||
| 439 | * @param bool $useTableDiffing |
||
| 440 | * |
||
| 441 | * @return HtmlDiffConfig |
||
| 442 | */ |
||
| 443 | public function setUseTableDiffing($useTableDiffing) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param null|\Doctrine\Common\Cache\Cache $cacheProvider |
||
| 452 | * |
||
| 453 | * @return $this |
||
| 454 | */ |
||
| 455 | public function setCacheProvider(\Doctrine\Common\Cache\Cache $cacheProvider = null) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @return null|\Doctrine\Common\Cache\Cache |
||
| 464 | */ |
||
| 465 | 17 | public function getCacheProvider() |
|
| 469 | |||
| 470 | 17 | public function isPurifierEnabled(): bool |
|
| 474 | |||
| 475 | public function setPurifierEnabled(bool $purifierEnabled = true): self |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param null|string |
||
| 484 | * |
||
| 485 | * @return $this |
||
| 486 | */ |
||
| 487 | 2 | public function setPurifierCacheLocation($purifierCacheLocation = null) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * @return null|string |
||
| 496 | */ |
||
| 497 | 17 | public function getPurifierCacheLocation() |
|
| 501 | |||
| 502 | /** |
||
| 503 | * @param string $tag |
||
| 504 | * |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | 17 | protected function getOpeningTag($tag) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * @param string $tag |
||
| 514 | * |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | 17 | protected function getClosingTag($tag) |
|
| 521 | } |
||
| 522 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.