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 | * @return HtmlDiffConfig |
||
| 78 | */ |
||
| 79 | 11 | public static function create() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * HtmlDiffConfig constructor. |
||
| 86 | */ |
||
| 87 | 11 | public function __construct() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @return int |
||
| 94 | */ |
||
| 95 | 4 | public function getMatchThreshold() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param int $matchThreshold |
||
| 102 | * |
||
| 103 | * @return AbstractDiff |
||
|
|
|||
| 104 | */ |
||
| 105 | public function setMatchThreshold($matchThreshold) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param array $chars |
||
| 114 | */ |
||
| 115 | public function setSpecialCaseChars(array $chars) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return array|null |
||
| 122 | */ |
||
| 123 | 11 | public function getSpecialCaseChars() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $char |
||
| 130 | * |
||
| 131 | * @return $this |
||
| 132 | */ |
||
| 133 | public function addSpecialCaseChar($char) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $char |
||
| 144 | * |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | public function removeSpecialCaseChar($char) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param array $tags |
||
| 159 | * |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | 11 | public function setSpecialCaseTags(array $tags = array()) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $tag |
||
| 177 | * |
||
| 178 | * @return $this |
||
| 179 | */ |
||
| 180 | 11 | public function addSpecialCaseTag($tag) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param string $tag |
||
| 201 | * |
||
| 202 | * @return $this |
||
| 203 | */ |
||
| 204 | public function removeSpecialCaseTag($tag) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return array|null |
||
| 225 | */ |
||
| 226 | public function getSpecialCaseTags() |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * @return boolean |
||
| 234 | */ |
||
| 235 | 11 | public function isGroupDiffs() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param boolean $groupDiffs |
||
| 242 | * |
||
| 243 | * @return HtmlDiffConfig |
||
| 244 | */ |
||
| 245 | public function setGroupDiffs($groupDiffs) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | public function getEncoding() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $encoding |
||
| 262 | * |
||
| 263 | * @return HtmlDiffConfig |
||
| 264 | */ |
||
| 265 | 11 | public function setEncoding($encoding) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @return boolean |
||
| 274 | */ |
||
| 275 | public function isInsertSpaceInReplace() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param boolean $insertSpaceInReplace |
||
| 282 | * |
||
| 283 | * @return HtmlDiffConfig |
||
| 284 | */ |
||
| 285 | public function setInsertSpaceInReplace($insertSpaceInReplace) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | 11 | public function getIsolatedDiffTags() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @param array $isolatedDiffTags |
||
| 302 | * |
||
| 303 | * @return HtmlDiffConfig |
||
| 304 | */ |
||
| 305 | public function setIsolatedDiffTags($isolatedDiffTags) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $tag |
||
| 314 | * @param null|string $placeholder |
||
| 315 | * |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function addIsolatedDiffTag($tag, $placeholder = null) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param string $tag |
||
| 346 | * |
||
| 347 | * @return $this |
||
| 348 | */ |
||
| 349 | public function removeIsolatedDiffTag($tag) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param string $tag |
||
| 360 | * |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | 11 | public function isIsolatedDiffTag($tag) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * @param string $text |
||
| 370 | * |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | 11 | public function isIsolatedDiffTagPlaceholder($text) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * @param string $tag |
||
| 380 | * |
||
| 381 | * @return null|string |
||
| 382 | */ |
||
| 383 | 11 | public function getIsolatedDiffTagPlaceholder($tag) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @return array |
||
| 390 | */ |
||
| 391 | 6 | public function getSpecialCaseOpeningTags() |
|
| 395 | |||
| 396 | /** |
||
| 397 | * @return array |
||
| 398 | */ |
||
| 399 | 6 | public function getSpecialCaseClosingTags() |
|
| 403 | |||
| 404 | /** |
||
| 405 | * @return boolean |
||
| 406 | */ |
||
| 407 | 5 | public function isUseTableDiffing() |
|
| 411 | |||
| 412 | /** |
||
| 413 | * @param boolean $useTableDiffing |
||
| 414 | * |
||
| 415 | * @return HtmlDiffConfig |
||
| 416 | */ |
||
| 417 | public function setUseTableDiffing($useTableDiffing) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param null|\Doctrine\Common\Cache\Cache $cacheProvider |
||
| 426 | * |
||
| 427 | * @return $this |
||
| 428 | */ |
||
| 429 | public function setCacheProvider(\Doctrine\Common\Cache\Cache $cacheProvider = null) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @return null|\Doctrine\Common\Cache\Cache |
||
| 438 | */ |
||
| 439 | 11 | public function getCacheProvider() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * @param string $tag |
||
| 446 | * |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | 11 | protected function getOpeningTag($tag) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $tag |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | 11 | protected function getClosingTag($tag) |
|
| 463 | } |
||
| 464 |
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.