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 |
||
| 9 | class HtmlDiffConfig |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | protected $specialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $specialCaseChars = array('.', ',', '(', ')', '\''); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var bool |
||
| 23 | */ |
||
| 24 | protected $groupDiffs = true; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | protected $insertSpaceInReplace = false; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $encoding = 'UTF-8'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $isolatedDiffTags = array( |
||
| 40 | 'ol' => '[[REPLACE_ORDERED_LIST]]', |
||
| 41 | 'ul' => '[[REPLACE_UNORDERED_LIST]]', |
||
| 42 | 'sub' => '[[REPLACE_SUB_SCRIPT]]', |
||
| 43 | 'sup' => '[[REPLACE_SUPER_SCRIPT]]', |
||
| 44 | 'dl' => '[[REPLACE_DEFINITION_LIST]]', |
||
| 45 | 'table' => '[[REPLACE_TABLE]]', |
||
| 46 | 'strong' => '[[REPLACE_STRONG]]', |
||
| 47 | 'b' => '[[REPLACE_B]]', |
||
| 48 | 'em' => '[[REPLACE_EM]]', |
||
| 49 | 'i' => '[[REPLACE_I]]', |
||
| 50 | 'a' => '[[REPLACE_A]]', |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | protected $matchThreshold = 80; |
||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $specialCaseOpeningTags = array(); |
||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $specialCaseClosingTags = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var bool |
||
| 68 | */ |
||
| 69 | protected $useTableDiffing = true; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var null|\Doctrine\Common\Cache\Cache |
||
| 73 | */ |
||
| 74 | protected $cacheProvider; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var null|string |
||
| 78 | */ |
||
| 79 | protected $purifierCacheLocation = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return HtmlDiffConfig |
||
| 83 | */ |
||
| 84 | 11 | public static function create() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * HtmlDiffConfig constructor. |
||
| 91 | */ |
||
| 92 | 11 | public function __construct() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @return int |
||
| 99 | */ |
||
| 100 | 4 | public function getMatchThreshold() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @param int $matchThreshold |
||
| 107 | * |
||
| 108 | * @return AbstractDiff |
||
|
|
|||
| 109 | */ |
||
| 110 | public function setMatchThreshold($matchThreshold) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param array $chars |
||
| 119 | */ |
||
| 120 | public function setSpecialCaseChars(array $chars) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return array|null |
||
| 127 | */ |
||
| 128 | 11 | public function getSpecialCaseChars() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $char |
||
| 135 | * |
||
| 136 | * @return $this |
||
| 137 | */ |
||
| 138 | public function addSpecialCaseChar($char) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $char |
||
| 149 | * |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | public function removeSpecialCaseChar($char) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param array $tags |
||
| 164 | * |
||
| 165 | * @return $this |
||
| 166 | */ |
||
| 167 | 11 | public function setSpecialCaseTags(array $tags = array()) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $tag |
||
| 182 | * |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | 11 | public function addSpecialCaseTag($tag) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $tag |
||
| 206 | * |
||
| 207 | * @return $this |
||
| 208 | */ |
||
| 209 | public function removeSpecialCaseTag($tag) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return array|null |
||
| 230 | */ |
||
| 231 | public function getSpecialCaseTags() |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * @return boolean |
||
| 239 | */ |
||
| 240 | 11 | public function isGroupDiffs() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param boolean $groupDiffs |
||
| 247 | * |
||
| 248 | * @return HtmlDiffConfig |
||
| 249 | */ |
||
| 250 | public function setGroupDiffs($groupDiffs) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | public function getEncoding() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $encoding |
||
| 267 | * |
||
| 268 | * @return HtmlDiffConfig |
||
| 269 | */ |
||
| 270 | 11 | public function setEncoding($encoding) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @return boolean |
||
| 279 | */ |
||
| 280 | public function isInsertSpaceInReplace() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param boolean $insertSpaceInReplace |
||
| 287 | * |
||
| 288 | * @return HtmlDiffConfig |
||
| 289 | */ |
||
| 290 | public function setInsertSpaceInReplace($insertSpaceInReplace) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | 11 | public function getIsolatedDiffTags() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param array $isolatedDiffTags |
||
| 307 | * |
||
| 308 | * @return HtmlDiffConfig |
||
| 309 | */ |
||
| 310 | public function setIsolatedDiffTags($isolatedDiffTags) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param string $tag |
||
| 319 | * @param null|string $placeholder |
||
| 320 | * |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | public function addIsolatedDiffTag($tag, $placeholder = null) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param string $tag |
||
| 351 | * |
||
| 352 | * @return $this |
||
| 353 | */ |
||
| 354 | public function removeIsolatedDiffTag($tag) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param string $tag |
||
| 365 | * |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | 11 | public function isIsolatedDiffTag($tag) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * @param string $text |
||
| 375 | * |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | 11 | public function isIsolatedDiffTagPlaceholder($text) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * @param string $tag |
||
| 385 | * |
||
| 386 | * @return null|string |
||
| 387 | */ |
||
| 388 | 11 | public function getIsolatedDiffTagPlaceholder($tag) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * @return array |
||
| 395 | */ |
||
| 396 | 6 | public function getSpecialCaseOpeningTags() |
|
| 400 | |||
| 401 | /** |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | 6 | public function getSpecialCaseClosingTags() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * @return boolean |
||
| 411 | */ |
||
| 412 | 5 | public function isUseTableDiffing() |
|
| 416 | |||
| 417 | /** |
||
| 418 | * @param boolean $useTableDiffing |
||
| 419 | * |
||
| 420 | * @return HtmlDiffConfig |
||
| 421 | */ |
||
| 422 | public function setUseTableDiffing($useTableDiffing) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param null|\Doctrine\Common\Cache\Cache $cacheProvider |
||
| 431 | * |
||
| 432 | * @return $this |
||
| 433 | */ |
||
| 434 | public function setCacheProvider(\Doctrine\Common\Cache\Cache $cacheProvider = null) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @return null|\Doctrine\Common\Cache\Cache |
||
| 443 | */ |
||
| 444 | 11 | public function getCacheProvider() |
|
| 448 | |||
| 449 | /** |
||
| 450 | * @param null|string |
||
| 451 | * |
||
| 452 | * @return $this |
||
| 453 | */ |
||
| 454 | public function setPurifierCacheLocation($purifierCacheLocation = null) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @return null|string |
||
| 463 | */ |
||
| 464 | public function getPurifierCacheLocation() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param string $tag |
||
| 471 | * |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | 11 | protected function getOpeningTag($tag) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * @param string $tag |
||
| 481 | * |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | 11 | protected function getClosingTag($tag) |
|
| 488 | } |
||
| 489 |
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.