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

ListBlockParser::closeBlock()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 13
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 25
ccs 14
cts 14
cp 1
crap 7
rs 8.8333
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|null
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) {
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) {
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
        $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

81
        $this->block->setEndLine($this->block->lastChild()->/** @scrutinizer ignore-call */ getEndLine());
Loading history...
82
    }
83
84 146
    private static function endsWithBlankLine(AbstractBlock $block): bool
85
    {
86 146
        return $block->next() !== null && $block->getEndLine() !== $block->next()->getStartLine() - 1;
0 ignored issues
show
Bug introduced by
The method getStartLine() 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

86
        return $block->next() !== null && $block->getEndLine() !== $block->next()->/** @scrutinizer ignore-call */ getStartLine() - 1;
Loading history...
87
    }
88
}
89