| Conditions | 8 |
| Paths | 28 |
| Total Lines | 53 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 57 | public function parse(string $contents, AbstractBlock $block): void |
||
| 58 | { |
||
| 59 | $contents = \trim($contents); |
||
| 60 | $cursor = new Cursor($contents); |
||
| 61 | |||
| 62 | $inlineParserContext = new InlineParserContext($cursor, $block, $this->referenceMap, $this->environment->getConfiguration()->get('max_delimiters_per_line')); |
||
| 63 | |||
| 64 | // Have all parsers look at the line to determine what they might want to parse and what positions they exist at |
||
| 65 | foreach ($this->matchParsers($contents) as $matchPosition => $parsers) { |
||
| 66 | $currentPosition = $cursor->getPosition(); |
||
| 67 | // We've already gone past this point |
||
| 68 | if ($currentPosition > $matchPosition) { |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | |||
| 72 | // We've skipped over some uninteresting text that should be added as a plain text node |
||
| 73 | if ($currentPosition < $matchPosition) { |
||
| 74 | $cursor->advanceBy($matchPosition - $currentPosition); |
||
| 75 | $this->addPlainText($cursor->getPreviousText(), $block); |
||
| 76 | } |
||
| 77 | |||
| 78 | // We're now at a potential start - see which of the current parsers can handle it |
||
| 79 | $parsed = false; |
||
| 80 | foreach ($parsers as [$parser, $matches]) { |
||
| 81 | \assert($parser instanceof InlineParserInterface); |
||
| 82 | if ($parser->parse($inlineParserContext->withMatches($matches))) { |
||
| 83 | // A parser has successfully handled the text at the given position; don't consider any others at this position |
||
| 84 | $parsed = true; |
||
| 85 | break; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | if ($parsed) { |
||
| 90 | continue; |
||
| 91 | } |
||
| 92 | |||
| 93 | // Despite potentially being interested, nothing actually parsed text here, so add the current character and continue onwards |
||
| 94 | $this->addPlainText((string) $cursor->getCurrentCharacter(), $block); |
||
| 95 | $cursor->advance(); |
||
| 96 | } |
||
| 97 | |||
| 98 | // Add any remaining text that wasn't parsed |
||
| 99 | if (! $cursor->isAtEnd()) { |
||
| 100 | $this->addPlainText($cursor->getRemainder(), $block); |
||
| 101 | } |
||
| 102 | |||
| 103 | // Process any delimiters that were found |
||
| 104 | $delimiterStack = $inlineParserContext->getDelimiterStack(); |
||
| 105 | $delimiterStack->processDelimiters(null, $this->environment->getDelimiterProcessors()); |
||
| 106 | $delimiterStack->removeAll(); |
||
| 107 | |||
| 108 | // Combine adjacent text notes into one |
||
| 109 | AdjacentTextMerger::mergeChildNodes($block); |
||
| 110 | } |
||
| 178 |