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 |
||
| 9 | abstract class AbstractDiff |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | public static $defaultSpecialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
||
|
|
|||
| 15 | /** |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\''); |
||
| 19 | /** |
||
| 20 | * @var bool |
||
| 21 | */ |
||
| 22 | public static $defaultGroupDiffs = true; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $content; |
||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $oldText; |
||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $newText; |
||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $oldWords = array(); |
||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $newWords = array(); |
||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $encoding; |
||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $specialCaseOpeningTags = array(); |
||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $specialCaseClosingTags = array(); |
||
| 56 | /** |
||
| 57 | * @var array|null |
||
| 58 | */ |
||
| 59 | protected $specialCaseTags; |
||
| 60 | /** |
||
| 61 | * @var array|null |
||
| 62 | */ |
||
| 63 | protected $specialCaseChars; |
||
| 64 | /** |
||
| 65 | * @var bool|null |
||
| 66 | */ |
||
| 67 | protected $groupDiffs; |
||
| 68 | /** |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | protected $matchThreshold = 80; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * AbstractDiff constructor. |
||
| 75 | * |
||
| 76 | * @param string $oldText |
||
| 77 | * @param string $newText |
||
| 78 | * @param string $encoding |
||
| 79 | * @param null|array $specialCaseTags |
||
| 80 | * @param null|bool $groupDiffs |
||
| 81 | */ |
||
| 82 | 11 | public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @return int |
||
| 105 | */ |
||
| 106 | public function getMatchThreshold() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param int $matchThreshold |
||
| 113 | * |
||
| 114 | * @return AbstractDiff |
||
| 115 | */ |
||
| 116 | 4 | public function setMatchThreshold($matchThreshold) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @param array $chars |
||
| 125 | */ |
||
| 126 | 11 | public function setSpecialCaseChars(array $chars) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @return array|null |
||
| 133 | */ |
||
| 134 | public function getSpecialCaseChars() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $char |
||
| 141 | */ |
||
| 142 | public function addSpecialCaseChar($char) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $char |
||
| 151 | */ |
||
| 152 | public function removeSpecialCaseChar($char) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param array $tags |
||
| 162 | */ |
||
| 163 | 11 | public function setSpecialCaseTags(array $tags = array()) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @param string $tag |
||
| 174 | */ |
||
| 175 | public function addSpecialCaseTag($tag) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $tag |
||
| 194 | */ |
||
| 195 | public function removeSpecialCaseTag($tag) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return array|null |
||
| 214 | */ |
||
| 215 | public function getSpecialCaseTags() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function getOldHtml() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | public function getNewHtml() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function getDifference() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param bool $boolean |
||
| 246 | * |
||
| 247 | * @return $this |
||
| 248 | */ |
||
| 249 | public function setGroupDiffs($boolean) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | 11 | public function isGroupDiffs() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $tag |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | protected function getOpeningTag($tag) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $tag |
||
| 276 | * |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | protected function getClosingTag($tag) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $str |
||
| 286 | * @param string $start |
||
| 287 | * @param string $end |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | protected function getStringBetween($str, $start, $end) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param string $html |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | 11 | protected function purifyHtml($html) |
|
| 324 | |||
| 325 | 11 | protected function splitInputsToWords() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $text |
||
| 333 | * |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | 11 | protected function isPartOfWord($text) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @param array $characterString |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | 11 | protected function convertHtmlToListOfWords($characterString) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * @param string $val |
||
| 424 | * |
||
| 425 | * @return bool |
||
| 426 | */ |
||
| 427 | 11 | protected function isStartOfTag($val) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * @param string $val |
||
| 434 | * |
||
| 435 | * @return bool |
||
| 436 | */ |
||
| 437 | 11 | protected function isEndOfTag($val) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * @param string $value |
||
| 444 | * |
||
| 445 | * @return bool |
||
| 446 | */ |
||
| 447 | protected function isWhiteSpace($value) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param string $value |
||
| 454 | * |
||
| 455 | * @return array |
||
| 456 | */ |
||
| 457 | 11 | protected function explode($value) |
|
| 462 | } |
||
| 463 |
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.