Passed
Push — master ( 1ee19c...3e5a10 )
by Kacper
02:56
created

Css::everywhere()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
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\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\OpenRule;
24
use Kadet\Highlighter\Parser\Rule;
25
use Kadet\Highlighter\Parser\Token\ContextualToken;
26
use Kadet\Highlighter\Parser\Token\MetaToken;
27
use Kadet\Highlighter\Parser\Token\Token;
28
use Kadet\Highlighter\Parser\TokenFactory;
29
use Kadet\Highlighter\Parser\Validator\Validator;
30
31
class Css extends Language
32
{
33
34
    /**
35
     * Tokenization rules
36
     *
37
     * @return \Kadet\Highlighter\Parser\Rule[]|\Kadet\Highlighter\Parser\Rule[][]
38
     */
39
    public function getRules()
40
    {
41
        $identifier = '[\w-]+';
42
        $at = [
43
            'charset', 'import', 'namespace',
44
            'media', 'supports', 'document', 'page', 'font-face', 'keyframes', 'viewport', 'counter-style',
45
            'font-feature-values', 'swash', 'ornaments', 'annotation', 'stylistic', 'styleset', 'character-variant'
46
        ];
47
48
        return [
49
            'meta.declaration' => [
50
                new OpenRule(new SubStringMatcher('{'), [
51
                    'context' => ['!meta.declaration.media', '!comment'],
52
                    'factory' => new TokenFactory(MetaToken::class)
53
                ]),
54
                new CloseRule(new SubStringMatcher('}')),
55
            ],
56
57
            'meta.declaration.media' => [
58
                new Rule(new RegexMatcher('/@media(.*?\{)/'), [
59
                    'context' => Validator::everywhere(),
60
                    'factory' => new TokenFactory(MetaToken::class)
61
                ]),
62
            ],
63
64
            'meta.declaration.rule' => [
65
                new OpenRule(new RegexMatcher('/@media.*(\()/'), [
66
                    'context' => ['meta.declaration.media'],
67
                    'factory' => new TokenFactory(MetaToken::class)
68
                ]),
69
                new CloseRule(new SubStringMatcher(')')),
70
            ],
71
72
            'keyword.at-rule' => new Rule(new RegexMatcher('/(@(?:'.implode('|', $at).'))/'), [
73
                'priority' => 2
74
            ]),
75
76
            'string.single' => new Rule(new SubStringMatcher('\''), [
77
                'context' => $this->everywhere(),
78
                'factory' => new TokenFactory(ContextualToken::class),
79
            ]),
80
81
            'string.double' => new Rule(new SubStringMatcher('"'), [
82
                'context' => $this->everywhere(),
83
                'factory' => new TokenFactory(ContextualToken::class),
84
            ]),
85
86
            'symbol.selector.id'    => new Rule(new RegexMatcher("/(#$identifier)/i")),
87
            'symbol.selector.tag'   => new Rule(new RegexMatcher('/(?>[\s}]|^)(?=(\w+)[^;]*\{)/ms')),
88
            'symbol.selector.class' => new Rule(new RegexMatcher("/(\\.$identifier)/i")),
89
90
            'symbol.selector.class.pseudo' => new Rule(new RegexMatcher("/(:{1,2}$identifier)/")),
91
92
            'number' => new Rule(new RegexMatcher("/([-+]?[0-9]*\\.?[0-9]+([\\w%]+)?)/"), [
93
                'context'  => ['meta.declaration', '!constant.color', '!comment', '!symbol', '!comment', '!string'],
94
                'priority' => 0
95
            ]),
96
            'constant.property' => new Rule(new RegexMatcher("/($identifier:)/"), [
97
                'context' => ['meta.declaration', '!symbol', '!comment'],
98
                'priority' => 0
99
            ]),
100
101
            'call' => new Rule(new RegexMatcher("/($identifier)\\s*\\((.*?)\\)/", [
102
                1 => Token::NAME,
103
                2 => 'string.argument'
104
            ]), [
105
                'context' => ['meta.declaration', '!comment', '!string']
106
            ]),
107
108
            'constant.color' => new Rule(new RegexMatcher("/(#[0-9a-f]{3,6})/i"), [
109
                'priority' => 2,
110
                'context'  => ['meta.declaration', '!symbol.color', '!comment']
111
            ]),
112
113
            'operator' => new Rule(new WordMatcher(['>', '+', '*', '!important'], ['separated' => false]), [
114
                'context' => $this->everywhere()
115
            ]),
116
117
            'operator.punctuation' => new Rule(new SubStringMatcher(';', ['separated' => false]), [
0 ignored issues
show
Unused Code introduced by
The call to SubStringMatcher::__construct() has too many arguments starting with array('separated' => false).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
118
                'context' => $this->everywhere()
119
            ]),
120
121
            'comment' => new Rule(new CommentMatcher([], [['/*', '*/']]), ['context' => $this->everywhere()])
122
        ];
123
    }
124
125
    /**
126
     * Unique language identifier, for example 'php'
127
     *
128
     * @return string
129
     */
130
    public function getIdentifier()
131
    {
132
        return 'css';
133
    }
134
135
    protected function everywhere() {
136
        static $validator;
137
        if (!$validator) {
138
            $validator = new Validator(['!string', '!comment']);
139
        }
140
141
        return $validator;
142
    }
143
}
144