Completed
Push — latest ( 609def...dcbb7e )
by Colin
14s queued 11s
created

CommonMark/Parser/Block/HeadingStartParser.php (2 issues)

Labels
Severity
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\CommonMark\Parser\Block;
15
16
use League\CommonMark\Parser\Block\BlockStart;
17
use League\CommonMark\Parser\Block\BlockStartParserInterface;
18
use League\CommonMark\Parser\Cursor;
19
use League\CommonMark\Parser\MarkdownParserStateInterface;
20
use League\CommonMark\Util\RegexHelper;
21
22
class HeadingStartParser implements BlockStartParserInterface
23
{
24 2187
    public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
25
    {
26 2187
        if ($cursor->isIndented()) {
27 186
            return BlockStart::none();
1 ignored issue
show
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...
28
        }
29
30 2088
        $cursor->advanceToNextNonSpaceOrTab();
31
32 2088
        if ($atxHeading = self::getAtxHeader($cursor)) {
33 156
            return BlockStart::of($atxHeading)->at($cursor);
34
        }
35
36 1956
        $setextHeadingLevel = self::getSetextHeadingLevel($cursor);
37 1956
        if ($setextHeadingLevel > 0) {
38 138
            $content = $parserState->getParagraphContent();
39 138
            if ($content !== null) {
40 72
                $cursor->advanceToEnd();
41
42 72
                return BlockStart::of(new HeadingParser($setextHeadingLevel, $content))
43 72
                    ->at($cursor)
44 72
                    ->replaceActiveBlockParser();
45
            }
46
        }
47
48 1914
        return BlockStart::none();
1 ignored issue
show
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...
49
    }
50
51 2088
    private static function getAtxHeader(Cursor $cursor): ?HeadingParser
52
    {
53 2088
        $match = RegexHelper::matchFirst('/^#{1,6}(?:[ \t]+|$)/', $cursor->getLine(), $cursor->getNextNonSpacePosition());
54 2088
        if (! $match) {
55 1956
            return null;
56
        }
57
58 156
        $cursor->advanceToNextNonSpaceOrTab();
59 156
        $cursor->advanceBy(\strlen($match[0]));
60
61 156
        $level = \strlen(\trim($match[0]));
62 156
        $str   = $cursor->getRemainder();
63 156
        $str   = \preg_replace('/^[ \t]*#+[ \t]*$/', '', $str);
64
        \assert(\is_string($str));
65 156
        $str = \preg_replace('/[ \t]+#+[ \t]*$/', '', $str);
66
        \assert(\is_string($str));
67
68 156
        return new HeadingParser($level, $str);
69
    }
70
71 1956
    private static function getSetextHeadingLevel(Cursor $cursor): int
72
    {
73 1956
        $match = RegexHelper::matchFirst('/^(?:=+|-+)[ \t]*$/', $cursor->getLine(), $cursor->getNextNonSpacePosition());
74 1956
        if ($match === null) {
75 1872
            return 0;
76
        }
77
78 138
        return $match[0][0] === '=' ? 1 : 2;
79
    }
80
}
81