Completed
Push — master ( 15e8d5...9ebb39 )
by Colin
04:59
created

SmartPunctExtension::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
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
 * 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\Extension;
16
17
use League\CommonMark\Block\Renderer as BlockRenderer;
18
use League\CommonMark\Inline\Parser as InlineParser;
19
use League\CommonMark\Inline\Processor as InlineProcessor;
20
use League\CommonMark\Inline\Renderer as InlineRenderer;
21
22
class SmartPunctExtension extends Extension
23
{
24
    /**
25
     * @return InlineParser\InlineParserInterface[]
26
     */
27 45
    public function getInlineParsers()
28
    {
29
        return [
30 45
            new InlineParser\QuoteParser(),
31 45
            new InlineParser\SmartPunctParser(),
32 45
        ];
33
    }
34
35
    /**
36
     * @return InlineProcessor\InlineProcessorInterface[]
37
     */
38 45
    public function getInlineProcessors()
39
    {
40
        return [
41 45
            new InlineProcessor\QuoteProcessor(),
42 45
        ];
43
    }
44
45
    /**
46
     * @return BlockRenderer\BlockRendererInterface[]
47
     */
48 45
    public function getBlockRenderers()
49
    {
50
        return [
51 45
            'League\CommonMark\Block\Element\Document'  => new BlockRenderer\DocumentRenderer(),
52 45
            'League\CommonMark\Block\Element\Paragraph' => new BlockRenderer\ParagraphRenderer(),
53 45
        ];
54
    }
55
56
    /**
57
     * @return InlineRenderer\InlineRendererInterface[]
58
     */
59 45
    public function getInlineRenderers()
60
    {
61
        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...
62 45
            'League\CommonMark\Inline\Element\Text' => new InlineRenderer\TextRenderer(),
63 45
        ];
64
    }
65
}
66