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 |
||
9 | class HtmlDiffConfig |
||
10 | { |
||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $specialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $specialCaseChars = array('.', ',', '(', ')', '\''); |
||
20 | |||
21 | /** |
||
22 | * @var bool |
||
23 | */ |
||
24 | protected $groupDiffs = true; |
||
25 | |||
26 | /** |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $insertSpaceInReplace = false; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $encoding = 'UTF-8'; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $isolatedDiffTags = array( |
||
40 | 'ol' => '[[REPLACE_ORDERED_LIST]]', |
||
41 | 'ul' => '[[REPLACE_UNORDERED_LIST]]', |
||
42 | 'sub' => '[[REPLACE_SUB_SCRIPT]]', |
||
43 | 'sup' => '[[REPLACE_SUPER_SCRIPT]]', |
||
44 | 'dl' => '[[REPLACE_DEFINITION_LIST]]', |
||
45 | 'table' => '[[REPLACE_TABLE]]', |
||
46 | 'strong' => '[[REPLACE_STRONG]]', |
||
47 | 'b' => '[[REPLACE_B]]', |
||
48 | 'em' => '[[REPLACE_EM]]', |
||
49 | 'i' => '[[REPLACE_I]]', |
||
50 | 'a' => '[[REPLACE_A]]', |
||
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 | * @return HtmlDiffConfig |
||
73 | */ |
||
74 | 11 | public static function create() |
|
78 | |||
79 | /** |
||
80 | * HtmlDiffConfig constructor. |
||
81 | */ |
||
82 | 11 | public function __construct() |
|
86 | |||
87 | /** |
||
88 | * @return int |
||
89 | */ |
||
90 | 4 | public function getMatchThreshold() |
|
94 | |||
95 | /** |
||
96 | * @param int $matchThreshold |
||
97 | * |
||
98 | * @return AbstractDiff |
||
|
|||
99 | */ |
||
100 | public function setMatchThreshold($matchThreshold) |
||
106 | |||
107 | /** |
||
108 | * @param array $chars |
||
109 | */ |
||
110 | public function setSpecialCaseChars(array $chars) |
||
114 | |||
115 | /** |
||
116 | * @return array|null |
||
117 | */ |
||
118 | 11 | public function getSpecialCaseChars() |
|
122 | |||
123 | /** |
||
124 | * @param string $char |
||
125 | * |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function addSpecialCaseChar($char) |
||
136 | |||
137 | /** |
||
138 | * @param string $char |
||
139 | * |
||
140 | * @return $this |
||
141 | */ |
||
142 | public function removeSpecialCaseChar($char) |
||
151 | |||
152 | /** |
||
153 | * @param array $tags |
||
154 | * |
||
155 | * @return $this |
||
156 | */ |
||
157 | 11 | public function setSpecialCaseTags(array $tags = array()) |
|
169 | |||
170 | /** |
||
171 | * @param string $tag |
||
172 | * |
||
173 | * @return $this |
||
174 | */ |
||
175 | 11 | public function addSpecialCaseTag($tag) |
|
193 | |||
194 | /** |
||
195 | * @param string $tag |
||
196 | * |
||
197 | * @return $this |
||
198 | */ |
||
199 | public function removeSpecialCaseTag($tag) |
||
217 | |||
218 | /** |
||
219 | * @return array|null |
||
220 | */ |
||
221 | public function getSpecialCaseTags() |
||
225 | |||
226 | |||
227 | /** |
||
228 | * @return boolean |
||
229 | */ |
||
230 | 11 | public function isGroupDiffs() |
|
234 | |||
235 | /** |
||
236 | * @param boolean $groupDiffs |
||
237 | * |
||
238 | * @return HtmlDiffConfig |
||
239 | */ |
||
240 | public function setGroupDiffs($groupDiffs) |
||
246 | |||
247 | /** |
||
248 | * @return string |
||
249 | */ |
||
250 | public function getEncoding() |
||
254 | |||
255 | /** |
||
256 | * @param string $encoding |
||
257 | * |
||
258 | * @return HtmlDiffConfig |
||
259 | */ |
||
260 | 11 | public function setEncoding($encoding) |
|
266 | |||
267 | /** |
||
268 | * @return boolean |
||
269 | */ |
||
270 | public function isInsertSpaceInReplace() |
||
274 | |||
275 | /** |
||
276 | * @param boolean $insertSpaceInReplace |
||
277 | * |
||
278 | * @return HtmlDiffConfig |
||
279 | */ |
||
280 | public function setInsertSpaceInReplace($insertSpaceInReplace) |
||
286 | |||
287 | /** |
||
288 | * @return array |
||
289 | */ |
||
290 | 11 | public function getIsolatedDiffTags() |
|
294 | |||
295 | /** |
||
296 | * @param array $isolatedDiffTags |
||
297 | * |
||
298 | * @return HtmlDiffConfig |
||
299 | */ |
||
300 | public function setIsolatedDiffTags($isolatedDiffTags) |
||
306 | |||
307 | /** |
||
308 | * @param string $tag |
||
309 | * @param null|string $placeholder |
||
310 | * |
||
311 | * @return $this |
||
312 | */ |
||
313 | public function addIsolatedDiffTag($tag, $placeholder = null) |
||
338 | |||
339 | /** |
||
340 | * @param string $tag |
||
341 | * |
||
342 | * @return $this |
||
343 | */ |
||
344 | public function removeIsolatedDiffTag($tag) |
||
352 | |||
353 | /** |
||
354 | * @param string $tag |
||
355 | * |
||
356 | * @return bool |
||
357 | */ |
||
358 | 11 | public function isIsolatedDiffTag($tag) |
|
362 | |||
363 | /** |
||
364 | * @param string $text |
||
365 | * |
||
366 | * @return bool |
||
367 | */ |
||
368 | 11 | public function isIsolatedDiffTagPlaceholder($text) |
|
372 | |||
373 | /** |
||
374 | * @param string $tag |
||
375 | * |
||
376 | * @return null|string |
||
377 | */ |
||
378 | 11 | public function getIsolatedDiffTagPlaceholder($tag) |
|
382 | |||
383 | /** |
||
384 | * @return array |
||
385 | */ |
||
386 | 6 | public function getSpecialCaseOpeningTags() |
|
390 | |||
391 | /** |
||
392 | * @return array |
||
393 | */ |
||
394 | 6 | public function getSpecialCaseClosingTags() |
|
398 | |||
399 | /** |
||
400 | * @return boolean |
||
401 | */ |
||
402 | 5 | public function isUseTableDiffing() |
|
406 | |||
407 | /** |
||
408 | * @param boolean $useTableDiffing |
||
409 | * |
||
410 | * @return HtmlDiffConfig |
||
411 | */ |
||
412 | public function setUseTableDiffing($useTableDiffing) |
||
418 | |||
419 | /** |
||
420 | * @param string $tag |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | 11 | protected function getOpeningTag($tag) |
|
428 | |||
429 | /** |
||
430 | * @param string $tag |
||
431 | * |
||
432 | * @return string |
||
433 | */ |
||
434 | 11 | protected function getClosingTag($tag) |
|
438 | } |
||
439 |
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.