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 |
||
| 8 | class HtmlDiffConfig |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var array |
||
| 12 | */ |
||
| 13 | protected $specialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected $specialCaseChars = array('.', ',', '(', ')', '\''); |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | protected $groupDiffs = true; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | protected $insertSpaceInReplace = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $encoding = 'UTF-8'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $isolatedDiffTags = array( |
||
| 39 | 'ol' => '[[REPLACE_ORDERED_LIST]]', |
||
| 40 | 'ul' => '[[REPLACE_UNORDERED_LIST]]', |
||
| 41 | 'sub' => '[[REPLACE_SUB_SCRIPT]]', |
||
| 42 | 'sup' => '[[REPLACE_SUPER_SCRIPT]]', |
||
| 43 | 'dl' => '[[REPLACE_DEFINITION_LIST]]', |
||
| 44 | 'table' => '[[REPLACE_TABLE]]', |
||
| 45 | 'strong' => '[[REPLACE_STRONG]]', |
||
| 46 | 'b' => '[[REPLACE_STRONG]]', |
||
| 47 | 'em' => '[[REPLACE_EM]]', |
||
| 48 | 'i' => '[[REPLACE_EM]]', |
||
| 49 | 'a' => '[[REPLACE_A]]', |
||
| 50 | 'img' => '[[REPLACE_IMG]]', |
||
| 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 | 15 | public static function create() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * HtmlDiffConfig constructor. |
||
| 91 | */ |
||
| 92 | 15 | public function __construct() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @return int |
||
| 99 | */ |
||
| 100 | 8 | 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 | 15 | public function getSpecialCaseChars() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $char |
||
| 135 | * |
||
| 136 | * @return $this |
||
| 137 | */ |
||
| 138 | public function addSpecialCaseChar($char) |
||
| 139 | { |
||
| 140 | if (!in_array($char, $this->specialCaseChars)) { |
||
| 141 | $this->specialCaseChars[] = $char; |
||
| 142 | } |
||
| 143 | |||
| 144 | return $this; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $char |
||
| 149 | * |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | public function removeSpecialCaseChar($char) |
||
| 153 | { |
||
| 154 | $key = array_search($char, $this->specialCaseChars); |
||
| 155 | if ($key !== false) { |
||
| 156 | unset($this->specialCaseChars[$key]); |
||
| 157 | } |
||
| 158 | |||
| 159 | return $this; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param array $tags |
||
| 164 | * |
||
| 165 | * @return $this |
||
| 166 | */ |
||
| 167 | 15 | public function setSpecialCaseTags(array $tags = array()) |
|
| 168 | { |
||
| 169 | 15 | $this->specialCaseTags = $tags; |
|
| 170 | 15 | $this->specialCaseOpeningTags = array(); |
|
| 171 | 15 | $this->specialCaseClosingTags = array(); |
|
| 172 | |||
| 173 | 15 | foreach ($this->specialCaseTags as $tag) { |
|
| 174 | 15 | $this->addSpecialCaseTag($tag); |
|
| 175 | } |
||
| 176 | |||
| 177 | 15 | return $this; |
|
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $tag |
||
| 182 | * |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | 15 | public function addSpecialCaseTag($tag) |
|
| 186 | { |
||
| 187 | 15 | if (!in_array($tag, $this->specialCaseTags)) { |
|
| 188 | $this->specialCaseTags[] = $tag; |
||
| 189 | } |
||
| 190 | |||
| 191 | 15 | $opening = $this->getOpeningTag($tag); |
|
| 192 | 15 | $closing = $this->getClosingTag($tag); |
|
| 193 | |||
| 194 | 15 | if (!in_array($opening, $this->specialCaseOpeningTags)) { |
|
| 195 | 15 | $this->specialCaseOpeningTags[] = $opening; |
|
| 196 | } |
||
| 197 | 15 | if (!in_array($closing, $this->specialCaseClosingTags)) { |
|
| 198 | 15 | $this->specialCaseClosingTags[] = $closing; |
|
| 199 | } |
||
| 200 | |||
| 201 | 15 | return $this; |
|
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $tag |
||
| 206 | * |
||
| 207 | * @return $this |
||
| 208 | */ |
||
| 209 | public function removeSpecialCaseTag($tag) |
||
| 210 | { |
||
| 211 | if (($key = array_search($tag, $this->specialCaseTags)) !== false) { |
||
| 212 | unset($this->specialCaseTags[$key]); |
||
| 213 | |||
| 214 | $opening = $this->getOpeningTag($tag); |
||
| 215 | $closing = $this->getClosingTag($tag); |
||
| 216 | |||
| 217 | if (($key = array_search($opening, $this->specialCaseOpeningTags)) !== false) { |
||
| 218 | unset($this->specialCaseOpeningTags[$key]); |
||
| 219 | } |
||
| 220 | if (($key = array_search($closing, $this->specialCaseClosingTags)) !== false) { |
||
| 221 | unset($this->specialCaseClosingTags[$key]); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | return $this; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return array|null |
||
| 230 | */ |
||
| 231 | public function getSpecialCaseTags() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | 15 | public function isGroupDiffs() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param bool $groupDiffs |
||
| 246 | * |
||
| 247 | * @return HtmlDiffConfig |
||
| 248 | */ |
||
| 249 | public function setGroupDiffs($groupDiffs) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | 1 | public function getEncoding() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $encoding |
||
| 266 | * |
||
| 267 | * @return HtmlDiffConfig |
||
| 268 | */ |
||
| 269 | 15 | public function setEncoding($encoding) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | public function isInsertSpaceInReplace() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param bool $insertSpaceInReplace |
||
| 286 | * |
||
| 287 | * @return HtmlDiffConfig |
||
| 288 | */ |
||
| 289 | public function setInsertSpaceInReplace($insertSpaceInReplace) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | 15 | public function getIsolatedDiffTags() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @param array $isolatedDiffTags |
||
| 306 | * |
||
| 307 | * @return HtmlDiffConfig |
||
| 308 | */ |
||
| 309 | public function setIsolatedDiffTags($isolatedDiffTags) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param string $tag |
||
| 318 | * @param null|string $placeholder |
||
| 319 | * |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | public function addIsolatedDiffTag($tag, $placeholder = null) |
||
| 323 | { |
||
| 324 | if (null === $placeholder) { |
||
| 325 | $placeholder = sprintf('[[REPLACE_%s]]', strtoupper($tag)); |
||
| 326 | } |
||
| 327 | |||
| 328 | if ($this->isIsolatedDiffTag($tag) && $this->isolatedDiffTags[$tag] !== $placeholder) { |
||
| 329 | throw new \InvalidArgumentException( |
||
| 330 | sprintf('Isolated diff tag "%s" already exists using a different placeholder', $tag) |
||
| 331 | ); |
||
| 332 | } |
||
| 333 | |||
| 334 | $matchingKey = array_search($placeholder, $this->isolatedDiffTags, true); |
||
| 335 | if (false !== $matchingKey && $matchingKey !== $tag) { |
||
| 336 | throw new \InvalidArgumentException( |
||
| 337 | sprintf('Placeholder already being used for a different tag "%s"', $tag) |
||
| 338 | ); |
||
| 339 | } |
||
| 340 | |||
| 341 | if (!array_key_exists($tag, $this->isolatedDiffTags)) { |
||
| 342 | $this->isolatedDiffTags[$tag] = $placeholder; |
||
| 343 | } |
||
| 344 | |||
| 345 | return $this; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param string $tag |
||
| 350 | * |
||
| 351 | * @return $this |
||
| 352 | */ |
||
| 353 | public function removeIsolatedDiffTag($tag) |
||
| 354 | { |
||
| 355 | if ($this->isIsolatedDiffTag($tag)) { |
||
| 356 | unset($this->isolatedDiffTags[$tag]); |
||
| 357 | } |
||
| 358 | |||
| 359 | return $this; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param string $tag |
||
| 364 | * |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | 14 | public function isIsolatedDiffTag($tag) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @param string $text |
||
| 374 | * |
||
| 375 | * @return bool |
||
| 376 | */ |
||
| 377 | 15 | public function isIsolatedDiffTagPlaceholder($text) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @param string $tag |
||
| 384 | * |
||
| 385 | * @return null|string |
||
| 386 | */ |
||
| 387 | 14 | public function getIsolatedDiffTagPlaceholder($tag) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | 6 | public function getSpecialCaseOpeningTags() |
|
| 399 | |||
| 400 | /** |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | 6 | public function getSpecialCaseClosingTags() |
|
| 407 | |||
| 408 | /** |
||
| 409 | * @return bool |
||
| 410 | */ |
||
| 411 | 10 | public function isUseTableDiffing() |
|
| 415 | |||
| 416 | /** |
||
| 417 | * @param bool $useTableDiffing |
||
| 418 | * |
||
| 419 | * @return HtmlDiffConfig |
||
| 420 | */ |
||
| 421 | public function setUseTableDiffing($useTableDiffing) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param null|\Doctrine\Common\Cache\Cache $cacheProvider |
||
| 430 | * |
||
| 431 | * @return $this |
||
| 432 | */ |
||
| 433 | public function setCacheProvider(\Doctrine\Common\Cache\Cache $cacheProvider = null) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @return null|\Doctrine\Common\Cache\Cache |
||
| 442 | */ |
||
| 443 | 15 | public function getCacheProvider() |
|
| 447 | |||
| 448 | /** |
||
| 449 | * @param null|string |
||
| 450 | * |
||
| 451 | * @return $this |
||
| 452 | */ |
||
| 453 | 2 | public function setPurifierCacheLocation($purifierCacheLocation = null) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * @return null|string |
||
| 462 | */ |
||
| 463 | 15 | public function getPurifierCacheLocation() |
|
| 467 | |||
| 468 | /** |
||
| 469 | * @param string $tag |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | 15 | protected function getOpeningTag($tag) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * @param string $tag |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | 15 | protected function getClosingTag($tag) |
|
| 487 | } |
||
| 488 |
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.