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

CommonMark/Parser/Block/ListItemParser.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\Extension\CommonMark\Node\Block\ListBlock;
17
use League\CommonMark\Extension\CommonMark\Node\Block\ListData;
18
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
19
use League\CommonMark\Node\Block\AbstractBlock;
20
use League\CommonMark\Node\Block\Paragraph;
21
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
22
use League\CommonMark\Parser\Block\BlockContinue;
23
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
24
use League\CommonMark\Parser\Cursor;
25
26
final class ListItemParser extends AbstractBlockContinueParser
27
{
28
    /**
29
     * @var ListItem
30
     *
31
     * @psalm-readonly
32
     */
33
    private $block;
34
35
    /** @var bool */
36
    private $hadBlankLine = false;
37
38 288
    public function __construct(ListData $listData)
39
    {
40 288
        $this->block = new ListItem($listData);
41 288
    }
42
43
    /**
44
     * @return ListItem
45
     */
46 288
    public function getBlock(): AbstractBlock
47
    {
48 288
        return $this->block;
49
    }
50
51 270
    public function isContainer(): bool
52
    {
53 270
        return true;
54
    }
55
56 264
    public function canContain(AbstractBlock $childBlock): bool
57
    {
58 264
        if ($this->hadBlankLine) {
59
            // We saw a blank line in this list item, that means the list block is loose.
60
            //
61
            // spec: if any of its constituent list items directly contain two block-level elements with a blank line
62
            // between them
63 87
            $parent = $this->block->parent();
64 87
            if ($parent instanceof ListBlock) {
65 87
                $parent->setTight(false);
66
            }
67
        }
68
69 264
        return true;
70
    }
71
72 228
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
73
    {
74 228
        if ($cursor->isBlank()) {
75 135
            if ($this->block->firstChild() === null) {
76
                // Blank line after empty list item
77 6
                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...
78
            }
79
80 129
            $activeBlock = $activeBlockParser->getBlock();
81
            // If the active block is a code block, blank lines in it should not affect if the list is tight.
82 129
            $this->hadBlankLine = $activeBlock instanceof Paragraph || $activeBlock instanceof ListItem;
83 129
            $cursor->advanceToNextNonSpaceOrTab();
84
85 129
            return BlockContinue::at($cursor);
86
        }
87
88 225
        $contentIndent = $this->block->getListData()->markerOffset + $this->getBlock()->getListData()->padding;
89 225
        if ($cursor->getIndent() >= $contentIndent) {
90 129
            $cursor->advanceBy($contentIndent, true);
91
92 129
            return BlockContinue::at($cursor);
93
        }
94
95 144
        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...
96
    }
97
}
98