TableOfContentsPlaceholderParser   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
dl 0
loc 52
ccs 17
cts 18
cp 0.9444
c 2
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ tryStart() 0 13 3
A tryContinue() 0 3 1
A hp$0 ➔ setConfiguration() 0 3 1
A __construct() 0 3 1
A getBlock() 0 3 1
A hp$0 ➔ blockStartParser() 0 28 1
blockStartParser() 0 28 ?
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\TableOfContents;
15
16
use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsPlaceholder;
17
use League\CommonMark\Node\Block\AbstractBlock;
18
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
19
use League\CommonMark\Parser\Block\BlockContinue;
20
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
21
use League\CommonMark\Parser\Block\BlockStart;
22
use League\CommonMark\Parser\Block\BlockStartParserInterface;
23
use League\CommonMark\Parser\Cursor;
24
use League\CommonMark\Parser\MarkdownParserStateInterface;
25
use League\Config\ConfigurationAwareInterface;
26
use League\Config\ConfigurationInterface;
27
28
final class TableOfContentsPlaceholderParser extends AbstractBlockContinueParser
29
{
30
    /**
31
     * @var TableOfContentsPlaceholder
32
     *
33
     * @psalm-readonly
34
     */
35
    private $block;
36
37 3
    public function __construct()
38
    {
39 3
        $this->block = new TableOfContentsPlaceholder();
40 3
    }
41
42 3
    public function getBlock(): AbstractBlock
43
    {
44 3
        return $this->block;
45
    }
46
47 3
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
48
    {
49 3
        return BlockContinue::none();
50
    }
51
52 2
    public static function blockStartParser(): BlockStartParserInterface
53
    {
54 2
        return new class () implements BlockStartParserInterface, ConfigurationAwareInterface {
55
            /**
56
             * @var ConfigurationInterface
57
             *
58
             * @psalm-readonly-allow-private-mutation
59
             */
60
            private $config;
61
62 1
            public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
63
            {
64 3
                $placeholder = $this->config->get('table_of_contents/placeholder');
65 3
                if ($placeholder === null) {
66
                    return BlockStart::none();
67
                }
68
69
                // The placeholder must be the only thing on the line
70 3
                if ($cursor->match('/^' . \preg_quote($placeholder, '/') . '$/') === null) {
71 3
                    return BlockStart::none();
72
                }
73
74 3
                return BlockStart::of(new TableOfContentsPlaceholderParser())->at($cursor);
75
            }
76
77 1
            public function setConfiguration(ConfigurationInterface $configuration): void
78
            {
79 3
                $this->config = $configuration;
80 3
            }
81
        };
82
    }
83
}
84