Complex classes like ParserWrapper 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 ParserWrapper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | final class ParserWrapper |
||
22 | { |
||
23 | /** @var array Disabled tags */ |
||
24 | protected $disabled = array(); |
||
25 | /** @var \BBC\Codes */ |
||
26 | protected $codes; |
||
27 | /** @var \BBC\BBCParser */ |
||
28 | protected $bbc_parser; |
||
29 | /** @var \BBC\SmileyParser */ |
||
30 | protected $smiley_parser; |
||
31 | /** @var \BBC\HtmlParser */ |
||
32 | protected $html_parser; |
||
33 | /** @var \BBC\Autolink */ |
||
34 | protected $autolink_parser; |
||
35 | /** @var bool If smileys are enabled */ |
||
36 | protected $smileys_enabled = true; |
||
37 | /** @var ParserWrapper */ |
||
38 | public static $instance; |
||
39 | |||
40 | /** |
||
41 | * Find and return ParserWrapper instance if it exists, |
||
42 | * or create a new instance |
||
43 | * |
||
44 | * @return ParserWrapper |
||
45 | */ |
||
46 | 9 | public static function instance() |
|
47 | { |
||
48 | 9 | if (self::$instance === null) |
|
49 | 9 | { |
|
50 | 1 | self::$instance = new ParserWrapper; |
|
51 | 1 | } |
|
52 | |||
53 | 9 | return self::$instance; |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * ParserWrapper constructor. |
||
58 | */ |
||
59 | 1 | private function __construct() |
|
63 | |||
64 | /** |
||
65 | * Check if the server load is too high to execute BBC parsing |
||
66 | * |
||
67 | * @return bool If the parser can execute |
||
68 | */ |
||
69 | 6 | protected function checkLoad() |
|
81 | |||
82 | /** |
||
83 | * Is BBC parsing enabled |
||
84 | * |
||
85 | * @return bool |
||
86 | */ |
||
87 | 6 | protected function isEnabled() |
|
93 | |||
94 | /** |
||
95 | * Enable or disable smileys |
||
96 | * |
||
97 | * @param bool|int $toggle |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | 6 | public function enableSmileys($toggle) |
|
102 | { |
||
103 | 6 | $this->smileys_enabled = (bool) $toggle; |
|
104 | 6 | return $this; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Get parsers based on where it will be used |
||
109 | * |
||
110 | * @param string $area Where it is being called from |
||
111 | * @return array |
||
112 | */ |
||
113 | 6 | protected function getParsersByArea($area) |
|
114 | { |
||
115 | $parsers = array( |
||
116 | 6 | 'autolink' => false, |
|
117 | 6 | 'html' => false, |
|
118 | 6 | 'bbc' => false, |
|
119 | 6 | 'smiley' => false, |
|
120 | 6 | ); |
|
121 | |||
122 | // First see if any hooks set a parser. |
||
123 | 6 | foreach ($parsers as $parser_type => &$parser) |
|
124 | { |
||
125 | 6 | call_integration_hook('integrate_' . $area . '_' . $parser_type . '_parser', array(&$parser, $this)); |
|
126 | |||
127 | // If not, use the default one |
||
128 | 6 | if ($parser === false) |
|
129 | 6 | { |
|
130 | 6 | $parser = call_user_func(array($this, 'get' . ucfirst($parser_type) . 'Parser'), $area); |
|
131 | 6 | } |
|
132 | 6 | } |
|
133 | |||
134 | 6 | return $parsers; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Return the current message parsers |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | public function getMessageParser() |
||
143 | { |
||
144 | return $this->getParsersByArea('message'); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Return the current signature parsers |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | public function getSignatureParser() |
||
153 | { |
||
154 | return $this->getParsersByArea('signature'); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Return the news parsers |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | public function getNewsParser() |
||
163 | { |
||
164 | return $this->getParsersByArea('news'); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Parse a string based on where it's being called from |
||
169 | * |
||
170 | * @param string $area Where this is being called from |
||
171 | * @param string $message The message to be parsed |
||
172 | * |
||
173 | * @return string The Parsed message |
||
174 | */ |
||
175 | 6 | protected function parse($area, $message) |
|
176 | { |
||
177 | // If the load average is too high, don't parse the BBC. |
||
178 | 6 | if (!$this->checkLoad()) |
|
179 | 6 | { |
|
180 | return $message; |
||
181 | } |
||
182 | |||
183 | 6 | $parsers = $this->getParsersByArea($area); |
|
184 | 6 | $smileys_enabled = $this->smileys_enabled && $GLOBALS['user_info']['smiley_set'] !== 'none'; |
|
185 | |||
186 | 6 | if (!$this->isEnabled()) |
|
187 | 6 | { |
|
188 | // You need to run the smiley parser to get rid of the markers |
||
189 | return $parsers['smiley'] |
||
|
|||
190 | ->setEnabled($smileys_enabled) |
||
191 | ->parse($message); |
||
192 | } |
||
193 | |||
194 | 6 | $message = $parsers['bbc']->parse($message); |
|
195 | |||
196 | 6 | return $parsers['smiley'] |
|
197 | 6 | ->setEnabled($smileys_enabled) |
|
198 | 6 | ->parse($message); |
|
199 | } |
||
200 | |||
201 | /** |
||
202 | * Parse the BBC and smileys in messages |
||
203 | * |
||
204 | * @param string $message |
||
205 | * @param bool|int $smileys_enabled |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | 4 | public function parseMessage($message, $smileys_enabled) |
|
213 | |||
214 | /** |
||
215 | * Parse the BBC and smileys in signatures |
||
216 | * |
||
217 | * @param string $signature |
||
218 | * @param bool $smileys_enabled |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | 1 | public function parseSignature($signature, $smileys_enabled) |
|
223 | { |
||
224 | 1 | return $this->enableSmileys($smileys_enabled)->parse('signature', $signature); |
|
225 | } |
||
226 | |||
227 | /** |
||
228 | * Parse the BBC and smileys in news items |
||
229 | * |
||
230 | * @param string $news |
||
231 | * |
||
232 | * @return string |
||
233 | */ |
||
234 | public function parseNews($news) |
||
238 | |||
239 | /** |
||
240 | * Parse the BBC and smileys in emails |
||
241 | * |
||
242 | * @param string $email |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | public function parseEmail($email) |
||
250 | |||
251 | /** |
||
252 | * Parse the BBC and smileys in custom profile fields |
||
253 | * |
||
254 | * @param string $field |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | public function parseCustomFields($field) |
||
264 | |||
265 | /** |
||
266 | * Parse the BBC and smileys in poll questions/answers |
||
267 | * |
||
268 | * @param string $poll |
||
269 | * |
||
270 | * @return string |
||
271 | */ |
||
272 | public function parsePoll($poll) |
||
276 | |||
277 | /** |
||
278 | * Parse the BBC and smileys in the registration agreement |
||
279 | * |
||
280 | * @param string $agreement |
||
281 | * |
||
282 | * @return string |
||
283 | */ |
||
284 | public function parseAgreement($agreement) |
||
288 | |||
289 | /** |
||
290 | * Parse the BBC and smileys in personal messages |
||
291 | * |
||
292 | * @param string $pm |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | public function parsePM($pm) |
||
300 | |||
301 | /** |
||
302 | * Parse the BBC and smileys in user submitted reports |
||
303 | * |
||
304 | * @param string $report |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | public function parseReport($report) |
||
312 | |||
313 | /** |
||
314 | * Parse the BBC and smileys in package descriptions |
||
315 | * |
||
316 | * @param string $package |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | public function parsePackage($package) |
||
324 | |||
325 | /** |
||
326 | * Parse the BBC and smileys in user verification controls |
||
327 | * |
||
328 | * @param string $question |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | public function parseVerificationControls($question) |
||
336 | |||
337 | /** |
||
338 | * Parse the BBC and smileys in moderator notices to users |
||
339 | * |
||
340 | * @param string $notice |
||
341 | * |
||
342 | * @return string |
||
343 | */ |
||
344 | public function parseNotice($notice) |
||
348 | |||
349 | /** |
||
350 | * Parse the BBC and smileys in board descriptions |
||
351 | * |
||
352 | * @param string $board |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | 1 | public function parseBoard($board) |
|
357 | { |
||
358 | 1 | return $this->enableSmileys(true)->parse('board', $board); |
|
359 | } |
||
360 | |||
361 | /** |
||
362 | * Set the disabled tags |
||
363 | * |
||
364 | * @param string[] $disabled (usually from $modSettings['disabledBBC']) |
||
365 | * |
||
366 | * @return $this |
||
367 | */ |
||
368 | public function setDisabled(array $disabled) |
||
377 | |||
378 | /** |
||
379 | * Return the bbc code definitions for the parser |
||
380 | * |
||
381 | * @return Codes |
||
382 | */ |
||
383 | 4 | public function getCodes() |
|
394 | |||
395 | /** |
||
396 | * Return an instance of the bbc parser |
||
397 | * |
||
398 | * @return BBCParser |
||
399 | */ |
||
400 | 6 | public function getBBCParser() |
|
409 | |||
410 | /** |
||
411 | * Return an, that's right not and, just an, like a single instance of the autolink parser |
||
412 | * |
||
413 | * @return Autolink |
||
414 | */ |
||
415 | 6 | public function getAutolinkParser() |
|
424 | |||
425 | /** |
||
426 | * Return an, that's right not and, just an, like a single instance of the Smiley parser |
||
427 | * |
||
428 | * @return SmileyParser |
||
429 | */ |
||
430 | 6 | public function getSmileyParser() |
|
447 | |||
448 | /** |
||
449 | * Return an, that's right not and, just an, like a single instance of the HTML parser |
||
450 | * |
||
451 | * @return HtmlParser |
||
452 | */ |
||
453 | 6 | public function getHtmlParser() |
|
462 | } |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.