UnmatchedBlockCloser   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 64
ccs 21
cts 21
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 areAllClosed() 0 4 1
A resetTip() 0 8 2
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
/**
20
 * @internal
21
 */
22
class UnmatchedBlockCloser
23
{
24
    /**
25
     * @var ContextInterface
26
     */
27
    private $context;
28
29
    /**
30
     * @var AbstractBlock
31
     */
32
    private $oldTip;
33
34
    /**
35
     * @var AbstractBlock
36
     */
37
    private $lastMatchedContainer;
38
39
    /**
40
     * @param ContextInterface $context
41
     */
42 2079
    public function __construct(ContextInterface $context)
43
    {
44 2079
        $this->context = $context;
45
46 2079
        $this->resetTip();
47 2076
    }
48
49
    /**
50
     * @param AbstractBlock $block
51
     */
52 2067
    public function setLastMatchedContainer(AbstractBlock $block)
53
    {
54 2067
        $this->lastMatchedContainer = $block;
55 2067
    }
56
57 2064
    public function closeUnmatchedBlocks()
58
    {
59 2064
        $endLine = $this->context->getLineNumber() - 1;
60
61 2064
        while ($this->oldTip !== $this->lastMatchedContainer) {
62
            /** @var AbstractBlock $oldTip */
63 681
            $oldTip = $this->oldTip->parent();
64 681
            $this->oldTip->finalize($this->context, $endLine);
65 681
            $this->oldTip = $oldTip;
66
        }
67 2064
    }
68
69 2079
    public function resetTip()
70
    {
71 2079
        if ($this->context->getTip() === null) {
72 3
            throw new \RuntimeException('No tip to reset to');
73
        }
74
75 2076
        $this->oldTip = $this->context->getTip();
76 2076
    }
77
78
    /**
79
     * @return bool
80
     */
81 675
    public function areAllClosed(): bool
82
    {
83 675
        return $this->context->getTip() === $this->lastMatchedContainer;
84
    }
85
}
86