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 | 12 | public static function create() |
|
88 | |||
89 | /** |
||
90 | * HtmlDiffConfig constructor. |
||
91 | */ |
||
92 | 12 | public function __construct() |
|
96 | |||
97 | /** |
||
98 | * @return int |
||
99 | */ |
||
100 | 7 | 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 | 12 | public function getSpecialCaseChars() |
|
132 | |||
133 | /** |
||
134 | * @param string $char |
||
135 | * |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function addSpecialCaseChar($char) |
||
146 | |||
147 | /** |
||
148 | * @param string $char |
||
149 | * |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function removeSpecialCaseChar($char) |
||
161 | |||
162 | /** |
||
163 | * @param array $tags |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | 12 | public function setSpecialCaseTags(array $tags = array()) |
|
179 | |||
180 | /** |
||
181 | * @param string $tag |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | 12 | public function addSpecialCaseTag($tag) |
|
203 | |||
204 | /** |
||
205 | * @param string $tag |
||
206 | * |
||
207 | * @return $this |
||
208 | */ |
||
209 | public function removeSpecialCaseTag($tag) |
||
227 | |||
228 | /** |
||
229 | * @return array|null |
||
230 | */ |
||
231 | public function getSpecialCaseTags() |
||
235 | |||
236 | /** |
||
237 | * @return bool |
||
238 | */ |
||
239 | 12 | 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 | public function getEncoding() |
||
263 | |||
264 | /** |
||
265 | * @param string $encoding |
||
266 | * |
||
267 | * @return HtmlDiffConfig |
||
268 | */ |
||
269 | 12 | 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 | 12 | 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) |
||
347 | |||
348 | /** |
||
349 | * @param string $tag |
||
350 | * |
||
351 | * @return $this |
||
352 | */ |
||
353 | public function removeIsolatedDiffTag($tag) |
||
361 | |||
362 | /** |
||
363 | * @param string $tag |
||
364 | * |
||
365 | * @return bool |
||
366 | */ |
||
367 | 11 | public function isIsolatedDiffTag($tag) |
|
371 | |||
372 | /** |
||
373 | * @param string $text |
||
374 | * |
||
375 | * @return bool |
||
376 | */ |
||
377 | 12 | public function isIsolatedDiffTagPlaceholder($text) |
|
381 | |||
382 | /** |
||
383 | * @param string $tag |
||
384 | * |
||
385 | * @return null|string |
||
386 | */ |
||
387 | 11 | 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 | 7 | 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 | 12 | public function getCacheProvider() |
|
447 | |||
448 | /** |
||
449 | * @param null|string |
||
450 | * |
||
451 | * @return $this |
||
452 | */ |
||
453 | public function setPurifierCacheLocation($purifierCacheLocation = null) |
||
459 | |||
460 | /** |
||
461 | * @return null|string |
||
462 | */ |
||
463 | 12 | public function getPurifierCacheLocation() |
|
464 | { |
||
465 | 12 | return $this->purifierCacheLocation; |
|
466 | } |
||
467 | |||
468 | /** |
||
469 | * @param string $tag |
||
470 | * |
||
471 | * @return string |
||
472 | */ |
||
473 | 12 | protected function getOpeningTag($tag) |
|
477 | |||
478 | /** |
||
479 | * @param string $tag |
||
480 | * |
||
481 | * @return string |
||
482 | */ |
||
483 | 12 | protected function getClosingTag($tag) |
|
487 | } |
||
488 |
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.