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 |
||
189 | protected function realParse( &$parser, &$text ) { |
||
190 | global $wgRequest; |
||
191 | |||
192 | $action = $wgRequest->getVal( 'action', 'view' ); |
||
193 | |||
194 | if ( $text === null || |
||
195 | $text === '' || |
||
196 | $action === 'edit' || |
||
197 | $action === 'ajax' || |
||
198 | isset( $_POST[ 'wpPreview' ] ) |
||
199 | ) { |
||
200 | |||
201 | return true; |
||
202 | } |
||
203 | |||
204 | // Get array of terms |
||
205 | $glossary = $this->getLingoTree(); |
||
206 | |||
207 | if ( $glossary == null ) { |
||
208 | return true; |
||
209 | } |
||
210 | |||
211 | // Parse HTML from page |
||
212 | wfSuppressWarnings(); |
||
213 | |||
214 | $doc = new DOMDocument( '1.0', 'utf-8' ); |
||
215 | $doc->loadHTML( '<html><head><meta http-equiv="content-type" content="charset=utf-8"/></head><body>' . $text . '</body></html>' ); |
||
216 | |||
217 | wfRestoreWarnings(); |
||
218 | |||
219 | // Find all text in HTML. |
||
220 | $xpath = new DOMXpath( $doc ); |
||
221 | $elements = $xpath->query( |
||
222 | "//*[not(ancestor-or-self::*[@class='noglossary'] or ancestor-or-self::a)][text()!=' ']/text()" |
||
223 | ); |
||
224 | |||
225 | // Iterate all HTML text matches |
||
226 | $nb = $elements->length; |
||
227 | $changedDoc = false; |
||
228 | |||
229 | for ( $pos = 0; $pos < $nb; $pos++ ) { |
||
230 | $el = $elements->item( $pos ); |
||
231 | |||
232 | if ( strlen( $el->nodeValue ) < $glossary->getMinTermLength() ) { |
||
233 | continue; |
||
234 | } |
||
235 | |||
236 | $matches = array(); |
||
237 | preg_match_all( |
||
238 | self::$regex, |
||
239 | $el->nodeValue, |
||
240 | $matches, |
||
241 | PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER |
||
242 | ); |
||
243 | |||
244 | if ( count( $matches ) == 0 || count( $matches[ 0 ] ) == 0 ) { |
||
245 | continue; |
||
246 | } |
||
247 | |||
248 | $lexemes = &$matches[ 0 ]; |
||
249 | $countLexemes = count( $lexemes ); |
||
250 | $parent = &$el->parentNode; |
||
251 | $index = 0; |
||
252 | $changedElem = false; |
||
253 | |||
254 | while ( $index < $countLexemes ) { |
||
255 | list( $skipped, $used, $definition ) = |
||
256 | $glossary->findNextTerm( $lexemes, $index, $countLexemes ); |
||
257 | |||
258 | if ( $used > 0 ) { // found a term |
||
259 | if ( $skipped > 0 ) { // skipped some text, insert it as is |
||
260 | $parent->insertBefore( |
||
261 | $doc->createTextNode( |
||
262 | substr( $el->nodeValue, |
||
263 | $currLexIndex = $lexemes[ $index ][ 1 ], |
||
264 | $lexemes[ $index + $skipped ][ 1 ] - $currLexIndex ) |
||
265 | ), |
||
266 | $el |
||
267 | ); |
||
268 | } |
||
269 | |||
270 | $parent->insertBefore( $definition->getFullDefinition( $doc ), $el ); |
||
271 | |||
272 | $changedElem = true; |
||
273 | } else { // did not find term, just use the rest of the text |
||
274 | // If we found no term now and no term before, there was no |
||
275 | // term in the whole element. Might as well not change the |
||
276 | // element at all. |
||
277 | // Only change element if found term before |
||
278 | if ( $changedElem ) { |
||
279 | $parent->insertBefore( |
||
280 | $doc->createTextNode( |
||
281 | substr( $el->nodeValue, $lexemes[ $index ][ 1 ] ) |
||
282 | ), |
||
283 | $el |
||
284 | ); |
||
285 | } else { |
||
286 | // In principle superfluous, the loop would run out |
||
287 | // anyway. Might save a bit of time. |
||
288 | break; |
||
289 | } |
||
290 | } |
||
291 | |||
292 | $index += $used + $skipped; |
||
293 | } |
||
294 | |||
295 | if ( $changedElem ) { |
||
296 | $parent->removeChild( $el ); |
||
297 | $changedDoc = true; |
||
298 | } |
||
299 | } |
||
300 | |||
301 | if ( $changedDoc ) { |
||
302 | $this->loadModules( $parser ); |
||
303 | |||
304 | // U - Ungreedy, D - dollar matches only end of string, s - dot matches newlines |
||
305 | $text = preg_replace( '%(^.*<body>)|(</body>.*$)%UDs', '', $doc->saveHTML() ); |
||
306 | } |
||
307 | |||
308 | return true; |
||
309 | } |
||
310 | |||
363 |
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