Complex classes like AbstractDiff 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 AbstractDiff, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | abstract class AbstractDiff |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var array |
||
| 12 | * |
||
| 13 | * @deprecated since 0.1.0 |
||
| 14 | */ |
||
| 15 | public static $defaultSpecialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
||
|
|
|||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | * |
||
| 19 | * @deprecated since 0.1.0 |
||
| 20 | */ |
||
| 21 | public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\''); |
||
| 22 | /** |
||
| 23 | * @var bool |
||
| 24 | * |
||
| 25 | * @deprecated since 0.1.0 |
||
| 26 | */ |
||
| 27 | public static $defaultGroupDiffs = true; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var HtmlDiffConfig |
||
| 31 | */ |
||
| 32 | protected $config; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $content; |
||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $oldText; |
||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $newText; |
||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $oldWords = array(); |
||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $newWords = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var DiffCache[] |
||
| 57 | */ |
||
| 58 | protected $diffCaches = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var \HTMLPurifier |
||
| 62 | */ |
||
| 63 | protected $purifier; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * AbstractDiff constructor. |
||
| 67 | * |
||
| 68 | * @param string $oldText |
||
| 69 | * @param string $newText |
||
| 70 | * @param string $encoding |
||
| 71 | * @param null|array $specialCaseTags |
||
| 72 | * @param null|bool $groupDiffs |
||
| 73 | */ |
||
| 74 | 12 | public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @return bool|string |
||
| 96 | */ |
||
| 97 | abstract public function build(); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Initializes HTMLPurifier with cache location. |
||
| 101 | * |
||
| 102 | * @param null|string $defaultPurifierSerializerCache |
||
| 103 | */ |
||
| 104 | 12 | public function initPurifier($defaultPurifierSerializerCache = null) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @return DiffCache|null |
||
| 118 | */ |
||
| 119 | protected function getDiffCache() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | 12 | protected function hasDiffCache() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @return HtmlDiffConfig |
||
| 144 | */ |
||
| 145 | 12 | public function getConfig() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param HtmlDiffConfig $config |
||
| 152 | * |
||
| 153 | * @return AbstractDiff |
||
| 154 | */ |
||
| 155 | 12 | public function setConfig(HtmlDiffConfig $config) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @return int |
||
| 165 | * |
||
| 166 | * @deprecated since 0.1.0 |
||
| 167 | */ |
||
| 168 | public function getMatchThreshold() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param int $matchThreshold |
||
| 175 | * |
||
| 176 | * @return AbstractDiff |
||
| 177 | * |
||
| 178 | * @deprecated since 0.1.0 |
||
| 179 | */ |
||
| 180 | public function setMatchThreshold($matchThreshold) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param array $chars |
||
| 189 | * |
||
| 190 | * @deprecated since 0.1.0 |
||
| 191 | */ |
||
| 192 | public function setSpecialCaseChars(array $chars) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return array|null |
||
| 199 | * |
||
| 200 | * @deprecated since 0.1.0 |
||
| 201 | */ |
||
| 202 | public function getSpecialCaseChars() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $char |
||
| 209 | * |
||
| 210 | * @deprecated since 0.1.0 |
||
| 211 | */ |
||
| 212 | public function addSpecialCaseChar($char) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $char |
||
| 219 | * |
||
| 220 | * @deprecated since 0.1.0 |
||
| 221 | */ |
||
| 222 | public function removeSpecialCaseChar($char) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param array $tags |
||
| 229 | * |
||
| 230 | * @deprecated since 0.1.0 |
||
| 231 | */ |
||
| 232 | public function setSpecialCaseTags(array $tags = array()) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $tag |
||
| 239 | * |
||
| 240 | * @deprecated since 0.1.0 |
||
| 241 | */ |
||
| 242 | public function addSpecialCaseTag($tag) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $tag |
||
| 249 | * |
||
| 250 | * @deprecated since 0.1.0 |
||
| 251 | */ |
||
| 252 | public function removeSpecialCaseTag($tag) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return array|null |
||
| 259 | * |
||
| 260 | * @deprecated since 0.1.0 |
||
| 261 | */ |
||
| 262 | public function getSpecialCaseTags() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public function getOldHtml() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public function getNewHtml() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public function getDifference() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Clears the diff content. |
||
| 293 | * |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | public function clearContent() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param bool $boolean |
||
| 303 | * |
||
| 304 | * @return $this |
||
| 305 | * |
||
| 306 | * @deprecated since 0.1.0 |
||
| 307 | */ |
||
| 308 | public function setGroupDiffs($boolean) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return bool |
||
| 317 | * |
||
| 318 | * @deprecated since 0.1.0 |
||
| 319 | */ |
||
| 320 | public function isGroupDiffs() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param string $tag |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | protected function getOpeningTag($tag) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param string $tag |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | protected function getClosingTag($tag) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $str |
||
| 347 | * @param string $start |
||
| 348 | * @param string $end |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | protected function getStringBetween($str, $start, $end) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param string $html |
||
| 369 | * |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | 12 | protected function purifyHtml($html) |
|
| 385 | |||
| 386 | 12 | protected function splitInputsToWords() |
|
| 391 | |||
| 392 | /** |
||
| 393 | * @param string $text |
||
| 394 | * |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | 12 | protected function isPartOfWord($text) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * @param array $characterString |
||
| 404 | * |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | 12 | protected function convertHtmlToListOfWords($characterString) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * @param string $val |
||
| 486 | * |
||
| 487 | * @return bool |
||
| 488 | */ |
||
| 489 | 12 | protected function isStartOfTag($val) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * @param string $val |
||
| 496 | * |
||
| 497 | * @return bool |
||
| 498 | */ |
||
| 499 | 12 | protected function isEndOfTag($val) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * @param string $value |
||
| 506 | * |
||
| 507 | * @return bool |
||
| 508 | */ |
||
| 509 | protected function isWhiteSpace($value) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param string $value |
||
| 516 | * |
||
| 517 | * @return array |
||
| 518 | */ |
||
| 519 | 12 | protected function explode($value) |
|
| 524 | } |
||
| 525 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.