Completed
Push — master ( b06b97...f73407 )
by Colin
16s
created

ListBlock::finalize()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 10.0363

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 13
cts 14
cp 0.9286
rs 7.6666
c 0
b 0
f 0
cc 10
nc 6
nop 2
crap 10.0363

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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(): ListData
46
    {
47 255
        return $this->listData;
48
    }
49
50
    /**
51
     * @return bool
52
     */
53 30
    public function endsWithBlankLine(): bool
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): bool
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(): bool
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(): bool
94
    {
95 123
        return false;
96
    }
97
98 204
    public function matchesNextLine(Cursor $cursor): bool
99
    {
100 204
        return true;
101
    }
102
103 237
    public function finalize(ContextInterface $context, int $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 130
                    break;
126
                }
127
            }
128
        }
129 237
    }
130
131
    /**
132
     * @return bool
133
     */
134 255
    public function isTight(): bool
135
    {
136 255
        return $this->tight;
137
    }
138
139
    /**
140
     * @param bool $tight
141
     *
142
     * @return $this
143
     */
144
    public function setTight(bool $tight): self
145
    {
146
        $this->tight = $tight;
147
148
        return $this;
149
    }
150
}
151