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 |
||
5 | abstract class AbstractDiff |
||
6 | { |
||
7 | const STRATEGY_MATCHING = 'matching'; |
||
8 | const STRATEGY_RELATIVE = 'relative'; |
||
9 | |||
10 | public static $defaultSpecialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
||
|
|||
11 | public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\''); |
||
12 | public static $defaultGroupDiffs = true; |
||
13 | |||
14 | protected $content; |
||
15 | protected $oldText; |
||
16 | protected $newText; |
||
17 | protected $oldWords = array(); |
||
18 | protected $newWords = array(); |
||
19 | protected $encoding; |
||
20 | protected $specialCaseOpeningTags = array(); |
||
21 | protected $specialCaseClosingTags = array(); |
||
22 | protected $specialCaseTags; |
||
23 | protected $specialCaseChars; |
||
24 | protected $groupDiffs; |
||
25 | protected $matchThreshold = 80; |
||
26 | protected $debug = false; |
||
27 | |||
28 | protected $strategy = self::STRATEGY_MATCHING; |
||
29 | |||
30 | 11 | public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null) |
|
31 | { |
||
32 | 11 | if ($specialCaseTags === null) { |
|
33 | $specialCaseTags = static::$defaultSpecialCaseTags; |
||
34 | } |
||
35 | |||
36 | 11 | if ($groupDiffs === null) { |
|
37 | 11 | $groupDiffs = static::$defaultGroupDiffs; |
|
38 | 11 | } |
|
39 | |||
40 | 11 | $this->oldText = $this->purifyHtml(trim($oldText)); |
|
41 | 11 | $this->newText = $this->purifyHtml(trim($newText)); |
|
42 | 11 | $this->encoding = $encoding; |
|
43 | 11 | $this->content = ''; |
|
44 | 11 | $this->groupDiffs = $groupDiffs; |
|
45 | 11 | $this->setSpecialCaseTags($specialCaseTags); |
|
46 | 11 | $this->setSpecialCaseChars(static::$defaultSpecialCaseChars); |
|
47 | 11 | } |
|
48 | |||
49 | public function setStrategy($strategy) |
||
50 | { |
||
51 | $this->strategy = $strategy; |
||
52 | |||
53 | return $this; |
||
54 | } |
||
55 | |||
56 | public function getStrategy() |
||
57 | { |
||
58 | return $this->strategy; |
||
59 | } |
||
60 | |||
61 | public function setDebug($debug) |
||
62 | { |
||
63 | $this->debug = $debug; |
||
64 | |||
65 | return $this; |
||
66 | } |
||
67 | |||
68 | public function getDebug() |
||
69 | { |
||
70 | return $this->debug; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return int |
||
75 | */ |
||
76 | public function getMatchThreshold() |
||
77 | { |
||
78 | return $this->matchThreshold; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param int $matchThreshold |
||
83 | * |
||
84 | * @return AbstractDiff |
||
85 | */ |
||
86 | 4 | public function setMatchThreshold($matchThreshold) |
|
87 | { |
||
88 | 4 | $this->matchThreshold = $matchThreshold; |
|
89 | |||
90 | 4 | return $this; |
|
91 | } |
||
92 | |||
93 | |||
94 | |||
95 | 11 | public function setSpecialCaseChars(array $chars) |
|
96 | { |
||
97 | 11 | $this->specialCaseChars = $chars; |
|
98 | 11 | } |
|
99 | |||
100 | public function getSpecialCaseChars() |
||
101 | { |
||
102 | return $this->specialCaseChars; |
||
103 | } |
||
104 | |||
105 | public function addSpecialCaseChar($char) |
||
111 | |||
112 | public function removeSpecialCaseChar($char) |
||
113 | { |
||
114 | $key = array_search($char, $this->specialCaseChars); |
||
115 | if ($key !== false) { |
||
116 | unset($this->specialCaseChars[$key]); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | 11 | public function setSpecialCaseTags(array $tags = array()) |
|
121 | { |
||
122 | 11 | $this->specialCaseTags = $tags; |
|
123 | |||
124 | 11 | foreach ($this->specialCaseTags as $tag) { |
|
125 | $this->addSpecialCaseTag($tag); |
||
126 | 11 | } |
|
127 | 11 | } |
|
128 | |||
129 | public function addSpecialCaseTag($tag) |
||
130 | { |
||
131 | if (!in_array($tag, $this->specialCaseTags)) { |
||
132 | $this->specialCaseTags[] = $tag; |
||
133 | } |
||
134 | |||
135 | $opening = $this->getOpeningTag($tag); |
||
136 | $closing = $this->getClosingTag($tag); |
||
137 | |||
138 | if (!in_array($opening, $this->specialCaseOpeningTags)) { |
||
139 | $this->specialCaseOpeningTags[] = $opening; |
||
140 | } |
||
141 | if (!in_array($closing, $this->specialCaseClosingTags)) { |
||
142 | $this->specialCaseClosingTags[] = $closing; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | public function removeSpecialCaseTag($tag) |
||
147 | { |
||
148 | if (($key = array_search($tag, $this->specialCaseTags)) !== false) { |
||
149 | unset($this->specialCaseTags[$key]); |
||
150 | |||
151 | $opening = $this->getOpeningTag($tag); |
||
152 | $closing = $this->getClosingTag($tag); |
||
153 | |||
154 | if (($key = array_search($opening, $this->specialCaseOpeningTags)) !== false) { |
||
155 | unset($this->specialCaseOpeningTags[$key]); |
||
156 | } |
||
157 | if (($key = array_search($closing, $this->specialCaseClosingTags)) !== false) { |
||
158 | unset($this->specialCaseClosingTags[$key]); |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | |||
163 | public function getSpecialCaseTags() |
||
167 | |||
168 | public function getOldHtml() |
||
172 | |||
173 | public function getNewHtml() |
||
174 | { |
||
175 | return $this->newText; |
||
176 | } |
||
177 | |||
178 | public function getDifference() |
||
179 | { |
||
180 | return $this->content; |
||
181 | } |
||
182 | |||
183 | public function setGroupDiffs($boolean) |
||
184 | { |
||
185 | $this->groupDiffs = $boolean; |
||
186 | } |
||
187 | |||
188 | 11 | public function isGroupDiffs() |
|
189 | { |
||
190 | 11 | return $this->groupDiffs; |
|
191 | } |
||
192 | |||
193 | protected function getOpeningTag($tag) |
||
194 | { |
||
195 | return "/<".$tag."[^>]*/i"; |
||
196 | } |
||
197 | |||
198 | protected function getClosingTag($tag) |
||
199 | { |
||
200 | return "</".$tag.">"; |
||
201 | } |
||
202 | |||
203 | protected function getStringBetween($str, $start, $end) |
||
204 | { |
||
205 | $expStr = explode( $start, $str, 2 ); |
||
206 | if ( count( $expStr ) > 1 ) { |
||
207 | $expStr = explode( $end, $expStr[ 1 ] ); |
||
208 | if ( count( $expStr ) > 1 ) { |
||
209 | array_pop( $expStr ); |
||
210 | |||
211 | return implode( $end, $expStr ); |
||
212 | } |
||
213 | } |
||
214 | |||
215 | return ''; |
||
216 | } |
||
217 | |||
218 | 11 | protected function purifyHtml($html, $tags = null) |
|
219 | { |
||
220 | 11 | if ( class_exists( 'Tidy' ) && false ) { |
|
221 | $config = array( 'output-xhtml' => true, 'indent' => false ); |
||
222 | $tidy = new tidy(); |
||
223 | $tidy->parseString( $html, $config, 'utf8' ); |
||
224 | $html = (string) $tidy; |
||
225 | |||
226 | return $this->getStringBetween( $html, '<body>' ); |
||
227 | } |
||
228 | |||
229 | 11 | return $html; |
|
230 | } |
||
231 | |||
232 | 11 | protected function splitInputsToWords() |
|
237 | |||
238 | 11 | protected function isPartOfWord($text) |
|
239 | { |
||
240 | 11 | return ctype_alnum(str_replace($this->specialCaseChars, '', $text)); |
|
241 | } |
||
242 | |||
243 | 11 | protected function convertHtmlToListOfWords($characterString) |
|
244 | { |
||
245 | 11 | $mode = 'character'; |
|
246 | 11 | $current_word = ''; |
|
247 | 11 | $words = array(); |
|
248 | 11 | foreach ($characterString as $i => $character) { |
|
249 | switch ($mode) { |
||
250 | 11 | case 'character': |
|
251 | 11 | if ( $this->isStartOfTag( $character ) ) { |
|
252 | 11 | if ($current_word != '') { |
|
253 | 10 | $words[] = $current_word; |
|
254 | 10 | } |
|
255 | 11 | $current_word = "<"; |
|
256 | 11 | $mode = 'tag'; |
|
257 | 11 | } elseif (preg_match("/\s/", $character)) { |
|
258 | 11 | if ($current_word !== '') { |
|
259 | 11 | $words[] = $current_word; |
|
318 | |||
319 | 11 | protected function isStartOfTag($val) |
|
323 | |||
324 | 11 | protected function isEndOfTag($val) |
|
328 | |||
329 | protected function isWhiteSpace($value) |
||
333 | |||
334 | 11 | protected function explode($value) |
|
339 | } |
||
340 |
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.