Completed
Push — 1.5 ( fb52dd...2fb592 )
by Colin
02:48
created

ListBlock   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 0
loc 101
ccs 34
cts 36
cp 0.9444
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 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
A endsWithBlankLine() 0 12 4
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
/**
21
 * @method children() AbstractBlock[]
22
 */
23
class ListBlock extends AbstractBlock
24
{
25
    const TYPE_BULLET = 'bullet';
26
    const TYPE_ORDERED = 'ordered';
27
28
    /**
29
     * @deprecated This constant is deprecated in league/commonmark 1.4 and will be removed in 2.0; use TYPE_BULLET instead
30
     */
31
    const TYPE_UNORDERED = self::TYPE_BULLET;
32
33
    /**
34
     * @var bool
35
     */
36
    protected $tight = false;
37
38
    /**
39
     * @var ListData
40
     */
41
    protected $listData;
42
43 360
    public function __construct(ListData $listData)
44
    {
45 360
        $this->listData = $listData;
46 360
    }
47
48
    /**
49
     * @return ListData
50
     */
51 321
    public function getListData(): ListData
52
    {
53 321
        return $this->listData;
54
    }
55
56 153
    public function endsWithBlankLine(): bool
57
    {
58 153
        if ($this->lastLineBlank) {
59 3
            return true;
60
        }
61
62 153
        if ($this->hasChildren()) {
63 153
            return $this->lastChild() instanceof AbstractBlock && $this->lastChild()->endsWithBlankLine();
64
        }
65
66
        return false;
67
    }
68
69 285
    public function canContain(AbstractBlock $block): bool
70
    {
71 285
        return $block instanceof ListItem;
72
    }
73
74 144
    public function isCode(): bool
75
    {
76 144
        return false;
77
    }
78
79 225
    public function matchesNextLine(Cursor $cursor): bool
80
    {
81 225
        return true;
82
    }
83
84 267
    public function finalize(ContextInterface $context, int $endLineNumber)
85
    {
86 267
        parent::finalize($context, $endLineNumber);
87
88 267
        $this->tight = true; // tight by default
89
90 267
        foreach ($this->children() as $item) {
91 267
            if (!($item instanceof AbstractBlock)) {
92
                continue;
93
            }
94
95
            // check for non-final list item ending with blank line:
96 267
            if ($item->endsWithBlankLine() && $item !== $this->lastChild()) {
97 21
                $this->tight = false;
98 21
                break;
99
            }
100
101
            // Recurse into children of list item, to see if there are
102
            // spaces between any of them:
103 255
            foreach ($item->children() as $subItem) {
104 249
                if ($subItem instanceof AbstractBlock && $subItem->endsWithBlankLine() && ($item !== $this->lastChild() || $subItem !== $item->lastChild())) {
105 84
                    $this->tight = false;
106 84
                    break;
107
                }
108
            }
109
        }
110 267
    }
111
112 321
    public function isTight(): bool
113
    {
114 321
        return $this->tight;
115
    }
116
117 3
    public function setTight(bool $tight): self
118
    {
119 3
        $this->tight = $tight;
120
121 3
        return $this;
122
    }
123
}
124