Passed
Pull Request — 2.4 (#1011)
by Colin
04:46 queued 02:22
created

ListBlockParser   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 67
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A canContain() 0 3 1
A isContainer() 0 3 1
A tryContinue() 0 5 1
A __construct() 0 3 1
A getBlock() 0 3 1
A endsWithBlankLine() 0 5 2
B closeBlock() 0 27 8
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\CommonMark\Parser\Block;
15
16
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
17
use League\CommonMark\Extension\CommonMark\Node\Block\ListData;
18
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
19
use League\CommonMark\Node\Block\AbstractBlock;
20
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
21
use League\CommonMark\Parser\Block\BlockContinue;
22
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
23
use League\CommonMark\Parser\Cursor;
24
25
final class ListBlockParser extends AbstractBlockContinueParser
26
{
27
    /** @psalm-readonly */
28
    private ListBlock $block;
29
30 216
    public function __construct(ListData $listData)
31
    {
32 216
        $this->block = new ListBlock($listData);
33
    }
34
35 216
    public function getBlock(): ListBlock
36
    {
37 216
        return $this->block;
38
    }
39
40 204
    public function isContainer(): bool
41
    {
42 204
        return true;
43
    }
44
45 204
    public function canContain(AbstractBlock $childBlock): bool
46
    {
47 204
        return $childBlock instanceof ListItem;
48
    }
49
50 176
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
51
    {
52
        // List blocks themselves don't have any markers, only list items. So try to stay in the list.
53
        // If there is a block start other than list item, canContain makes sure that this list is closed.
54 176
        return BlockContinue::at($cursor);
55
    }
56
57 204
    public function closeBlock(): void
58
    {
59 204
        $item = $this->block->firstChild();
60 204
        while ($item instanceof AbstractBlock) {
61
            // check for non-final list item ending with blank line:
62 204
            if ($item->next() !== null && self::endsWithBlankLine($item)) {
63 20
                $this->block->setTight(false);
64 20
                break;
65
            }
66
67
            // recurse into children of list item, to see if there are spaces between any of them
68 196
            $subitem = $item->firstChild();
69 196
            while ($subitem instanceof AbstractBlock) {
70 192
                if ($subitem->next() && self::endsWithBlankLine($subitem)) {
71 58
                    $this->block->setTight(false);
72 58
                    break 2;
73
                }
74
75 150
                $subitem = $subitem->next();
76
            }
77
78 154
            $item = $item->next();
79
        }
80
81 204
        $lastChild = $this->block->lastChild();
82 204
        if ($lastChild instanceof AbstractBlock) {
83 204
            $this->block->setEndLine($lastChild->getEndLine());
84
        }
85
    }
86
87 146
    private static function endsWithBlankLine(AbstractBlock $block): bool
88
    {
89 146
        $next = $block->next();
90
91 146
        return $next instanceof AbstractBlock && $block->getEndLine() !== $next->getStartLine() - 1;
92
    }
93
}
94