Passed
Branch symfony-console (1f7bcb)
by Kacper
03:39
created

Html::setupRules()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 33
rs 8.8571
cc 1
eloc 23
nc 1
nop 0
1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\Language;
17
18
use Kadet\Highlighter\Matcher\RegexMatcher;
19
use Kadet\Highlighter\Parser\CloseRule;
20
use Kadet\Highlighter\Parser\Token\LanguageToken;
21
use Kadet\Highlighter\Parser\OpenRule;
22
use Kadet\Highlighter\Parser\TokenFactory;
23
24
class Html extends Xml
25
{
26
    /**
27
     * Tokenization rules
28
     */
29
    public function setupRules()
30
    {
31
        parent::setupRules();
32
33
        $css = new Css();
34
        $js  = new JavaScript();
35
        $this->rules->addMany([
36
            'language.'.$js->getIdentifier() => [
37
                new OpenRule(new RegexMatcher('/<script.*?>()/'), [
38
                    'factory'     => new TokenFactory(LanguageToken::class),
39
                    'inject'      => $js,
40
                    'language'    => $this,
41
                    'postProcess' => true
42
                ]),
43
                new CloseRule(new RegexMatcher('/()<\/script>/'), [
44
                    'factory'  => new TokenFactory(LanguageToken::class),
45
                    'language' => $js
46
                ])
47
            ],
48
            'language.'.$css->getIdentifier() => [
49
                new OpenRule(new RegexMatcher('/<style.*?>()/'), [
50
                    'factory'     => new TokenFactory(LanguageToken::class),
51
                    'inject'      => $css,
52
                    'language'    => $this,
53
                    'postProcess' => true
54
                ]),
55
                new CloseRule(new RegexMatcher('/()<\/style>/'), [
56
                    'factory'  => new TokenFactory(LanguageToken::class),
57
                    'language' => $css
58
                ])
59
            ]
60
        ]);
61
    }
62
63
    public function getIdentifier()
64
    {
65
        return 'html';
66
    }
67
68 View Code Duplication
    public static function getAliases()
69
    {
70
        return [
1 ignored issue
show
Bug Best Practice introduced by
The return type of return array('name' => a...ay('*.html', '*.htm')); (array<string,string[]>) is incompatible with the return type of the parent method Kadet\Highlighter\Language\Xml::getAliases of type array<string,string[]>.

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...
71
            'name'      => ['html'],
72
            'mime'      => ['text/html'],
73
            'extension' => ['*.html', '*.htm']
74
        ];
75
    }
76
}
77