UnmatchedBlockCloser::resetTip()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 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