Passed
Push — master ( 81aad7...eebd04 )
by Kacper
04:31
created

Xaml::setupRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 20
ccs 0
cts 15
cp 0
crap 2
rs 9.4285
c 1
b 0
f 1
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
19
use Kadet\Highlighter\Matcher\RegexMatcher;
20
use Kadet\Highlighter\Parser\Rule;
21
22
class Xaml extends Xml
23
{
24
    public function setupRules()
25
    {
26
        parent::setupRules();
27
28
        $attribute = $this->rules->rule('symbol.attribute');
29
        $tag = $this->rules->rule('symbol.tag');
30
31
        $this->rules->remove('symbol.attribute');
32
        $this->rules->remove('symbol.tag');
33
34
        $this->rules->add('variable', $attribute);
35
        $this->rules->add('symbol.class', $tag);
36
        $this->rules->add('variable.property', new Rule(new RegexMatcher('/[\w-]+\.([\w-]+)/'), [
37
            'context' => ['*tag', '*expression', '!string']
38
        ]));
39
40
        $this->rules->add('expression', new Rule(new RegexMatcher('/=["\']?(?P<expression>\{(?>(?:(?&expression)|[^{}]*)*)\})/'), [
41
            'context' => ['tag.open']
42
        ]));
43
    }
44
45
    public static function getMetadata()
46
    {
47
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('name' => a...n' => array('*.xaml')); (array<string,array>) is incompatible with the return type of the parent method Kadet\Highlighter\Language\Xml::getMetadata 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...
48
            'name'      => ['xaml'],
49
            'mime'      => [],
50
            'extension' => ['*.xaml']
51
        ];
52
    }
53
}
54