Completed
Push — master ( 9dd27c...9b01bc )
by Kacper
03:24
created

Twig::getMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
1
<?php
2
3
4
namespace Kadet\Highlighter\Language;
5
6
7
use Kadet\Highlighter\Language\Python\Django;
8
use Kadet\Highlighter\Matcher\RegexMatcher;
9
use Kadet\Highlighter\Matcher\WordMatcher;
10
use Kadet\Highlighter\Parser\Rule;
11
12
class Twig extends Django
13
{
14
    public function setupRules()
15
    {
16
        parent::setupRules();
17
18
        $tag = $this->rules->rule('call.template-tag');
19
        $this->rules->remove('call.template-tag');
20
21
        $this->rules->addMany([
22
            'call' => [
23
                new Rule(new RegexMatcher('/(\w+)\s*\(/si'), ['priority' => 1]),
24
                'test' => new Rule(new RegexMatcher('/is(?:\s+not)?\s+(\w+)/si'), ['priority' => 1]),
25
            ],
26
            'keyword' => [
27
                'template-tag' => new Rule(new WordMatcher(['only', 'with', 'is', 'not', 'ignore missing', 'in'])),
28
                $tag, // template tags are often used in django as functions, whereas in twig they are more keyword like
29
            ]
30
        ]);
31
    }
32
33
    public function getIdentifier()
34
    {
35
        return 'twig';
36
    }
37
38
    public static function getMetadata()
39
    {
40
        return [
0 ignored issues
show
Best Practice introduced by
The expression return array('name' => a... 'injectable' => true); seems to be an array, but some of its elements' types (string[]) are incompatible with the return type of the parent method Kadet\Highlighter\Langua...hon\Django::getMetadata of type array<string,string[]|boolean>.

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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
41
            'name'      => ['twig'],
42
            'mime'      => ['text/x-twig'],
43
            'extension' => ['*.twig'],
44
            'standalone' => false,
45
            'injectable' => true
46
        ];
47
    }
48
}