Passed
Push — 2.4 ( b81cc1...50bd4d )
by Colin
02:21
created

ListItemParser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 63
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isContainer() 0 3 1
A canContain() 0 14 3
A getBlock() 0 3 1
A __construct() 0 3 1
A tryContinue() 0 25 5
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\Node\Block\Paragraph;
21
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
22
use League\CommonMark\Parser\Block\BlockContinue;
23
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
24
use League\CommonMark\Parser\Cursor;
25
26
final class ListItemParser extends AbstractBlockContinueParser
27
{
28
    /** @psalm-readonly */
29
    private ListItem $block;
30
31
    private bool $hadBlankLine = false;
32
33 212
    public function __construct(ListData $listData)
34
    {
35 212
        $this->block = new ListItem($listData);
36
    }
37
38 212
    public function getBlock(): ListItem
39
    {
40 212
        return $this->block;
41
    }
42
43 200
    public function isContainer(): bool
44
    {
45 200
        return true;
46
    }
47
48 196
    public function canContain(AbstractBlock $childBlock): bool
49
    {
50 196
        if ($this->hadBlankLine) {
51
            // We saw a blank line in this list item, that means the list block is loose.
52
            //
53
            // spec: if any of its constituent list items directly contain two block-level elements with a blank line
54
            // between them
55 58
            $parent = $this->block->parent();
56 58
            if ($parent instanceof ListBlock) {
57 58
                $parent->setTight(false);
58
            }
59
        }
60
61 196
        return true;
62
    }
63
64 172
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
65
    {
66 172
        if ($cursor->isBlank()) {
67 104
            if ($this->block->firstChild() === null) {
68
                // Blank line after empty list item
69 4
                return BlockContinue::none();
70
            }
71
72 100
            $activeBlock = $activeBlockParser->getBlock();
73
            // If the active block is a code block, blank lines in it should not affect if the list is tight.
74 100
            $this->hadBlankLine = $activeBlock instanceof Paragraph || $activeBlock instanceof ListItem;
75 100
            $cursor->advanceToNextNonSpaceOrTab();
76
77 100
            return BlockContinue::at($cursor);
78
        }
79
80 170
        $contentIndent = $this->block->getListData()->markerOffset + $this->getBlock()->getListData()->padding;
81 170
        if ($cursor->getIndent() >= $contentIndent) {
82 96
            $cursor->advanceBy($contentIndent, true);
83
84 96
            return BlockContinue::at($cursor);
85
        }
86
87
        // Note: We'll hit this case for lazy continuation lines, they will get added later.
88 114
        return BlockContinue::none();
89
    }
90
}
91