Passed
Push — commonmark-spec-0.31 ( 6bbcc4 )
by Colin
03:10
created

ListItemParser::closeBlock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
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\ListData;
17
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
18
use League\CommonMark\Node\Block\AbstractBlock;
19
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
20
use League\CommonMark\Parser\Block\BlockContinue;
21
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
22
use League\CommonMark\Parser\Cursor;
23
24
final class ListItemParser extends AbstractBlockContinueParser
25
{
26
    /** @psalm-readonly */
27
    private ListItem $block;
28
29 216
    public function __construct(ListData $listData)
30
    {
31 216
        $this->block = new ListItem($listData);
32
    }
33
34 216
    public function getBlock(): ListItem
35
    {
36 216
        return $this->block;
37
    }
38
39 204
    public function isContainer(): bool
40
    {
41 204
        return true;
42
    }
43
44 200
    public function canContain(AbstractBlock $childBlock): bool
45
    {
46 200
        return ! $childBlock instanceof ListItem;
47
    }
48
49 176
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
50
    {
51 176
        if ($cursor->isBlank()) {
52 108
            if ($this->block->firstChild() === null) {
53
                // Blank line after empty list item
54 4
                return BlockContinue::none();
55
            }
56
57 104
            $cursor->advanceToNextNonSpaceOrTab();
58
59 104
            return BlockContinue::at($cursor);
60
        }
61
62 174
        $contentIndent = $this->block->getListData()->markerOffset + $this->getBlock()->getListData()->padding;
63 174
        if ($cursor->getIndent() >= $contentIndent) {
64 100
            $cursor->advanceBy($contentIndent, true);
65
66 100
            return BlockContinue::at($cursor);
67
        }
68
69
        // Note: We'll hit this case for lazy continuation lines, they will get added later.
70 118
        return BlockContinue::none();
71
    }
72
73 204
    public function closeBlock(): void
74
    {
75 204
        if ($this->block->lastChild() !== null) {
76 200
            $this->block->setEndLine($this->block->lastChild()->getEndLine());
0 ignored issues
show
Bug introduced by
The method getEndLine() does not exist on League\CommonMark\Node\Node. It seems like you code against a sub-type of League\CommonMark\Node\Node such as League\CommonMark\Node\Block\AbstractBlock. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
            $this->block->setEndLine($this->block->lastChild()->/** @scrutinizer ignore-call */ getEndLine());
Loading history...
77
        } else {
78
            // Empty list item
79 12
            $this->block->setEndLine($this->block->getStartLine());
80
        }
81
    }
82
}
83