Passed
Push — master ( 05f929...203740 )
by Kacper
03:48 queued 01:02
created

Language/PhpLanguage.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2015, Some right reserved.
6
 * @author Kacper "Kadet" Donat <[email protected]>
7
 *
8
 * Contact with author:
9
 * Xmpp: [email protected]
10
 * E-mail: [email protected]
11
 *
12
 * From Kadet with love.
13
 */
14
15
namespace Kadet\Highlighter\Language;
16
17
18
use Kadet\Highlighter\Matcher\CommentMatcher;
19
use Kadet\Highlighter\Matcher\RegexMatcher;
20
use Kadet\Highlighter\Matcher\SubStringMatcher;
21
use Kadet\Highlighter\Matcher\WordMatcher;
22
use Kadet\Highlighter\Parser\CloseRule;
23
use Kadet\Highlighter\Parser\LanguageToken;
24
use Kadet\Highlighter\Parser\ContextualToken;
25
use Kadet\Highlighter\Parser\Rule;
26
use Kadet\Highlighter\Parser\OpenRule;
27
use Kadet\Highlighter\Parser\Token;
28
use Kadet\Highlighter\Parser\TokenFactory;
29
30
class PhpLanguage extends Language
31
{
32
    public function getRules()
33
    {
34
        return [
35
            'string.single' => new Rule(new SubStringMatcher('\''), [
36
                'context' => ['!keyword.escape', '!comment', '!string', '!keyword.nowdoc'],
37
                'factory' => new TokenFactory(ContextualToken::className()),
38
            ]),
39
40
            'string.double' => new Rule(new SubStringMatcher('"'), [
41
                'context' => ['!keyword.escape', '!comment', '!string'],
42
                'factory' => new TokenFactory(ContextualToken::className()),
43
            ]),
44
45
            'string.heredoc' => new Rule(new RegexMatcher('/<<<\s*(\w+)(?P<string>.*?)\n\1;/sm', ['string' => Token::NAME, 0 => 'keyword.heredoc']), ['context' => ['!comment']]),
46
            'string.nowdoc' => new Rule(new RegexMatcher('/<<<\s*\'(\w+)\'(?P<string>.*?)\n\1;/sm', ['string' => Token::NAME, 0 => 'keyword.nowdoc']), ['context' => ['!comment']]),
47
48
            'variable' => new Rule(new RegexMatcher('/[^\\\](\$[a-z_]\w*)/i'), [
49
                'context' => ['*comment.docblock', '!string.nowdoc', '!string.single', '!comment']
50
            ]),
51
            'variable.property' => new Rule(new RegexMatcher('/(?=(?:\w|\)|\])\s*->([a-z_]\w*))/i'), [
52
                'priority' => -2
53
            ]),
54
55
            'symbol.function' => new Rule(new RegexMatcher('/function\s+([a-z_]\w+)\s*\(/i')),
56
            'symbol.class' => [
57
                new Rule(new RegexMatcher('/(?:class|new|use|extends)\s+([\w\\\]+)/i')),
58
                new Rule(new RegexMatcher('/([\w\\\]+)::/i')),
59
                new Rule(new RegexMatcher('/@(?:var|property(?:-read|-write)?)\s+([^\$][\w\\\]+)/i'), ['context' => ['comment.docblock']]),
60
            ],
61
62
            'symbol.interface' => [
63
                new Rule(new RegexMatcher('/interface\s+([\w\\\]+)/i')),
64
                new Rule(new RegexMatcher('/implements\s+([\w\\\]+)(?:,\s*([\w\\\]+))*/i')),
65
            ],
66
67
            'keyword.escape' => new Rule(new RegexMatcher('/(\\\(?:x[0-9a-fA-F]{1,2}|u\{[0-9a-fA-F]{1,6}\}|[0-7]{1,3}|.))/i'), [
68
                'context' => ['string']
69
            ]),
70
71
            'comment' => new Rule(new CommentMatcher(['//', '#'], [
72
                '$.docblock' => ['/**', '*/'],
73
                ['/* ', '*/']
74
            ])),
75
76
            'keyword.annotation' => new Rule(new RegexMatcher('/[\s]+(@[\w-]+)/i'), [
77
                'context' => ['comment.docblock']
78
            ]),
79
80
            'call' => new Rule(new RegexMatcher('/([a-z_]\w*)\s*\(/i'), ['priority' => -1]),
81
82
            'constant' => new Rule(new WordMatcher(array_merge([
83
                '__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__',
84
                '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__',
85
            ], array_keys(get_defined_constants(true)["Core"]))), ['priority' => -2]),
86
            'constant.static' => new Rule(new RegexMatcher('/(?:[\w\\\]+::|const\s+)(\w+)/i'), ['priority' => -2]   ),
87
88
            'keyword' => new Rule(new WordMatcher([
89
                '__halt_compiler', 'abstract', 'and', 'array',
90
                'as', 'break', 'callable', 'case', 'catch',
91
                'class', 'clone', 'const', 'continue', 'declare',
92
                'default', 'die', 'do', 'echo', 'else', 'elseif',
93
                'empty', 'enddeclare', 'endfor', 'endforeach', 'endif',
94
                'endswitch', 'endwhile', 'eval', 'exit', 'extends',
95
                'final', 'finally', 'for', 'foreach', 'function',
96
                'global', 'goto', 'if', 'implements', 'include', 'include_once',
97
                'instanceof', 'insteadof', 'interface', 'isset', 'list',
98
                'namespace', 'new', 'or', 'print', 'private', 'protected',
99
                'public', 'require', 'require_once', 'return', 'static',
100
                'switch', 'throw', 'trait', 'try', 'unset', 'parent', 'self',
101
                'use', 'var', 'while', 'xor', 'yield'
102
            ]), ['context' => ['!string', '!variable', '!comment']]),
103
104
            'keyword.cast' => new Rule(
105
                new RegexMatcher('/(\((?:int|integer|bool|boolean|float|double|real|string|array|object|unset)\))/')
106
            ),
107
108
            'delimiter' => new Rule(new RegexMatcher('/(<\?php|<\?=|\?>)/')),
109
            'number' => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|\d+))/')),
110
111
            'operator.punctuation' => new Rule(new WordMatcher([',', ';'], ['separated' => false]), ['priority' => 0]),
112
        ];
113
    }
114
115
    public function getOpenClose() {
116
        return [
1 ignored issue
show
Bug Best Practice introduced by
The return type of return array(new \Kadet\...'language' => $this))); (array<Kadet\Highlighter\...ghter\Parser\CloseRule>) is incompatible with the return type of the parent method Kadet\Highlighter\Language\Language::getOpenClose of type Kadet\Highlighter\Parser\Rule.

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...
117
            new OpenRule(new RegexMatcher('/(<\?php|<\?=)/si'), [
118
                'factory'  => new TokenFactory(LanguageToken::className()),
119
                'priority' => 1000,
120
                'context'  => ['*'],
121
                'inject'   => $this,
122
                'language' => null
123
            ]),
124
            new CloseRule(new RegexMatcher('/(\?>|$)/'), [
125
                'context'  => ['!string', '!comment'],
126
                'priority' => 1000,
127
                'factory'  => new TokenFactory(LanguageToken::className()),
128
                'language' => $this
129
            ])
130
        ];
131
    }
132
133
    public function getIdentifier()
134
    {
135
        return 'php';
136
    }
137
}