Completed
Push — master ( da0d4f...97e8b3 )
by Colin
03:00
created

ListBlock::isTight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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\Block\Element;
16
17
use League\CommonMark\ContextInterface;
18
use League\CommonMark\Cursor;
19
20
class ListBlock extends AbstractBlock
21
{
22
    const TYPE_UNORDERED = 'Bullet';
23
    const TYPE_ORDERED = 'Ordered';
24
25
    /**
26
     * @var bool
27
     */
28
    protected $tight = false;
29
30
    /**
31
     * @var ListData
32
     */
33
    protected $listData;
34
35 270
    public function __construct(ListData $listData)
36
    {
37 270
        $this->listData = $listData;
38 270
    }
39
40
    /**
41
     * @return ListData
42
     */
43 267
    public function getListData(): ListData
44
    {
45 267
        return $this->listData;
46
    }
47
48
    /**
49
     * @return bool
50
     */
51 30
    public function endsWithBlankLine(): bool
52
    {
53 30
        if ($this->lastLineBlank) {
54 3
            return true;
55
        }
56
57 30
        if ($this->hasChildren()) {
58 30
            return $this->lastChild() instanceof AbstractBlock && $this->lastChild()->endsWithBlankLine();
59
        }
60
61
        return false;
62
    }
63
64
    /**
65
     * Returns true if this block can contain the given block as a child node
66
     *
67
     * @param AbstractBlock $block
68
     *
69
     * @return bool
70
     */
71 246
    public function canContain(AbstractBlock $block): bool
72
    {
73 246
        return $block instanceof ListItem;
74
    }
75
76
    /**
77
     * Whether this is a code block
78
     *
79
     * @return bool
80
     */
81 129
    public function isCode(): bool
82
    {
83 129
        return false;
84
    }
85
86 210
    public function matchesNextLine(Cursor $cursor): bool
87
    {
88 210
        return true;
89
    }
90
91 246
    public function finalize(ContextInterface $context, int $endLineNumber)
92
    {
93 246
        parent::finalize($context, $endLineNumber);
94
95 246
        $this->tight = true; // tight by default
96
97 246
        foreach ($this->children() as $item) {
98 246
            if (!($item instanceof AbstractBlock)) {
99
                continue;
100
            }
101
102
            // check for non-final list item ending with blank line:
103 246
            if ($item->endsWithBlankLine() && $item !== $this->lastChild()) {
104 21
                $this->tight = false;
105 21
                break;
106
            }
107
108
            // Recurse into children of list item, to see if there are
109
            // spaces between any of them:
110 234
            foreach ($item->children() as $subItem) {
111 228
                if ($subItem instanceof AbstractBlock && $subItem->endsWithBlankLine() && ($item !== $this->lastChild() || $subItem !== $item->lastChild())) {
112 81
                    $this->tight = false;
113 132
                    break;
114
                }
115
            }
116
        }
117 246
    }
118
119
    /**
120
     * @return bool
121
     */
122 267
    public function isTight(): bool
123
    {
124 267
        return $this->tight;
125
    }
126
127
    /**
128
     * @param bool $tight
129
     *
130
     * @return $this
131
     */
132 3
    public function setTight(bool $tight): self
133
    {
134 3
        $this->tight = $tight;
135
136 3
        return $this;
137
    }
138
}
139