Conditions | 45 |
Paths | 10380 |
Total Lines | 204 |
Code Lines | 122 |
Lines | 23 |
Ratio | 11.27 % |
Changes | 0 |
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 |
||
176 | private function execute() { |
||
177 | $text = $this->text; |
||
178 | # Parsing through the text line by line. The main thing |
||
179 | # happening here is handling of block-level elements p, pre, |
||
180 | # and making lists from lines starting with * # : etc. |
||
181 | $textLines = StringUtils::explode( "\n", $text ); |
||
182 | |||
183 | $lastPrefix = $output = ''; |
||
184 | $this->DTopen = $inBlockElem = false; |
||
185 | $prefixLength = 0; |
||
186 | $pendingPTag = false; |
||
187 | $inBlockquote = false; |
||
188 | |||
189 | foreach ( $textLines as $inputLine ) { |
||
190 | # Fix up $lineStart |
||
191 | if ( !$this->lineStart ) { |
||
192 | $output .= $inputLine; |
||
193 | $this->lineStart = true; |
||
194 | continue; |
||
195 | } |
||
196 | # * = ul |
||
197 | # # = ol |
||
198 | # ; = dt |
||
199 | # : = dd |
||
200 | |||
201 | $lastPrefixLength = strlen( $lastPrefix ); |
||
202 | $preCloseMatch = preg_match( '/<\\/pre/i', $inputLine ); |
||
203 | $preOpenMatch = preg_match( '/<pre/i', $inputLine ); |
||
204 | # If not in a <pre> element, scan for and figure out what prefixes are there. |
||
205 | if ( !$this->inPre ) { |
||
206 | # Multiple prefixes may abut each other for nested lists. |
||
207 | $prefixLength = strspn( $inputLine, '*#:;' ); |
||
208 | $prefix = substr( $inputLine, 0, $prefixLength ); |
||
209 | |||
210 | # eh? |
||
211 | # ; and : are both from definition-lists, so they're equivalent |
||
212 | # for the purposes of determining whether or not we need to open/close |
||
213 | # elements. |
||
214 | $prefix2 = str_replace( ';', ':', $prefix ); |
||
215 | $t = substr( $inputLine, $prefixLength ); |
||
216 | $this->inPre = (bool)$preOpenMatch; |
||
217 | } else { |
||
218 | # Don't interpret any other prefixes in preformatted text |
||
219 | $prefixLength = 0; |
||
220 | $prefix = $prefix2 = ''; |
||
221 | $t = $inputLine; |
||
222 | } |
||
223 | |||
224 | # List generation |
||
225 | if ( $prefixLength && $lastPrefix === $prefix2 ) { |
||
226 | # Same as the last item, so no need to deal with nesting or opening stuff |
||
227 | $output .= $this->nextItem( substr( $prefix, -1 ) ); |
||
228 | $pendingPTag = false; |
||
229 | |||
230 | if ( substr( $prefix, -1 ) === ';' ) { |
||
231 | # The one nasty exception: definition lists work like this: |
||
232 | # ; title : definition text |
||
233 | # So we check for : in the remainder text to split up the |
||
234 | # title and definition, without b0rking links. |
||
235 | $term = $t2 = ''; |
||
236 | View Code Duplication | if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) { |
|
237 | $t = $t2; |
||
238 | $output .= $term . $this->nextItem( ':' ); |
||
239 | } |
||
240 | } |
||
241 | } elseif ( $prefixLength || $lastPrefixLength ) { |
||
242 | # We need to open or close prefixes, or both. |
||
243 | |||
244 | # Either open or close a level... |
||
245 | $commonPrefixLength = $this->getCommon( $prefix, $lastPrefix ); |
||
246 | $pendingPTag = false; |
||
247 | |||
248 | # Close all the prefixes which aren't shared. |
||
249 | while ( $commonPrefixLength < $lastPrefixLength ) { |
||
250 | $output .= $this->closeList( $lastPrefix[$lastPrefixLength - 1] ); |
||
251 | --$lastPrefixLength; |
||
252 | } |
||
253 | |||
254 | # Continue the current prefix if appropriate. |
||
255 | if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) { |
||
256 | $output .= $this->nextItem( $prefix[$commonPrefixLength - 1] ); |
||
257 | } |
||
258 | |||
259 | # Open prefixes where appropriate. |
||
260 | if ( $lastPrefix && $prefixLength > $commonPrefixLength ) { |
||
261 | $output .= "\n"; |
||
262 | } |
||
263 | while ( $prefixLength > $commonPrefixLength ) { |
||
264 | $char = substr( $prefix, $commonPrefixLength, 1 ); |
||
265 | $output .= $this->openList( $char ); |
||
266 | |||
267 | View Code Duplication | if ( ';' === $char ) { |
|
268 | # @todo FIXME: This is dupe of code above |
||
269 | if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) { |
||
270 | $t = $t2; |
||
271 | $output .= $term . $this->nextItem( ':' ); |
||
272 | } |
||
273 | } |
||
274 | ++$commonPrefixLength; |
||
275 | } |
||
276 | if ( !$prefixLength && $lastPrefix ) { |
||
277 | $output .= "\n"; |
||
278 | } |
||
279 | $lastPrefix = $prefix2; |
||
280 | } |
||
281 | |||
282 | # If we have no prefixes, go to paragraph mode. |
||
283 | if ( 0 == $prefixLength ) { |
||
284 | # No prefix (not in list)--go to paragraph mode |
||
285 | # @todo consider using a stack for nestable elements like span, table and div |
||
286 | $openMatch = preg_match( |
||
287 | '/(?:<table|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|' |
||
288 | . '<p|<ul|<ol|<dl|<li|<\\/tr|<\\/td|<\\/th)/iS', |
||
289 | $t |
||
290 | ); |
||
291 | $closeMatch = preg_match( |
||
292 | '/(?:<\\/table|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|' |
||
293 | . '<td|<th|<\\/?blockquote|<\\/?div|<hr|<\\/pre|<\\/p|<\\/mw:|' |
||
294 | . Parser::MARKER_PREFIX |
||
295 | . '-pre|<\\/li|<\\/ul|<\\/ol|<\\/dl|<\\/?center)/iS', |
||
296 | $t |
||
297 | ); |
||
298 | |||
299 | if ( $openMatch || $closeMatch ) { |
||
300 | $pendingPTag = false; |
||
301 | # @todo bug 5718: paragraph closed |
||
302 | $output .= $this->closeParagraph(); |
||
303 | if ( $preOpenMatch && !$preCloseMatch ) { |
||
304 | $this->inPre = true; |
||
305 | } |
||
306 | $bqOffset = 0; |
||
307 | while ( preg_match( '/<(\\/?)blockquote[\s>]/i', $t, |
||
308 | $bqMatch, PREG_OFFSET_CAPTURE, $bqOffset ) |
||
309 | ) { |
||
310 | $inBlockquote = !$bqMatch[1][0]; // is this a close tag? |
||
311 | $bqOffset = $bqMatch[0][1] + strlen( $bqMatch[0][0] ); |
||
312 | } |
||
313 | $inBlockElem = !$closeMatch; |
||
314 | } elseif ( !$inBlockElem && !$this->inPre ) { |
||
315 | if ( ' ' == substr( $t, 0, 1 ) |
||
316 | && ( $this->lastSection === 'pre' || trim( $t ) != '' ) |
||
317 | && !$inBlockquote |
||
318 | ) { |
||
319 | # pre |
||
320 | View Code Duplication | if ( $this->lastSection !== 'pre' ) { |
|
321 | $pendingPTag = false; |
||
322 | $output .= $this->closeParagraph() . '<pre>'; |
||
323 | $this->lastSection = 'pre'; |
||
324 | } |
||
325 | $t = substr( $t, 1 ); |
||
326 | } else { |
||
327 | # paragraph |
||
328 | if ( trim( $t ) === '' ) { |
||
329 | if ( $pendingPTag ) { |
||
330 | $output .= $pendingPTag . '<br />'; |
||
331 | $pendingPTag = false; |
||
332 | $this->lastSection = 'p'; |
||
333 | } else { |
||
334 | View Code Duplication | if ( $this->lastSection !== 'p' ) { |
|
335 | $output .= $this->closeParagraph(); |
||
336 | $this->lastSection = ''; |
||
337 | $pendingPTag = '<p>'; |
||
338 | } else { |
||
339 | $pendingPTag = '</p><p>'; |
||
340 | } |
||
341 | } |
||
342 | } else { |
||
343 | if ( $pendingPTag ) { |
||
344 | $output .= $pendingPTag; |
||
345 | $pendingPTag = false; |
||
346 | $this->lastSection = 'p'; |
||
347 | } elseif ( $this->lastSection !== 'p' ) { |
||
348 | $output .= $this->closeParagraph() . '<p>'; |
||
349 | $this->lastSection = 'p'; |
||
350 | } |
||
351 | } |
||
352 | } |
||
353 | } |
||
354 | } |
||
355 | # somewhere above we forget to get out of pre block (bug 785) |
||
356 | if ( $preCloseMatch && $this->inPre ) { |
||
357 | $this->inPre = false; |
||
358 | } |
||
359 | if ( $pendingPTag === false ) { |
||
360 | $output .= $t; |
||
361 | if ( $prefixLength === 0 ) { |
||
362 | $output .= "\n"; |
||
363 | } |
||
364 | } |
||
365 | } |
||
366 | while ( $prefixLength ) { |
||
367 | $output .= $this->closeList( $prefix2[$prefixLength - 1] ); |
||
368 | --$prefixLength; |
||
369 | if ( !$prefixLength ) { |
||
370 | $output .= "\n"; |
||
371 | } |
||
372 | } |
||
373 | if ( $this->lastSection !== '' ) { |
||
374 | $output .= '</' . $this->lastSection . '>'; |
||
375 | $this->lastSection = ''; |
||
376 | } |
||
377 | |||
378 | return $output; |
||
379 | } |
||
380 | |||
536 |