AttributesBlockContinueParser::tryContinue()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 3
nop 2
dl 0
loc 27
ccs 14
cts 14
cp 1
crap 4
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 * (c) 2015 Martin Hasoň <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace League\CommonMark\Extension\Attributes\Parser;
16
17
use League\CommonMark\Extension\Attributes\Node\Attributes;
18
use League\CommonMark\Extension\Attributes\Util\AttributesHelper;
19
use League\CommonMark\Node\Block\AbstractBlock;
20
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
21
use League\CommonMark\Parser\Block\BlockContinue;
22
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
23
use League\CommonMark\Parser\Cursor;
24
25
final class AttributesBlockContinueParser extends AbstractBlockContinueParser
26
{
27
    private Attributes $block;
28
29
    private AbstractBlock $container;
30
31
    private bool $hasSubsequentLine = false;
32
33
    /**
34
     * @param array<string, mixed> $attributes The attributes identified by the block start parser
35
     * @param AbstractBlock        $container  The node we were in when these attributes were discovered
36
     */
37 14
    public function __construct(array $attributes, AbstractBlock $container)
38
    {
39 14
        $this->block = new Attributes($attributes);
40
41 14
        $this->container = $container;
42
    }
43
44 14
    public function getBlock(): AbstractBlock
45
    {
46 14
        return $this->block;
47
    }
48
49 12
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
50
    {
51 12
        $this->hasSubsequentLine = true;
52
53 12
        $cursor->advanceToNextNonSpaceOrTab();
54
55
        // Does this next line also have attributes?
56 12
        $attributes = AttributesHelper::parseAttributes($cursor);
57 12
        $cursor->advanceToNextNonSpaceOrTab();
58 12
        if ($cursor->isAtEnd() && $attributes !== []) {
59
            // It does! Merge them into what we parsed previously
60 2
            $this->block->setAttributes(AttributesHelper::mergeAttributes(
61 2
                $this->block->getAttributes(),
62 2
                $attributes
63 2
            ));
64
65
            // Tell the core parser we've consumed everything
66 2
            return BlockContinue::at($cursor);
67
        }
68
69
        // Okay, so there are no attributes on the next line
70
        // If this next line is blank we know we can't target the next node, it must be a previous one
71 12
        if ($cursor->isBlank()) {
72 6
            $this->block->setTarget(Attributes::TARGET_PREVIOUS);
73
        }
74
75 12
        return BlockContinue::none();
76
    }
77
78 14
    public function closeBlock(): void
79
    {
80
        // Attributes appearing at the very end of the document won't have any last lines to check
81
        // so we can make that determination here
82 14
        if (! $this->hasSubsequentLine) {
83 4
            $this->block->setTarget(Attributes::TARGET_PREVIOUS);
84
        }
85
86
        // We know this block must apply to the "previous" block, but that could be a sibling or parent,
87
        // so we check the containing block to see which one it might be.
88 14
        if ($this->block->getTarget() === Attributes::TARGET_PREVIOUS && $this->block->parent() === $this->container) {
89 2
            $this->block->setTarget(Attributes::TARGET_PARENT);
90
        }
91
    }
92
}
93