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

FencedCodeParser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 63
ccs 24
cts 25
cp 0.96
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getBlock() 0 4 1
A tryContinue() 0 16 5
A addLine() 0 4 1
A closeBlock() 0 16 3
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\FencedCode;
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\ArrayCollection;
21
use League\CommonMark\Util\RegexHelper;
22
23
final class FencedCodeParser extends AbstractBlockContinueParser
24
{
25
    /** @var FencedCode */
26
    private $block;
27
28
    /**
29
     * @var ArrayCollection<int, string>
30
     */
31
    protected $strings;
32
33 105
    public function __construct(int $fenceLength, string $fenceChar, int $fenceOffset)
34
    {
35 105
        $this->block = new FencedCode($fenceLength, $fenceChar, $fenceOffset);
36 105
        $this->strings = new ArrayCollection();
37 105
    }
38
39
    /**
40
     * @return FencedCode
41
     */
42 105
    public function getBlock(): AbstractBlock
43
    {
44 105
        return $this->block;
45
    }
46
47 99
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
48
    {
49
        // Check for closing code fence
50 99
        if (!$cursor->isIndented() && $cursor->getNextNonSpaceCharacter() === $this->block->getChar()) {
51 93
            $match = RegexHelper::matchAll('/^(?:`{3,}|~{3,})(?= *$)/', $cursor->getLine(), $cursor->getNextNonSpacePosition());
52 93
            if ($match !== null && \strlen($match[0]) >= $this->block->getLength()) {
53
                // closing fence - we're at end of line, so we can finalize now
54 87
                return BlockContinue::finished();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \League\CommonMar...ckContinue::finished(); (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...
55
            }
56
        }
57
58
        // Skip optional spaces of fence offset
59 93
        $cursor->match('/^ {0,' . $this->block->getOffset() . '}/');
60
61 93
        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...
62
    }
63
64 105
    public function addLine(string $line): void
65
    {
66 105
        $this->strings[] = $line;
67 105
    }
68
69 105
    public function closeBlock(): void
70
    {
71
        // first line becomes info string
72 105
        $firstLine = $this->strings->first();
73 105
        if ($firstLine === false) {
74
            $firstLine = '';
75
        }
76
77 105
        $this->block->setInfo(RegexHelper::unescape(\trim($firstLine)));
78
79 105
        if ($this->strings->count() === 1) {
80 12
            $this->block->setLiteral('');
81
        } else {
82 93
            $this->block->setLiteral(\implode("\n", $this->strings->slice(1)) . "\n");
83
        }
84 105
    }
85
}
86