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 | * Whether to keep newlines in the diff |
||
32 | * @var bool |
||
33 | */ |
||
34 | protected $keepNewLines = false; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $encoding = 'UTF-8'; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $isolatedDiffTags = array( |
||
45 | 'ol' => '[[REPLACE_ORDERED_LIST]]', |
||
46 | 'ul' => '[[REPLACE_UNORDERED_LIST]]', |
||
47 | 'sub' => '[[REPLACE_SUB_SCRIPT]]', |
||
48 | 'sup' => '[[REPLACE_SUPER_SCRIPT]]', |
||
49 | 'dl' => '[[REPLACE_DEFINITION_LIST]]', |
||
50 | 'table' => '[[REPLACE_TABLE]]', |
||
51 | 'strong' => '[[REPLACE_STRONG]]', |
||
52 | 'b' => '[[REPLACE_STRONG]]', |
||
53 | 'em' => '[[REPLACE_EM]]', |
||
54 | 'i' => '[[REPLACE_EM]]', |
||
55 | 'a' => '[[REPLACE_A]]', |
||
56 | 'img' => '[[REPLACE_IMG]]', |
||
57 | 'pre' => '[[REPLACE_PRE]]', |
||
58 | ); |
||
59 | |||
60 | /** |
||
61 | * @var int |
||
62 | */ |
||
63 | protected $matchThreshold = 80; |
||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $specialCaseOpeningTags = array(); |
||
68 | /** |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $specialCaseClosingTags = array(); |
||
72 | |||
73 | /** |
||
74 | * @var bool |
||
75 | */ |
||
76 | protected $useTableDiffing = true; |
||
77 | |||
78 | /** |
||
79 | * @var null|\Doctrine\Common\Cache\Cache |
||
80 | */ |
||
81 | protected $cacheProvider; |
||
82 | |||
83 | /** |
||
84 | * @var bool |
||
85 | */ |
||
86 | protected $purifierEnabled = true; |
||
87 | |||
88 | /** |
||
89 | * @var null|string |
||
90 | */ |
||
91 | protected $purifierCacheLocation = null; |
||
92 | |||
93 | /** |
||
94 | * @return HtmlDiffConfig |
||
95 | */ |
||
96 | 16 | public static function create() |
|
100 | |||
101 | /** |
||
102 | * HtmlDiffConfig constructor. |
||
103 | */ |
||
104 | 16 | public function __construct() |
|
108 | |||
109 | /** |
||
110 | * @return int |
||
111 | */ |
||
112 | 8 | public function getMatchThreshold() |
|
116 | |||
117 | /** |
||
118 | * @param int $matchThreshold |
||
119 | * |
||
120 | * @return AbstractDiff |
||
|
|||
121 | */ |
||
122 | public function setMatchThreshold($matchThreshold) |
||
128 | |||
129 | /** |
||
130 | * @param array $chars |
||
131 | */ |
||
132 | public function setSpecialCaseChars(array $chars) |
||
136 | |||
137 | /** |
||
138 | * @return array|null |
||
139 | */ |
||
140 | 16 | public function getSpecialCaseChars() |
|
144 | |||
145 | /** |
||
146 | * @param string $char |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function addSpecialCaseChar($char) |
||
158 | |||
159 | /** |
||
160 | * @param string $char |
||
161 | * |
||
162 | * @return $this |
||
163 | */ |
||
164 | public function removeSpecialCaseChar($char) |
||
173 | |||
174 | /** |
||
175 | * @param array $tags |
||
176 | * |
||
177 | * @return $this |
||
178 | */ |
||
179 | 16 | public function setSpecialCaseTags(array $tags = array()) |
|
191 | |||
192 | /** |
||
193 | * @param string $tag |
||
194 | * |
||
195 | * @return $this |
||
196 | */ |
||
197 | 16 | public function addSpecialCaseTag($tag) |
|
215 | |||
216 | /** |
||
217 | * @param string $tag |
||
218 | * |
||
219 | * @return $this |
||
220 | */ |
||
221 | public function removeSpecialCaseTag($tag) |
||
239 | |||
240 | /** |
||
241 | * @return array|null |
||
242 | */ |
||
243 | public function getSpecialCaseTags() |
||
247 | |||
248 | /** |
||
249 | * @return bool |
||
250 | */ |
||
251 | 16 | public function isGroupDiffs() |
|
255 | |||
256 | /** |
||
257 | * @param bool $groupDiffs |
||
258 | * |
||
259 | * @return HtmlDiffConfig |
||
260 | */ |
||
261 | public function setGroupDiffs($groupDiffs) |
||
267 | |||
268 | /** |
||
269 | * @return string |
||
270 | */ |
||
271 | 1 | public function getEncoding() |
|
275 | |||
276 | /** |
||
277 | * @param string $encoding |
||
278 | * |
||
279 | * @return HtmlDiffConfig |
||
280 | */ |
||
281 | 16 | public function setEncoding($encoding) |
|
287 | |||
288 | /** |
||
289 | * @return bool |
||
290 | */ |
||
291 | public function isInsertSpaceInReplace() |
||
295 | |||
296 | /** |
||
297 | * @param bool $insertSpaceInReplace |
||
298 | * |
||
299 | * @return HtmlDiffConfig |
||
300 | */ |
||
301 | public function setInsertSpaceInReplace($insertSpaceInReplace) |
||
307 | |||
308 | /** |
||
309 | * @return bool |
||
310 | */ |
||
311 | 16 | public function isKeepNewLines() |
|
315 | |||
316 | /** |
||
317 | * @param bool $keepNewLines |
||
318 | */ |
||
319 | public function setKeepNewLines($keepNewLines) |
||
323 | |||
324 | /** |
||
325 | * @return array |
||
326 | */ |
||
327 | 16 | public function getIsolatedDiffTags() |
|
331 | |||
332 | /** |
||
333 | * @param array $isolatedDiffTags |
||
334 | * |
||
335 | * @return HtmlDiffConfig |
||
336 | */ |
||
337 | public function setIsolatedDiffTags($isolatedDiffTags) |
||
343 | |||
344 | /** |
||
345 | * @param string $tag |
||
346 | * @param null|string $placeholder |
||
347 | * |
||
348 | * @return $this |
||
349 | */ |
||
350 | public function addIsolatedDiffTag($tag, $placeholder = null) |
||
375 | |||
376 | /** |
||
377 | * @param string $tag |
||
378 | * |
||
379 | * @return $this |
||
380 | */ |
||
381 | public function removeIsolatedDiffTag($tag) |
||
389 | |||
390 | /** |
||
391 | * @param string $tag |
||
392 | * |
||
393 | * @return bool |
||
394 | */ |
||
395 | 14 | public function isIsolatedDiffTag($tag) |
|
399 | |||
400 | /** |
||
401 | * @param string $text |
||
402 | * |
||
403 | * @return bool |
||
404 | */ |
||
405 | 16 | public function isIsolatedDiffTagPlaceholder($text) |
|
409 | |||
410 | /** |
||
411 | * @param string $tag |
||
412 | * |
||
413 | * @return null|string |
||
414 | */ |
||
415 | 14 | public function getIsolatedDiffTagPlaceholder($tag) |
|
419 | |||
420 | /** |
||
421 | * @return array |
||
422 | */ |
||
423 | 6 | public function getSpecialCaseOpeningTags() |
|
427 | |||
428 | /** |
||
429 | * @return array |
||
430 | */ |
||
431 | 6 | public function getSpecialCaseClosingTags() |
|
435 | |||
436 | /** |
||
437 | * @return bool |
||
438 | */ |
||
439 | 10 | public function isUseTableDiffing() |
|
443 | |||
444 | /** |
||
445 | * @param bool $useTableDiffing |
||
446 | * |
||
447 | * @return HtmlDiffConfig |
||
448 | */ |
||
449 | public function setUseTableDiffing($useTableDiffing) |
||
455 | |||
456 | /** |
||
457 | * @param null|\Doctrine\Common\Cache\Cache $cacheProvider |
||
458 | * |
||
459 | * @return $this |
||
460 | */ |
||
461 | public function setCacheProvider(\Doctrine\Common\Cache\Cache $cacheProvider = null) |
||
467 | |||
468 | /** |
||
469 | * @return null|\Doctrine\Common\Cache\Cache |
||
470 | */ |
||
471 | 16 | public function getCacheProvider() |
|
475 | |||
476 | 16 | public function isPurifierEnabled(): bool |
|
480 | |||
481 | public function setPurifierEnabled(bool $purifierEnabled = true): self |
||
487 | |||
488 | /** |
||
489 | * @param null|string |
||
490 | * |
||
491 | * @return $this |
||
492 | */ |
||
493 | 2 | public function setPurifierCacheLocation($purifierCacheLocation = null) |
|
499 | |||
500 | /** |
||
501 | * @return null|string |
||
502 | */ |
||
503 | 16 | public function getPurifierCacheLocation() |
|
507 | |||
508 | /** |
||
509 | * @param string $tag |
||
510 | * |
||
511 | * @return string |
||
512 | */ |
||
513 | 16 | protected function getOpeningTag($tag) |
|
517 | |||
518 | /** |
||
519 | * @param string $tag |
||
520 | * |
||
521 | * @return string |
||
522 | */ |
||
523 | 16 | protected function getClosingTag($tag) |
|
527 | } |
||
528 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.