Complex classes like AbstractDiff 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 AbstractDiff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | abstract class AbstractDiff |
||
10 | { |
||
11 | /** |
||
12 | * @var array |
||
13 | * |
||
14 | * @deprecated since 0.1.0 |
||
15 | */ |
||
16 | public static $defaultSpecialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
||
|
|||
17 | /** |
||
18 | * @var array |
||
19 | * |
||
20 | * @deprecated since 0.1.0 |
||
21 | */ |
||
22 | public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\''); |
||
23 | /** |
||
24 | * @var bool |
||
25 | * |
||
26 | * @deprecated since 0.1.0 |
||
27 | */ |
||
28 | public static $defaultGroupDiffs = true; |
||
29 | |||
30 | /** |
||
31 | * @var HtmlDiffConfig |
||
32 | */ |
||
33 | protected $config; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $content; |
||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $oldText; |
||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $newText; |
||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $oldWords = array(); |
||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $newWords = array(); |
||
55 | |||
56 | /** |
||
57 | * @var DiffCache[] |
||
58 | */ |
||
59 | private $diffCaches = array(); |
||
60 | |||
61 | /** |
||
62 | * AbstractDiff constructor. |
||
63 | * |
||
64 | * @param string $oldText |
||
65 | * @param string $newText |
||
66 | * @param string $encoding |
||
67 | * @param null|array $specialCaseTags |
||
68 | * @param null|bool $groupDiffs |
||
69 | */ |
||
70 | 11 | public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null) |
|
71 | { |
||
72 | 11 | mb_substitute_character(0x20); |
|
73 | |||
74 | 11 | $this->config = HtmlDiffConfig::create()->setEncoding($encoding); |
|
75 | |||
76 | 11 | if ($specialCaseTags !== null) { |
|
77 | 11 | $this->config->setSpecialCaseTags($specialCaseTags); |
|
78 | 11 | } |
|
79 | |||
80 | 11 | if ($groupDiffs !== null) { |
|
81 | $this->config->setGroupDiffs($groupDiffs); |
||
82 | } |
||
83 | |||
84 | 11 | $this->oldText = $this->purifyHtml(trim($oldText)); |
|
85 | 11 | $this->newText = $this->purifyHtml(trim($newText)); |
|
86 | 11 | $this->content = ''; |
|
87 | 11 | } |
|
88 | |||
89 | /** |
||
90 | * @return bool|string |
||
91 | */ |
||
92 | abstract public function build(); |
||
93 | |||
94 | /** |
||
95 | * @return DiffCache|null |
||
96 | */ |
||
97 | protected function getDiffCache() |
||
98 | { |
||
99 | if (!$this->hasDiffCache()) { |
||
100 | return null; |
||
101 | } |
||
102 | |||
103 | $hash = spl_object_hash($this->getConfig()->getCacheProvider()); |
||
104 | |||
105 | if (!array_key_exists($hash, $this->diffCaches)) { |
||
106 | $this->diffCaches[$hash] = new DiffCache($this->getConfig()->getCacheProvider()); |
||
107 | } |
||
108 | |||
109 | return $this->diffCaches[$hash]; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @return bool |
||
114 | */ |
||
115 | 11 | protected function hasDiffCache() |
|
116 | { |
||
117 | 11 | return null !== $this->getConfig()->getCacheProvider(); |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * @return HtmlDiffConfig |
||
122 | */ |
||
123 | 11 | public function getConfig() |
|
124 | { |
||
125 | 11 | return $this->config; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param HtmlDiffConfig $config |
||
130 | * |
||
131 | * @return AbstractDiff |
||
132 | */ |
||
133 | 7 | public function setConfig(HtmlDiffConfig $config) |
|
134 | { |
||
135 | 7 | $this->config = $config; |
|
136 | |||
137 | 7 | return $this; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return int |
||
142 | * |
||
143 | * @deprecated since 0.1.0 |
||
144 | */ |
||
145 | public function getMatchThreshold() |
||
149 | |||
150 | /** |
||
151 | * @param int $matchThreshold |
||
152 | * |
||
153 | * @return AbstractDiff |
||
154 | * |
||
155 | * @deprecated since 0.1.0 |
||
156 | */ |
||
157 | public function setMatchThreshold($matchThreshold) |
||
163 | |||
164 | /** |
||
165 | * @param array $chars |
||
166 | * |
||
167 | * @deprecated since 0.1.0 |
||
168 | */ |
||
169 | public function setSpecialCaseChars(array $chars) |
||
173 | |||
174 | /** |
||
175 | * @return array|null |
||
176 | * |
||
177 | * @deprecated since 0.1.0 |
||
178 | */ |
||
179 | public function getSpecialCaseChars() |
||
183 | |||
184 | /** |
||
185 | * @param string $char |
||
186 | * |
||
187 | * @deprecated since 0.1.0 |
||
188 | */ |
||
189 | public function addSpecialCaseChar($char) |
||
193 | |||
194 | /** |
||
195 | * @param string $char |
||
196 | * |
||
197 | * @deprecated since 0.1.0 |
||
198 | */ |
||
199 | public function removeSpecialCaseChar($char) |
||
203 | |||
204 | /** |
||
205 | * @param array $tags |
||
206 | * |
||
207 | * @deprecated since 0.1.0 |
||
208 | */ |
||
209 | public function setSpecialCaseTags(array $tags = array()) |
||
210 | { |
||
211 | $this->config->setSpecialCaseChars($tags); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @param string $tag |
||
216 | * |
||
217 | * @deprecated since 0.1.0 |
||
218 | */ |
||
219 | public function addSpecialCaseTag($tag) |
||
220 | { |
||
221 | $this->config->addSpecialCaseTag($tag); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @param string $tag |
||
226 | * |
||
227 | * @deprecated since 0.1.0 |
||
228 | */ |
||
229 | public function removeSpecialCaseTag($tag) |
||
230 | { |
||
231 | $this->config->removeSpecialCaseTag($tag); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @return array|null |
||
236 | * |
||
237 | * @deprecated since 0.1.0 |
||
238 | */ |
||
239 | public function getSpecialCaseTags() |
||
243 | |||
244 | /** |
||
245 | * @return string |
||
246 | */ |
||
247 | public function getOldHtml() |
||
248 | { |
||
249 | return $this->oldText; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @return string |
||
254 | */ |
||
255 | public function getNewHtml() |
||
256 | { |
||
257 | return $this->newText; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @return string |
||
262 | */ |
||
263 | public function getDifference() |
||
264 | { |
||
265 | return $this->content; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param bool $boolean |
||
270 | * |
||
271 | * @return $this |
||
272 | * |
||
273 | * @deprecated since 0.1.0 |
||
274 | */ |
||
275 | public function setGroupDiffs($boolean) |
||
281 | |||
282 | /** |
||
283 | * @return bool |
||
284 | * |
||
285 | * @deprecated since 0.1.0 |
||
286 | */ |
||
287 | public function isGroupDiffs() |
||
288 | { |
||
289 | return $this->config->isGroupDiffs(); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @param string $tag |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | protected function getOpeningTag($tag) |
||
301 | |||
302 | /** |
||
303 | * @param string $tag |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | protected function getClosingTag($tag) |
||
311 | |||
312 | /** |
||
313 | * @param string $str |
||
314 | * @param string $start |
||
315 | * @param string $end |
||
316 | * |
||
317 | * @return string |
||
318 | */ |
||
319 | protected function getStringBetween($str, $start, $end) |
||
333 | |||
334 | /** |
||
335 | * @param string $html |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | 11 | protected function purifyHtml($html) |
|
352 | |||
353 | 11 | protected function splitInputsToWords() |
|
358 | |||
359 | /** |
||
360 | * @param string $text |
||
361 | * |
||
362 | * @return bool |
||
363 | */ |
||
364 | 11 | protected function isPartOfWord($text) |
|
368 | |||
369 | /** |
||
370 | * @param array $characterString |
||
371 | * |
||
372 | * @return array |
||
373 | */ |
||
374 | 11 | protected function convertHtmlToListOfWords($characterString) |
|
449 | |||
450 | /** |
||
451 | * @param string $val |
||
452 | * |
||
453 | * @return bool |
||
454 | */ |
||
455 | 11 | protected function isStartOfTag($val) |
|
459 | |||
460 | /** |
||
461 | * @param string $val |
||
462 | * |
||
463 | * @return bool |
||
464 | */ |
||
465 | 11 | protected function isEndOfTag($val) |
|
469 | |||
470 | /** |
||
471 | * @param string $value |
||
472 | * |
||
473 | * @return bool |
||
474 | */ |
||
475 | protected function isWhiteSpace($value) |
||
479 | |||
480 | /** |
||
481 | * @param string $value |
||
482 | * |
||
483 | * @return array |
||
484 | */ |
||
485 | 11 | protected function explode($value) |
|
490 | } |
||
491 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.