Conditions | 17 |
Paths | 16 |
Total Lines | 121 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 306 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
177 | protected function realParse( &$parser, &$text ) { |
||
178 | global $wgRequest; |
||
179 | |||
180 | $action = $wgRequest->getVal( 'action', 'view' ); |
||
181 | |||
182 | if ( $text === null || |
||
183 | $text === '' || |
||
184 | $action === 'edit' || |
||
185 | $action === 'ajax' || |
||
186 | isset( $_POST[ 'wpPreview' ] ) |
||
187 | ) { |
||
188 | |||
189 | return true; |
||
190 | } |
||
191 | |||
192 | // Get array of terms |
||
193 | $glossary = $this->getLingoTree(); |
||
194 | |||
195 | if ( $glossary == null ) { |
||
196 | return true; |
||
197 | } |
||
198 | |||
199 | // Parse HTML from page |
||
200 | wfSuppressWarnings(); |
||
201 | |||
202 | $doc = new DOMDocument( '1.0', 'utf-8' ); |
||
203 | $doc->loadHTML( '<html><head><meta http-equiv="content-type" content="charset=utf-8"/></head><body>' . $text . '</body></html>' ); |
||
204 | |||
205 | wfRestoreWarnings(); |
||
206 | |||
207 | // Find all text in HTML. |
||
208 | $xpath = new DOMXpath( $doc ); |
||
209 | $elements = $xpath->query( |
||
210 | "//*[not(ancestor-or-self::*[@class='noglossary'] or ancestor-or-self::a)][text()!=' ']/text()" |
||
211 | ); |
||
212 | |||
213 | // Iterate all HTML text matches |
||
214 | $nb = $elements->length; |
||
215 | $changedDoc = false; |
||
216 | |||
217 | for ( $pos = 0; $pos < $nb; $pos++ ) { |
||
218 | $el = $elements->item( $pos ); |
||
219 | |||
220 | if ( strlen( $el->nodeValue ) < $glossary->getMinTermLength() ) { |
||
221 | continue; |
||
222 | } |
||
223 | |||
224 | $matches = array(); |
||
225 | preg_match_all( |
||
226 | self::$regex, |
||
227 | $el->nodeValue, |
||
228 | $matches, |
||
229 | PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER |
||
230 | ); |
||
231 | |||
232 | if ( count( $matches ) == 0 || count( $matches[ 0 ] ) == 0 ) { |
||
233 | continue; |
||
234 | } |
||
235 | |||
236 | $lexemes = &$matches[ 0 ]; |
||
237 | $countLexemes = count( $lexemes ); |
||
238 | $parent = &$el->parentNode; |
||
239 | $index = 0; |
||
240 | $changedElem = false; |
||
241 | |||
242 | while ( $index < $countLexemes ) { |
||
243 | list( $skipped, $used, $definition ) = |
||
244 | $glossary->findNextTerm( $lexemes, $index, $countLexemes ); |
||
245 | |||
246 | if ( $used > 0 ) { // found a term |
||
247 | if ( $skipped > 0 ) { // skipped some text, insert it as is |
||
248 | $parent->insertBefore( |
||
249 | $doc->createTextNode( |
||
250 | substr( $el->nodeValue, |
||
251 | $currLexIndex = $lexemes[ $index ][ 1 ], |
||
252 | $lexemes[ $index + $skipped ][ 1 ] - $currLexIndex ) |
||
253 | ), |
||
254 | $el |
||
255 | ); |
||
256 | } |
||
257 | |||
258 | $parent->insertBefore( $definition->getFullDefinition( $doc ), $el ); |
||
259 | |||
260 | $changedElem = true; |
||
261 | } else { // did not find term, just use the rest of the text |
||
262 | // If we found no term now and no term before, there was no |
||
263 | // term in the whole element. Might as well not change the |
||
264 | // element at all. |
||
265 | // Only change element if found term before |
||
266 | if ( $changedElem ) { |
||
267 | $parent->insertBefore( |
||
268 | $doc->createTextNode( |
||
269 | substr( $el->nodeValue, $lexemes[ $index ][ 1 ] ) |
||
270 | ), |
||
271 | $el |
||
272 | ); |
||
273 | } else { |
||
274 | // In principle superfluous, the loop would run out |
||
275 | // anyway. Might save a bit of time. |
||
276 | break; |
||
277 | } |
||
278 | } |
||
279 | |||
280 | $index += $used + $skipped; |
||
281 | } |
||
282 | |||
283 | if ( $changedElem ) { |
||
284 | $parent->removeChild( $el ); |
||
285 | $changedDoc = true; |
||
286 | } |
||
287 | } |
||
288 | |||
289 | if ( $changedDoc ) { |
||
290 | $this->loadModules( $parser ); |
||
291 | |||
292 | // U - Ungreedy, D - dollar matches only end of string, s - dot matches newlines |
||
293 | $text = preg_replace( '%(^.*<body>)|(</body>.*$)%UDs', '', $doc->saveHTML() ); |
||
294 | } |
||
295 | |||
296 | return true; |
||
297 | } |
||
298 | |||
351 |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state