Skip to content

Commit e1cfa8d

Browse files
committed
Use recursive lookup as a last resort to avoid throwing
1 parent b61bbd4 commit e1cfa8d

7 files changed

+2
-32
lines changed

src/Delimiter/DelimiterStack.php

+1-14
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
use League\CommonMark\Delimiter\Processor\CacheableDelimiterProcessorInterface;
2323
use League\CommonMark\Delimiter\Processor\DelimiterProcessorCollection;
24-
use League\CommonMark\Exception\LogicException;
2524
use League\CommonMark\Node\Inline\AdjacentTextMerger;
2625
use League\CommonMark\Node\Node;
2726

@@ -81,9 +80,6 @@ public function getLastBracket(): ?Bracket
8180
return $this->brackets;
8281
}
8382

84-
/**
85-
* @throws LogicException
86-
*/
8783
private function findEarliest(int $stackBottom): ?DelimiterInterface
8884
{
8985
// Move back to first relevant delim.
@@ -149,9 +145,6 @@ private function removeDelimiterAndNode(DelimiterInterface $delimiter): void
149145
$this->removeDelimiter($delimiter);
150146
}
151147

152-
/**
153-
* @throws LogicException
154-
*/
155148
private function removeDelimitersBetween(DelimiterInterface $opener, DelimiterInterface $closer): void
156149
{
157150
$delimiter = $closer->getPrevious();
@@ -165,8 +158,6 @@ private function removeDelimitersBetween(DelimiterInterface $opener, DelimiterIn
165158

166159
/**
167160
* @param DelimiterInterface|int|null $stackBottom
168-
*
169-
* @throws LogicException if the index/position cannot be determined for some delimiter
170161
*/
171162
public function removeAll($stackBottom = null): void
172163
{
@@ -230,8 +221,6 @@ public function searchByCharacter($characters): ?DelimiterInterface
230221
/**
231222
* @param DelimiterInterface|int|null $stackBottom
232223
*
233-
* @throws LogicException if the index/position cannot be determined for any delimiter
234-
*
235224
* @todo change $stackBottom to an int in 3.0
236225
*/
237226
public function processDelimiters($stackBottom, DelimiterProcessorCollection $processors): void
@@ -350,8 +339,6 @@ public function __destruct()
350339

351340
/**
352341
* @deprecated This method will be dropped in 3.0 once all delimiters MUST have an index/position
353-
*
354-
* @throws LogicException if no index was defined on this delimiter, and no reasonable guess could be made based on its neighbors
355342
*/
356343
private function getIndex(?DelimiterInterface $delimiter): int
357344
{
@@ -395,6 +382,6 @@ private function getIndex(?DelimiterInterface $delimiter): int
395382
} while ($next = $next->getNext());
396383

397384
// No index was defined on this delimiter, and none could be guesstimated based on the stack.
398-
throw new LogicException('No index was defined on this delimiter, and none could be guessed based on the stack. Ensure you are passing the index when instantiating the Delimiter.');
385+
return $this->missingIndexCache[$delimiter] = $this->getIndex($delimiter->getPrevious()) + 1;
399386
}
400387
}

src/Extension/Table/TableParser.php

-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
namespace League\CommonMark\Extension\Table;
1717

18-
use League\CommonMark\Exception\LogicException;
1918
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
2019
use League\CommonMark\Parser\Block\BlockContinue;
2120
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
@@ -151,9 +150,6 @@ public function parseInlines(InlineParserEngineInterface $inlineParser): void
151150
}
152151
}
153152

154-
/**
155-
* @throws LogicException
156-
*/
157153
private function parseCell(string $cell, int $column, InlineParserEngineInterface $inlineParser): TableCell
158154
{
159155
$tableCell = new TableCell(TableCell::TYPE_DATA, $this->columns[$column] ?? null);

src/Parser/Block/BlockContinueParserWithInlinesInterface.php

-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313

1414
namespace League\CommonMark\Parser\Block;
1515

16-
use League\CommonMark\Exception\LogicException;
1716
use League\CommonMark\Parser\InlineParserEngineInterface;
1817

1918
interface BlockContinueParserWithInlinesInterface extends BlockContinueParserInterface
2019
{
2120
/**
2221
* Parse any inlines inside of the current block
23-
*
24-
* @throws LogicException
2522
*/
2623
public function parseInlines(InlineParserEngineInterface $inlineParser): void;
2724
}

src/Parser/Inline/InlineParserInterface.php

-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@
1313

1414
namespace League\CommonMark\Parser\Inline;
1515

16-
use League\CommonMark\Exception\LogicException;
1716
use League\CommonMark\Parser\InlineParserContext;
1817

1918
interface InlineParserInterface
2019
{
2120
public function getMatchDefinition(): InlineParserMatch;
2221

23-
/**
24-
* @throws LogicException
25-
*/
2622
public function parse(InlineParserContext $inlineContext): bool;
2723
}

src/Parser/InlineParserEngineInterface.php

-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace League\CommonMark\Parser;
1515

16-
use League\CommonMark\Exception\LogicException;
1716
use League\CommonMark\Node\Block\AbstractBlock;
1817

1918
/**
@@ -23,8 +22,6 @@ interface InlineParserEngineInterface
2322
{
2423
/**
2524
* Parse the given contents as inlines and insert them into the given block
26-
*
27-
* @throws LogicException
2825
*/
2926
public function parse(string $contents, AbstractBlock $block): void;
3027
}

src/Parser/MarkdownParser.php

-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use League\CommonMark\Event\DocumentParsedEvent;
2424
use League\CommonMark\Event\DocumentPreParsedEvent;
2525
use League\CommonMark\Exception\CommonMarkException;
26-
use League\CommonMark\Exception\LogicException;
2726
use League\CommonMark\Input\MarkdownInput;
2827
use League\CommonMark\Node\Block\Document;
2928
use League\CommonMark\Node\Block\Paragraph;
@@ -267,8 +266,6 @@ private function finalize(BlockContinueParserInterface $blockParser, int $endLin
267266

268267
/**
269268
* Walk through a block & children recursively, parsing string content into inline content where appropriate.
270-
*
271-
* @throws LogicException
272269
*/
273270
private function processInlines(int $inputSize): void
274271
{

tests/unit/Delimiter/DelimiterStackTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public function testGetIndex(): void
345345
$method = $reflection->getMethod('getIndex');
346346
$method->setAccessible(true);
347347

348-
$this->assertSame(0, $method->invoke($stack, $delim1));
348+
$this->assertSame(9, $method->invoke($stack, $delim1));
349349
$this->assertSame(10, $method->invoke($stack, $delim2));
350350
$this->assertSame(20, $method->invoke($stack, $delim3));
351351
$this->assertSame(21, $method->invoke($stack, $delim4));

0 commit comments

Comments
 (0)