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

ListBlockParser::closeBlock()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 15
nc 6
nop 0
dl 0
loc 27
ccs 16
cts 16
cp 1
crap 8
rs 8.4444
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ListBlockParser::tryContinue() 0 12 3
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
    private bool $hadBlankLine = false;
31
32
    private int $linesAfterBlank = 0;
33
34 212
    public function __construct(ListData $listData)
35
    {
36 212
        $this->block = new ListBlock($listData);
37
    }
38
39 212
    public function getBlock(): ListBlock
40
    {
41 212
        return $this->block;
42
    }
43
44 200
    public function isContainer(): bool
45
    {
46 200
        return true;
47
    }
48
49 200
    public function canContain(AbstractBlock $childBlock): bool
50
    {
51 200
        if (! $childBlock instanceof ListItem) {
52 56
            return false;
53
        }
54
55
        // Another list item is being added to this list block.
56
        // If the previous line was blank, that means this list
57
        // block is "loose" (not tight).
58 200
        if ($this->hadBlankLine && $this->linesAfterBlank === 1) {
59 18
            $this->block->setTight(false);
60 18
            $this->hadBlankLine = false;
61
        }
62
63 200
        return true;
64
    }
65
66 172
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
67
    {
68 172
        if ($cursor->isBlank()) {
69 104
            $this->hadBlankLine    = true;
70 104
            $this->linesAfterBlank = 0;
71 172
        } elseif ($this->hadBlankLine) {
72 104
            $this->linesAfterBlank++;
73
        }
74
75
        // List blocks themselves don't have any markers, only list items. So try to stay in the list.
76
        // If there is a block start other than list item, canContain makes sure that this list is closed.
77 172
        return BlockContinue::at($cursor);
78
    }
79
}
80