| Conditions | 11 |
| Paths | 23 |
| Total Lines | 74 |
| Code Lines | 40 |
| 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 |
||
| 47 | public function parse(InlineParserContext $inlineContext): bool |
||
| 48 | { |
||
| 49 | // Look through stack of delimiters for a [ or ! |
||
| 50 | $opener = $inlineContext->getDelimiterStack()->getLastBracket(); |
||
| 51 | if ($opener === null) { |
||
| 52 | return false; |
||
| 53 | } |
||
| 54 | |||
| 55 | if (! $opener->isImage() && ! $opener->isActive()) { |
||
| 56 | // no matched opener; remove from stack |
||
| 57 | $inlineContext->getDelimiterStack()->removeBracket(); |
||
| 58 | |||
| 59 | return false; |
||
| 60 | } |
||
| 61 | |||
| 62 | $cursor = $inlineContext->getCursor(); |
||
| 63 | |||
| 64 | $startPos = $cursor->getPosition(); |
||
| 65 | $previousState = $cursor->saveState(); |
||
| 66 | |||
| 67 | $cursor->advanceBy(1); |
||
| 68 | |||
| 69 | // Check to see if we have a link/image |
||
| 70 | |||
| 71 | // Inline link? |
||
| 72 | if ($result = $this->tryParseInlineLinkAndTitle($cursor)) { |
||
| 73 | $link = $result; |
||
| 74 | } elseif ($link = $this->tryParseReference($cursor, $inlineContext->getReferenceMap(), $opener, $startPos)) { |
||
| 75 | $reference = $link; |
||
| 76 | $link = ['url' => $link->getDestination(), 'title' => $link->getTitle()]; |
||
| 77 | } else { |
||
| 78 | // No match; remove this opener from stack |
||
| 79 | $inlineContext->getDelimiterStack()->removeBracket(); |
||
| 80 | $cursor->restoreState($previousState); |
||
| 81 | |||
| 82 | return false; |
||
| 83 | } |
||
| 84 | |||
| 85 | $inline = $this->createInline($link['url'], $link['title'], $opener->isImage(), $reference ?? null); |
||
| 86 | $opener->getNode()->replaceWith($inline); |
||
| 87 | while (($label = $inline->next()) !== null) { |
||
| 88 | // Is there a Mention or Link contained within this link? |
||
| 89 | // CommonMark does not allow nested links, so we'll restore the original text. |
||
| 90 | if ($label instanceof Mention) { |
||
| 91 | $label->replaceWith($replacement = new Text($label->getPrefix() . $label->getIdentifier())); |
||
| 92 | $inline->appendChild($replacement); |
||
| 93 | } elseif ($label instanceof Link) { |
||
| 94 | foreach ($label->children() as $child) { |
||
| 95 | $label->insertBefore($child); |
||
| 96 | } |
||
| 97 | |||
| 98 | $label->detach(); |
||
| 99 | } else { |
||
| 100 | $inline->appendChild($label); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | // Process delimiters such as emphasis inside link/image |
||
| 105 | $delimiterStack = $inlineContext->getDelimiterStack(); |
||
| 106 | $stackBottom = $opener->getPosition(); |
||
| 107 | $delimiterStack->processDelimiters($stackBottom, $this->environment->getDelimiterProcessors()); |
||
| 108 | $delimiterStack->removeBracket(); |
||
| 109 | $delimiterStack->removeAll($stackBottom); |
||
| 110 | |||
| 111 | // Merge any adjacent Text nodes together |
||
| 112 | AdjacentTextMerger::mergeChildNodes($inline); |
||
| 113 | |||
| 114 | // processEmphasis will remove this and later delimiters. |
||
| 115 | // Now, for a link, we also remove earlier link openers (no links in links) |
||
| 116 | if (! $opener->isImage()) { |
||
| 117 | $inlineContext->getDelimiterStack()->deactivateLinkOpeners(); |
||
| 118 | } |
||
| 119 | |||
| 120 | return true; |
||
| 121 | } |
||
| 215 |