Completed
Push — master ( db24e4...a24032 )
by Colin
02:01
created

SmartPunctExtension::getInlineParsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark-extras package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (http://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Extras\SmartPunct;
16
17
use League\CommonMark\Block\Renderer as CoreBlockRenderer;
18
use League\CommonMark\Extension\Extension;
19
use League\CommonMark\Inline\Renderer as CoreInlineRenderer;
20
21
class SmartPunctExtension extends Extension
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 45
    public function getInlineParsers()
27
    {
28
        return [
29 45
            new QuoteParser(),
30 45
            new PunctuationParser(),
31 45
        ];
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 45
    public function getInlineProcessors()
38
    {
39
        return [
40 45
            new QuoteProcessor(),
41 45
        ];
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 45
    public function getBlockRenderers()
48
    {
49
        return [
50 45
            'League\CommonMark\Block\Element\Document'  => new CoreBlockRenderer\DocumentRenderer(),
51 45
            'League\CommonMark\Block\Element\Paragraph' => new CoreBlockRenderer\ParagraphRenderer(),
52 45
        ];
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 45
    public function getInlineRenderers()
59
    {
60
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('League\\Co...nderer\TextRenderer()); (array<string,League\Comm...\Renderer\TextRenderer>) is incompatible with the return type declared by the interface League\CommonMark\Extens...ace::getInlineRenderers of type League\CommonMark\Inline...lineRendererInterface[].

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...
61 45
            'League\CommonMark\Inline\Element\Text' => new CoreInlineRenderer\TextRenderer(),
62 45
        ];
63
    }
64
}
65