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 | * @deprecated since 0.1.0 |
||
| 15 | */ |
||
| 16 | public static $defaultSpecialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
||
|
|
|||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | * |
||
| 20 | * @deprecated since 0.1.0 |
||
| 21 | */ |
||
| 22 | public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\''); |
||
| 23 | /** |
||
| 24 | * @var bool |
||
| 25 | * |
||
| 26 | * @deprecated since 0.1.0 |
||
| 27 | */ |
||
| 28 | public static $defaultGroupDiffs = true; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var HtmlDiffConfig |
||
| 32 | */ |
||
| 33 | protected $config; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $content; |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $oldText; |
||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $newText; |
||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $oldWords = array(); |
||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $newWords = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * AbstractDiff constructor. |
||
| 58 | * |
||
| 59 | * @param string $oldText |
||
| 60 | * @param string $newText |
||
| 61 | * @param string $encoding |
||
| 62 | * @param null|array $specialCaseTags |
||
| 63 | * @param null|bool $groupDiffs |
||
| 64 | */ |
||
| 65 | 11 | public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @return HtmlDiffConfig |
||
| 86 | */ |
||
| 87 | public function getConfig() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param HtmlDiffConfig $config |
||
| 94 | * |
||
| 95 | * @return AbstractDiff |
||
| 96 | */ |
||
| 97 | 7 | public function setConfig(HtmlDiffConfig $config) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @return int |
||
| 106 | * |
||
| 107 | * @deprecated since 0.1.0 |
||
| 108 | */ |
||
| 109 | public function getMatchThreshold() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param int $matchThreshold |
||
| 116 | * |
||
| 117 | * @return AbstractDiff |
||
| 118 | * |
||
| 119 | * @deprecated since 0.1.0 |
||
| 120 | */ |
||
| 121 | public function setMatchThreshold($matchThreshold) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param array $chars |
||
| 130 | * |
||
| 131 | * @deprecated since 0.1.0 |
||
| 132 | */ |
||
| 133 | public function setSpecialCaseChars(array $chars) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return array|null |
||
| 140 | * |
||
| 141 | * @deprecated since 0.1.0 |
||
| 142 | */ |
||
| 143 | public function getSpecialCaseChars() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $char |
||
| 150 | * |
||
| 151 | * @deprecated since 0.1.0 |
||
| 152 | */ |
||
| 153 | public function addSpecialCaseChar($char) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $char |
||
| 160 | * |
||
| 161 | * @deprecated since 0.1.0 |
||
| 162 | */ |
||
| 163 | public function removeSpecialCaseChar($char) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param array $tags |
||
| 170 | * |
||
| 171 | * @deprecated since 0.1.0 |
||
| 172 | */ |
||
| 173 | public function setSpecialCaseTags(array $tags = array()) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param string $tag |
||
| 180 | * |
||
| 181 | * @deprecated since 0.1.0 |
||
| 182 | */ |
||
| 183 | public function addSpecialCaseTag($tag) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param string $tag |
||
| 190 | * |
||
| 191 | * @deprecated since 0.1.0 |
||
| 192 | */ |
||
| 193 | public function removeSpecialCaseTag($tag) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return array|null |
||
| 200 | * |
||
| 201 | * @deprecated since 0.1.0 |
||
| 202 | */ |
||
| 203 | public function getSpecialCaseTags() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function getOldHtml() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public function getNewHtml() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function getDifference() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param bool $boolean |
||
| 234 | * |
||
| 235 | * @return $this |
||
| 236 | * |
||
| 237 | * @deprecated since 0.1.0 |
||
| 238 | */ |
||
| 239 | public function setGroupDiffs($boolean) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return bool |
||
| 248 | * |
||
| 249 | * @deprecated since 0.1.0 |
||
| 250 | */ |
||
| 251 | public function isGroupDiffs() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $tag |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | protected function getOpeningTag($tag) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $tag |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | protected function getClosingTag($tag) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param string $str |
||
| 278 | * @param string $start |
||
| 279 | * @param string $end |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | protected function getStringBetween($str, $start, $end) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $html |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | 11 | protected function purifyHtml($html) |
|
| 316 | |||
| 317 | 11 | protected function splitInputsToWords() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $text |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | 11 | protected function isPartOfWord($text) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param array $characterString |
||
| 335 | * |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | 11 | protected function convertHtmlToListOfWords($characterString) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * @param string $val |
||
| 416 | * |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | 11 | protected function isStartOfTag($val) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $val |
||
| 426 | * |
||
| 427 | * @return bool |
||
| 428 | */ |
||
| 429 | 11 | protected function isEndOfTag($val) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $value |
||
| 436 | * |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | protected function isWhiteSpace($value) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param string $value |
||
| 446 | * |
||
| 447 | * @return array |
||
| 448 | */ |
||
| 449 | 11 | protected function explode($value) |
|
| 454 | } |
||
| 455 |
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.