ListBlock::finalize()   B
last analyzed

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