Passed
Push — latest ( ba39d8...ca9086 )
by Colin
08:23
created

TableOfContentsPlaceholderParser   A

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\Configuration\ConfigurationAwareInterface;
17
use League\CommonMark\Configuration\ConfigurationInterface;
18
use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsPlaceholder;
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\Block\BlockStart;
24
use League\CommonMark\Parser\Block\BlockStartParserInterface;
25
use League\CommonMark\Parser\Cursor;
26
use League\CommonMark\Parser\MarkdownParserStateInterface;
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();
1 ignored issue
show
Bug introduced by Colin O'Dell
Are you sure the usage of League\CommonMark\Parser...k\BlockContinue::none() targeting League\CommonMark\Parser...k\BlockContinue::none() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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();
1 ignored issue
show
Bug introduced by Colin O'Dell
Are you sure the usage of League\CommonMark\Parser\Block\BlockStart::none() targeting League\CommonMark\Parser\Block\BlockStart::none() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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();
1 ignored issue
show
Bug introduced by Colin O'Dell
Are you sure the usage of League\CommonMark\Parser\Block\BlockStart::none() targeting League\CommonMark\Parser\Block\BlockStart::none() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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