Completed
Push — cleanup ( a91339 )
by Colin
02:44
created

UnmatchedBlockCloser::resetTip()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.032
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 2037
    public function __construct(ContextInterface $context)
43
    {
44 2037
        $this->context = $context;
45
46 2037
        $this->resetTip();
47 2037
    }
48
49
    /**
50
     * @param AbstractBlock $block
51
     */
52 2028
    public function setLastMatchedContainer(AbstractBlock $block)
53
    {
54 2028
        $this->lastMatchedContainer = $block;
55 2028
    }
56
57 2028
    public function closeUnmatchedBlocks()
58
    {
59 2028
        $endLine = $this->context->getLineNumber() - 1;
60
61 2028
        while ($this->oldTip !== $this->lastMatchedContainer) {
62
            /** @var AbstractBlock $oldTip */
63 687
            $oldTip = $this->oldTip->parent();
64 687
            $this->oldTip->finalize($this->context, $endLine);
65 687
            $this->oldTip = $oldTip;
66
        }
67 2028
    }
68
69 2037
    public function resetTip()
70
    {
71 2037
        if ($this->context->getTip() === null) {
72
            throw new \RuntimeException('No tip to reset to');
73
        }
74
75 2037
        $this->oldTip = $this->context->getTip();
76 2037
    }
77
78
    /**
79
     * @return bool
80
     */
81 678
    public function areAllClosed(): bool
82
    {
83 678
        return $this->context->getTip() === $this->lastMatchedContainer;
84
    }
85
}
86