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

CommonMark/Parser/Block/IndentedCodeParser.php (1 issue)

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\Extension\CommonMark\Node\Block\IndentedCode;
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\Cursor;
22
use League\CommonMark\Util\ArrayCollection;
23
24
final class IndentedCodeParser extends AbstractBlockContinueParser
25
{
26
    /**
27
     * @var IndentedCode
28
     *
29
     * @psalm-readonly
30
     */
31
    private $block;
32
33
    /** @var ArrayCollection<string> */
34
    protected $strings;
35
36 153
    public function __construct()
37
    {
38 153
        $this->block   = new IndentedCode();
39 153
        $this->strings = new ArrayCollection();
40 153
    }
41
42
    /**
43
     * @return IndentedCode
44
     */
45 153
    public function getBlock(): AbstractBlock
46
    {
47 153
        return $this->block;
48
    }
49
50 93
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
51
    {
52 93
        if ($cursor->isIndented()) {
53 42
            $cursor->advanceBy(Cursor::INDENT_LEVEL, true);
54
55 42
            return BlockContinue::at($cursor);
56
        }
57
58 72
        if ($cursor->isBlank()) {
59 60
            $cursor->advanceToNextNonSpaceOrTab();
60
61 60
            return BlockContinue::at($cursor);
62
        }
63
64 51
        return BlockContinue::none();
1 ignored issue
show
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...
65
    }
66
67 153
    public function addLine(string $line): void
68
    {
69 153
        $this->strings[] = $line;
70 153
    }
71
72 153
    public function closeBlock(): void
73
    {
74 153
        $reversed = \array_reverse($this->strings->toArray(), true);
75 153
        foreach ($reversed as $index => $line) {
76 153
            if ($line !== '' && $line !== "\n" && ! \preg_match('/^(\n *)$/', $line)) {
77 153
                break;
78
            }
79
80 45
            unset($reversed[$index]);
81
        }
82
83 153
        $fixed = \array_reverse($reversed);
84 153
        $tmp   = \implode("\n", $fixed);
85 153
        if (\substr($tmp, -1) !== "\n") {
86 153
            $tmp .= "\n";
87
        }
88
89 153
        $this->block->setLiteral($tmp);
90 153
    }
91
}
92