Completed
Push — refactor-parsing ( adbb6b...fbe6de )
by Colin
08:21 queued 07:01
created

HtmlBlockParser::closeBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\CommonMark\Extension\CommonMark\Parser\Block;
13
14
use League\CommonMark\Extension\CommonMark\Node\Block\HtmlBlock;
15
use League\CommonMark\Node\Block\AbstractBlock;
16
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
17
use League\CommonMark\Parser\Block\BlockContinue;
18
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
19
use League\CommonMark\Parser\Cursor;
20
use League\CommonMark\Util\RegexHelper;
21
22
final class HtmlBlockParser extends AbstractBlockContinueParser
23
{
24
    /** @var HtmlBlock */
25
    private $block;
26
27
    /**
28
     * @var string
29
     */
30
    private $content = '';
31
32
    /** @var bool */
33
    private $finished = false;
34
35 168
    public function __construct(int $blockType)
36
    {
37 168
        $this->block = new HtmlBlock($blockType);
38 168
    }
39
40
    /**
41
     * @return HtmlBlock
42
     */
43 168
    public function getBlock(): AbstractBlock
44
    {
45 168
        return $this->block;
46
    }
47
48 153
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
49
    {
50 153
        if ($this->finished) {
51 48
            return BlockContinue::none();
52
        }
53
54 138
        if ($cursor->isBlank() && \in_array($this->block->getType(), [HtmlBlock::TYPE_6_BLOCK_ELEMENT, HtmlBlock::TYPE_7_MISC_ELEMENT], true)) {
55 48
            return BlockContinue::none();
56
        }
57
58 117
        return BlockContinue::at($cursor);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \League\CommonMar...kContinue::at($cursor); (self) is incompatible with the return type declared by the interface League\CommonMark\Parser...rInterface::tryContinue of type League\CommonMark\Parser\Block\BlockContinue|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
59
    }
60
61 168
    public function addLine(string $line): void
62
    {
63 168
        if ($this->content !== '') {
64 117
            $this->content .= "\n";
65
        }
66
67 168
        $this->content .= $line;
68
69
        // Check for end condition
70 168
        if ($this->block->getType() >= HtmlBlock::TYPE_1_CODE_CONTAINER && $this->block->getType() <= HtmlBlock::TYPE_5_CDATA) {
71 57
            $cursor = new Cursor($line);
72 57
            if ($cursor->match(RegexHelper::getHtmlBlockCloseRegex($this->block->getType())) !== null) {
73 54
                $this->finished = true;
74
            }
75
        }
76 168
    }
77
78 168
    public function closeBlock(): void
79
    {
80 168
        $this->block->setLiteral($this->content);
81 168
        $this->content = '';
82 168
    }
83
}
84