UnmatchedBlockCloser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 60
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setLastMatchedContainer() 0 4 1
A closeUnmatchedBlocks() 0 11 2
A resetTip() 0 4 1
A areAllClosed() 0 4 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;
16
17
use League\CommonMark\Block\Element\AbstractBlock;
18
19
class UnmatchedBlockCloser
20
{
21
    /**
22
     * @var ContextInterface
23
     */
24
    private $context;
25
26
    /**
27
     * @var AbstractBlock
28
     */
29
    private $oldTip;
30
31
    /**
32
     * @var AbstractBlock
33
     */
34
    private $lastMatchedContainer;
35
36
    /**
37
     * @param ContextInterface $context
38
     */
39 1950
    public function __construct(ContextInterface $context)
40
    {
41 1950
        $this->context = $context;
42
43 1950
        $this->resetTip();
44 1950
    }
45
46
    /**
47
     * @param AbstractBlock $block
48
     */
49 1938
    public function setLastMatchedContainer(AbstractBlock $block)
50
    {
51 1938
        $this->lastMatchedContainer = $block;
52 1938
    }
53
54 1938
    public function closeUnmatchedBlocks()
55
    {
56 1938
        $endLine = $this->context->getLineNumber() - 1;
57
58 1938
        while ($this->oldTip !== $this->lastMatchedContainer) {
59
            /** @var AbstractBlock $oldTip */
60 666
            $oldTip = $this->oldTip->parent();
61 666
            $this->oldTip->finalize($this->context, $endLine);
62 666
            $this->oldTip = $oldTip;
63
        }
64 1938
    }
65
66 1950
    public function resetTip()
67
    {
68 1950
        $this->oldTip = $this->context->getTip();
69 1950
    }
70
71
    /**
72
     * @return bool
73
     */
74 639
    public function areAllClosed(): bool
75
    {
76 639
        return $this->context->getTip() === $this->lastMatchedContainer;
77
    }
78
}
79