ListBlock   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.18%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 1
dl 0
loc 131
ccs 34
cts 39
cp 0.8718
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getListData() 0 4 1
A canContain() 0 4 1
A acceptsLines() 0 4 1
A isCode() 0 4 1
A matchesNextLine() 0 4 1
A endsWithBlankLine() 0 12 4
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\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 255
    public function __construct(ListData $listData)
36
    {
37 255
        parent::__construct();
38
39 255
        $this->listData = $listData;
40 255
    }
41
42
    /**
43
     * @return ListData
44
     */
45 255
    public function getListData()
46
    {
47 255
        return $this->listData;
48
    }
49
50
    /**
51
     * @return bool
52
     */
53 30
    public function endsWithBlankLine()
54
    {
55 30
        if ($this->lastLineBlank) {
56 3
            return true;
57
        }
58
59 30
        if ($this->hasChildren()) {
60 30
            return $this->lastChild() instanceof AbstractBlock && $this->lastChild()->endsWithBlankLine();
61
        }
62
63
        return false;
64
    }
65
66
    /**
67
     * Returns true if this block can contain the given block as a child node
68
     *
69
     * @param AbstractBlock $block
70
     *
71
     * @return bool
72
     */
73 237
    public function canContain(AbstractBlock $block)
74
    {
75 237
        return $block instanceof ListItem;
76
    }
77
78
    /**
79
     * Returns true if block type can accept lines of text
80
     *
81
     * @return bool
82
     */
83 21
    public function acceptsLines()
84
    {
85 21
        return false;
86
    }
87
88
    /**
89
     * Whether this is a code block
90
     *
91
     * @return bool
92
     */
93 123
    public function isCode()
94
    {
95 123
        return false;
96
    }
97
98 204
    public function matchesNextLine(Cursor $cursor)
99
    {
100 204
        return true;
101
    }
102
103 237
    public function finalize(ContextInterface $context, $endLineNumber)
104
    {
105 237
        parent::finalize($context, $endLineNumber);
106
107 237
        $this->tight = true; // tight by default
108
109 237
        foreach ($this->children() as $item) {
110 237
            if (!($item instanceof AbstractBlock)) {
111
                continue;
112
            }
113
114
            // check for non-final list item ending with blank line:
115 237
            if ($item->endsWithBlankLine() && $item !== $this->lastChild()) {
116 18
                $this->tight = false;
117 18
                break;
118
            }
119
120
            // Recurse into children of list item, to see if there are
121
            // spaces between any of them:
122 228
            foreach ($item->children() as $subItem) {
123 222
                if ($subItem instanceof AbstractBlock && $subItem->endsWithBlankLine() && ($item !== $this->lastChild() || $subItem !== $item->lastChild())) {
124 81
                    $this->tight = false;
125 228
                    break;
126
                }
127
            }
128
        }
129 237
    }
130
131
    /**
132
     * @return bool
133
     */
134 255
    public function isTight()
135
    {
136 255
        return $this->tight;
137
    }
138
139
    /**
140
     * @param bool $tight
141
     *
142
     * @return $this
143
     */
144
    public function setTight($tight)
145
    {
146
        $this->tight = $tight;
147
148
        return $this;
149
    }
150
}
151