Complex classes like LingoParser 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 LingoParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class LingoParser { |
||
46 | |||
47 | const WORD_VALUE = 0; |
||
48 | const WORD_OFFSET = 1; |
||
49 | |||
50 | private $mLingoTree = null; |
||
51 | |||
52 | /** |
||
53 | * @var Backend |
||
54 | */ |
||
55 | private $mLingoBackend = null; |
||
56 | private static $parserSingleton = null; |
||
57 | |||
58 | // The RegEx to split a chunk of text into words |
||
59 | 1 | public $regex = null; |
|
60 | |||
61 | /** |
||
62 | 1 | * Lingo\LingoParser constructor. |
|
63 | 1 | * @param MessageLog|null $messages |
|
64 | */ |
||
65 | public function __construct( MessageLog &$messages = null ) { |
||
|
|||
66 | // The RegEx to split a chunk of text into words |
||
67 | // Words are: placeholders for stripped items, sequences of letters and numbers, single characters that are neither letter nor number |
||
68 | $this->regex = '/' . preg_quote( Parser::MARKER_PREFIX, '/' ) . '.*?' . preg_quote( Parser::MARKER_SUFFIX, '/' ) . '|[\p{L}\p{N}]+|[^\p{L}\p{N}]/u'; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * |
||
73 | * @param \AbstractContent $content |
||
74 | * @param \Title $title |
||
75 | * @param \ParserOutput $po |
||
76 | * |
||
77 | * @return Boolean |
||
78 | */ |
||
79 | public function parse( $content, $title, $po ) { |
||
80 | |||
81 | /** @var \Parser $parser */ |
||
82 | $parser = $GLOBALS[ 'wgParser' ]; |
||
83 | |||
84 | if ( $this->shouldParse( $parser ) ) { |
||
85 | $this->realParse( $parser ); |
||
86 | } |
||
87 | |||
88 | return true; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return LingoParser |
||
93 | * @since 2.0.1 |
||
94 | */ |
||
95 | public static function getInstance() { |
||
96 | if ( !self::$parserSingleton ) { |
||
97 | self::$parserSingleton = new LingoParser(); |
||
98 | |||
99 | } |
||
100 | |||
101 | return self::$parserSingleton; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @return string |
||
106 | */ |
||
107 | private function getCacheKey() { |
||
108 | // FIXME: If Lingo ever stores the glossary tree per user, then the cache key also needs to include the user id (see T163608) |
||
109 | return ObjectCache::getLocalClusterInstance()->makeKey( 'ext', 'lingo', 'lingotree', Tree::TREE_VERSION, get_class( self::getInstance()->getBackend() ) ); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @return Backend the backend used by the parser |
||
114 | * @throws \MWException |
||
115 | */ |
||
116 | public function getBackend() { |
||
117 | |||
118 | if ( $this->mLingoBackend === null ) { |
||
119 | throw new \MWException( 'No Lingo backend available!' ); |
||
120 | } |
||
121 | |||
122 | return $this->mLingoBackend; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Returns the list of terms in the glossary |
||
127 | * |
||
128 | * @return array an array mapping terms (keys) to descriptions (values) |
||
129 | */ |
||
130 | public function getLingoArray() { |
||
131 | return $this->getLingoTree()->getTermList(); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Returns the list of terms in the glossary as a Lingo\Tree |
||
136 | * |
||
137 | * @return Tree a Lingo\Tree mapping terms (keys) to descriptions (values) |
||
138 | */ |
||
139 | public function getLingoTree() { |
||
181 | |||
182 | /** |
||
183 | * @return Tree |
||
184 | */ |
||
185 | protected function &buildLingo() { |
||
197 | |||
198 | /** |
||
199 | * Parses the given text and enriches applicable terms |
||
200 | * |
||
201 | * This method currently only recognizes terms consisting of max one word |
||
202 | * |
||
203 | * @param Parser $parser |
||
204 | * |
||
205 | * @return Boolean |
||
206 | */ |
||
207 | protected function realParse( &$parser ) { |
||
340 | |||
341 | /** |
||
342 | * @param Parser $parser |
||
343 | */ |
||
344 | protected function loadModules( &$parser ) { |
||
363 | |||
364 | /** |
||
365 | * Purges the lingo tree from the cache. |
||
366 | * |
||
367 | * @deprecated 2.0.2 |
||
368 | */ |
||
369 | public static function purgeCache() { |
||
373 | |||
374 | /** |
||
375 | * Purges the lingo tree from the cache. |
||
376 | * |
||
377 | * @since 2.0.2 |
||
378 | */ |
||
379 | public function purgeGlossaryFromCache() { |
||
385 | |||
386 | /** |
||
387 | * @since 2.0.1 |
||
388 | * @param Backend $backend |
||
389 | */ |
||
390 | public function setBackend( Backend $backend ) { |
||
394 | |||
395 | /** |
||
396 | * @param Parser $parser |
||
397 | * @return bool |
||
398 | */ |
||
399 | protected function shouldParse( &$parser ) { |
||
424 | } |
||
425 | |||
426 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.