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 null|string |
||
85 | */ |
||
86 | protected $purifierCacheLocation = null; |
||
87 | |||
88 | /** |
||
89 | * @return HtmlDiffConfig |
||
90 | */ |
||
91 | 15 | public static function create() |
|
95 | |||
96 | /** |
||
97 | * HtmlDiffConfig constructor. |
||
98 | */ |
||
99 | 15 | public function __construct() |
|
103 | |||
104 | /** |
||
105 | * @return int |
||
106 | */ |
||
107 | 8 | public function getMatchThreshold() |
|
111 | |||
112 | /** |
||
113 | * @param int $matchThreshold |
||
114 | * |
||
115 | * @return AbstractDiff |
||
|
|||
116 | */ |
||
117 | public function setMatchThreshold($matchThreshold) |
||
123 | |||
124 | /** |
||
125 | * @param array $chars |
||
126 | */ |
||
127 | public function setSpecialCaseChars(array $chars) |
||
131 | |||
132 | /** |
||
133 | * @return array|null |
||
134 | */ |
||
135 | 15 | public function getSpecialCaseChars() |
|
139 | |||
140 | /** |
||
141 | * @param string $char |
||
142 | * |
||
143 | * @return $this |
||
144 | */ |
||
145 | public function addSpecialCaseChar($char) |
||
153 | |||
154 | /** |
||
155 | * @param string $char |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | public function removeSpecialCaseChar($char) |
||
168 | |||
169 | /** |
||
170 | * @param array $tags |
||
171 | * |
||
172 | * @return $this |
||
173 | */ |
||
174 | 15 | public function setSpecialCaseTags(array $tags = array()) |
|
186 | |||
187 | /** |
||
188 | * @param string $tag |
||
189 | * |
||
190 | * @return $this |
||
191 | */ |
||
192 | 15 | public function addSpecialCaseTag($tag) |
|
210 | |||
211 | /** |
||
212 | * @param string $tag |
||
213 | * |
||
214 | * @return $this |
||
215 | */ |
||
216 | public function removeSpecialCaseTag($tag) |
||
234 | |||
235 | /** |
||
236 | * @return array|null |
||
237 | */ |
||
238 | public function getSpecialCaseTags() |
||
242 | |||
243 | /** |
||
244 | * @return bool |
||
245 | */ |
||
246 | 15 | public function isGroupDiffs() |
|
250 | |||
251 | /** |
||
252 | * @param bool $groupDiffs |
||
253 | * |
||
254 | * @return HtmlDiffConfig |
||
255 | */ |
||
256 | public function setGroupDiffs($groupDiffs) |
||
262 | |||
263 | /** |
||
264 | * @return string |
||
265 | */ |
||
266 | 1 | public function getEncoding() |
|
270 | |||
271 | /** |
||
272 | * @param string $encoding |
||
273 | * |
||
274 | * @return HtmlDiffConfig |
||
275 | */ |
||
276 | 15 | public function setEncoding($encoding) |
|
282 | |||
283 | /** |
||
284 | * @return bool |
||
285 | */ |
||
286 | public function isInsertSpaceInReplace() |
||
290 | |||
291 | /** |
||
292 | * @param bool $insertSpaceInReplace |
||
293 | * |
||
294 | * @return HtmlDiffConfig |
||
295 | */ |
||
296 | public function setInsertSpaceInReplace($insertSpaceInReplace) |
||
302 | |||
303 | /** |
||
304 | * @return bool |
||
305 | */ |
||
306 | 15 | public function isKeepNewLines() |
|
310 | |||
311 | /** |
||
312 | * @param bool $keepNewLines |
||
313 | */ |
||
314 | public function setKeepNewLines($keepNewLines) |
||
318 | |||
319 | /** |
||
320 | * @return array |
||
321 | */ |
||
322 | 15 | public function getIsolatedDiffTags() |
|
326 | |||
327 | /** |
||
328 | * @param array $isolatedDiffTags |
||
329 | * |
||
330 | * @return HtmlDiffConfig |
||
331 | */ |
||
332 | public function setIsolatedDiffTags($isolatedDiffTags) |
||
338 | |||
339 | /** |
||
340 | * @param string $tag |
||
341 | * @param null|string $placeholder |
||
342 | * |
||
343 | * @return $this |
||
344 | */ |
||
345 | public function addIsolatedDiffTag($tag, $placeholder = null) |
||
370 | |||
371 | /** |
||
372 | * @param string $tag |
||
373 | * |
||
374 | * @return $this |
||
375 | */ |
||
376 | public function removeIsolatedDiffTag($tag) |
||
384 | |||
385 | /** |
||
386 | * @param string $tag |
||
387 | * |
||
388 | * @return bool |
||
389 | */ |
||
390 | 14 | public function isIsolatedDiffTag($tag) |
|
394 | |||
395 | /** |
||
396 | * @param string $text |
||
397 | * |
||
398 | * @return bool |
||
399 | */ |
||
400 | 15 | public function isIsolatedDiffTagPlaceholder($text) |
|
404 | |||
405 | /** |
||
406 | * @param string $tag |
||
407 | * |
||
408 | * @return null|string |
||
409 | */ |
||
410 | 14 | public function getIsolatedDiffTagPlaceholder($tag) |
|
414 | |||
415 | /** |
||
416 | * @return array |
||
417 | */ |
||
418 | 6 | public function getSpecialCaseOpeningTags() |
|
422 | |||
423 | /** |
||
424 | * @return array |
||
425 | */ |
||
426 | 6 | public function getSpecialCaseClosingTags() |
|
430 | |||
431 | /** |
||
432 | * @return bool |
||
433 | */ |
||
434 | 10 | public function isUseTableDiffing() |
|
438 | |||
439 | /** |
||
440 | * @param bool $useTableDiffing |
||
441 | * |
||
442 | * @return HtmlDiffConfig |
||
443 | */ |
||
444 | public function setUseTableDiffing($useTableDiffing) |
||
450 | |||
451 | /** |
||
452 | * @param null|\Doctrine\Common\Cache\Cache $cacheProvider |
||
453 | * |
||
454 | * @return $this |
||
455 | */ |
||
456 | public function setCacheProvider(\Doctrine\Common\Cache\Cache $cacheProvider = null) |
||
462 | |||
463 | /** |
||
464 | * @return null|\Doctrine\Common\Cache\Cache |
||
465 | */ |
||
466 | 15 | public function getCacheProvider() |
|
470 | |||
471 | /** |
||
472 | * @param null|string |
||
473 | * |
||
474 | * @return $this |
||
475 | */ |
||
476 | 2 | public function setPurifierCacheLocation($purifierCacheLocation = null) |
|
482 | |||
483 | /** |
||
484 | * @return null|string |
||
485 | */ |
||
486 | 15 | public function getPurifierCacheLocation() |
|
490 | |||
491 | /** |
||
492 | * @param string $tag |
||
493 | * |
||
494 | * @return string |
||
495 | */ |
||
496 | 15 | protected function getOpeningTag($tag) |
|
500 | |||
501 | /** |
||
502 | * @param string $tag |
||
503 | * |
||
504 | * @return string |
||
505 | */ |
||
506 | 15 | protected function getClosingTag($tag) |
|
510 | } |
||
511 |
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.