Completed
Push — master ( 0b0b24...54ff37 )
by Colin
02:37
created

ListBlock   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 0
loc 96
ccs 33
cts 36
cp 0.9167
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getListData() 0 4 1
A endsWithBlankLine() 0 12 4
A canContain() 0 4 1
A isCode() 0 4 1
A matchesNextLine() 0 4 1
B finalize() 0 27 10
A isTight() 0 4 1
A setTight() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Extension\CommonMark\Node\Block;
16
17
use League\CommonMark\Node\Block\AbstractBlock;
18
use League\CommonMark\Parser\ContextInterface;
19
use League\CommonMark\Parser\Cursor;
20
21
/**
22
 * @method children() AbstractBlock[]
23
 */
24
class ListBlock extends AbstractBlock
25
{
26
    const TYPE_BULLET = 'bullet';
27
    const TYPE_ORDERED = 'ordered';
28
29
    /**
30
     * @var bool
31
     */
32
    protected $tight = false;
33
34
    /**
35
     * @var ListData
36
     */
37
    protected $listData;
38
39 339
    public function __construct(ListData $listData)
40
    {
41 339
        $this->listData = $listData;
42 339
    }
43
44
    /**
45
     * @return ListData
46
     */
47 315
    public function getListData(): ListData
48
    {
49 315
        return $this->listData;
50
    }
51
52 150
    public function endsWithBlankLine(): bool
53
    {
54 150
        if ($this->lastLineBlank) {
55
            return true;
56
        }
57
58 150
        if ($this->hasChildren()) {
59 150
            return $this->lastChild() instanceof AbstractBlock && $this->lastChild()->endsWithBlankLine();
60
        }
61
62
        return false;
63
    }
64
65 282
    public function canContain(AbstractBlock $block): bool
66
    {
67 282
        return $block instanceof ListItem;
68
    }
69
70 141
    public function isCode(): bool
71
    {
72 141
        return false;
73
    }
74
75 222
    public function matchesNextLine(Cursor $cursor): bool
76
    {
77 222
        return true;
78
    }
79
80 264
    public function finalize(ContextInterface $context, int $endLineNumber): void
81
    {
82 264
        parent::finalize($context, $endLineNumber);
83
84 264
        $this->tight = true; // tight by default
85
86 264
        foreach ($this->children() as $item) {
87 264
            if (!($item instanceof AbstractBlock)) {
88
                continue;
89
            }
90
91
            // check for non-final list item ending with blank line:
92 264
            if ($item->endsWithBlankLine() && $item !== $this->lastChild()) {
93 18
                $this->tight = false;
94 18
                break;
95
            }
96
97
            // Recurse into children of list item, to see if there are
98
            // spaces between any of them:
99 252
            foreach ($item->children() as $subItem) {
100 246
                if ($subItem instanceof AbstractBlock && $subItem->endsWithBlankLine() && ($item !== $this->lastChild() || $subItem !== $item->lastChild())) {
101 84
                    $this->tight = false;
102 84
                    break;
103
                }
104
            }
105
        }
106 264
    }
107
108 315
    public function isTight(): bool
109
    {
110 315
        return $this->tight;
111
    }
112
113 3
    public function setTight(bool $tight): self
114
    {
115 3
        $this->tight = $tight;
116
117 3
        return $this;
118
    }
119
}
120