Completed
Push — latest ( 6c0640...3af091 )
by Colin
16s 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 2289
    public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
25
    {
26 2289
        if ($cursor->isIndented()) {
27 195
            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 2190
        $cursor->advanceToNextNonSpaceOrTab();
31
32 2190
        if ($atxHeading = self::getAtxHeader($cursor)) {
33 171
            return BlockStart::of($atxHeading)->at($cursor);
34
        }
35
36 2049
        $setextHeadingLevel = self::getSetextHeadingLevel($cursor);
37 2049
        if ($setextHeadingLevel > 0) {
38 141
            $content = $parserState->getParagraphContent();
39 141
            if ($content !== null) {
40 75
                $cursor->advanceToEnd();
41
42 75
                return BlockStart::of(new HeadingParser($setextHeadingLevel, $content))
43 75
                    ->at($cursor)
44 75
                    ->replaceActiveBlockParser();
45
            }
46
        }
47
48 2004
        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 2190
    private static function getAtxHeader(Cursor $cursor): ?HeadingParser
52
    {
53 2190
        $match = RegexHelper::matchFirst('/^#{1,6}(?:[ \t]+|$)/', $cursor->getLine(), $cursor->getNextNonSpacePosition());
54 2190
        if (! $match) {
55 2049
            return null;
56
        }
57
58 171
        $cursor->advanceToNextNonSpaceOrTab();
59 171
        $cursor->advanceBy(\strlen($match[0]));
60
61 171
        $level = \strlen(\trim($match[0]));
62 171
        $str   = $cursor->getRemainder();
63 171
        $str   = \preg_replace('/^[ \t]*#+[ \t]*$/', '', $str);
64
        \assert(\is_string($str));
65 171
        $str = \preg_replace('/[ \t]+#+[ \t]*$/', '', $str);
66
        \assert(\is_string($str));
67
68 171
        return new HeadingParser($level, $str);
69
    }
70
71 2049
    private static function getSetextHeadingLevel(Cursor $cursor): int
72
    {
73 2049
        $match = RegexHelper::matchFirst('/^(?:=+|-+)[ \t]*$/', $cursor->getLine(), $cursor->getNextNonSpacePosition());
74 2049
        if ($match === null) {
75 1962
            return 0;
76
        }
77
78 141
        return $match[0][0] === '=' ? 1 : 2;
79
    }
80
}
81