Passed
Branch master (f3f280)
by Maciej
03:18
created

Css::setupRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 86
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 51
nc 1
nop 0
dl 0
loc 86
ccs 41
cts 41
cp 1
crap 1
rs 9.069
c 1
b 1
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Highlighter
5
 *
6
 * Copyright (C) 2016, Some right reserved.
7
 *
8
 * @author Kacper "Kadet" Donat <[email protected]>
9
 *
10
 * Contact with author:
11
 * Xmpp: [email protected]
12
 * E-mail: [email protected]
13
 *
14
 * From Kadet with love.
15
 */
16
17
namespace Kadet\Highlighter\Language;
18
19
use Kadet\Highlighter\Matcher\CommentMatcher;
20
use Kadet\Highlighter\Matcher\RegexMatcher;
21
use Kadet\Highlighter\Matcher\SubStringMatcher;
22
use Kadet\Highlighter\Matcher\WordMatcher;
23
use Kadet\Highlighter\Parser\CloseRule;
24
use Kadet\Highlighter\Parser\OpenRule;
25
use Kadet\Highlighter\Parser\Rule;
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 GreedyLanguage
32
{
33
34
    /**
35
     * Tokenization rules
36
     */
37 2
    public function setupRules()
38
    {
39 2
        $identifier = '-?[_a-zA-Z]+[_a-zA-Z0-9-]*';
40
        $at = [
41 2
            'charset', 'import', 'namespace',
42
            'media', 'supports', 'document', 'page', 'font-face', 'keyframes', 'viewport', 'counter-style',
43
            'font-feature-values', 'swash', 'ornaments', 'annotation', 'stylistic', 'styleset', 'character-variant'
44
        ];
45
        
46 2
        $this->rules->addMany([
47 2
            'string' => CommonFeatures::strings(['single' => '\'', 'double' => '"'], [
48 2
                'context' => $this->everywhere()
49
            ]),
50
51
            'meta.declaration' => [
52 2
                new OpenRule(new SubStringMatcher('{'), [
53 2
                    'context' => ['!meta.declaration.media', '!comment'],
54 2
                    'factory' => new TokenFactory(MetaToken::class)
55
                ]),
56 2
                new CloseRule(new SubStringMatcher('}')),
57
            ],
58
59
            'meta.declaration.media' => [
60 2
                new Rule(new RegexMatcher('/@media(.*?\{)/'), [
61 2
                    'context' => Validator::everywhere(),
62 2
                    'factory' => new TokenFactory(MetaToken::class)
63
                ]),
64
            ],
65
66
            'meta.declaration.rule' => [
67 2
                new OpenRule(new RegexMatcher('/@media.*(\()/'), [
68 2
                    'context' => ['meta.declaration.media'],
69 2
                    'factory' => new TokenFactory(MetaToken::class)
70
                ]),
71 2
                new CloseRule(new SubStringMatcher(')')),
72
            ],
73
74 2
            'keyword.at-rule' => new Rule(new RegexMatcher('/(@(?:-[a-z]+-)?(?:' . implode('|', $at) . '))/'), [
75 2
                'priority' => 2
76
            ]),
77
78 2
            'symbol.selector.id'    => new Rule(new RegexMatcher("/(#$identifier)/i")),
79 2
            'symbol.selector.tag'   => new Rule(new RegexMatcher('/(?>[\s}]|^)(?=(\w+)[^;]*\{)/ms')),
80 2
            'symbol.selector.class' => new Rule(new RegexMatcher("/(\\.$identifier)/i")),
81
82 2
            'symbol.selector.class.pseudo' => new Rule(new RegexMatcher("/(:{1,2}$identifier)/")),
83
84 2
            'number' => new Rule(new RegexMatcher("/([-+]?[0-9]*\\.?[0-9]+([\\w%]+)?)/"), [
85 2
                'context'  => ['meta.declaration', '!constant.color', '!comment', '!symbol', '!comment', '!string'],
86
                'priority' => 0
87
            ]),
88
            
89 2
            'symbol.property' => new Rule(new RegexMatcher("/($identifier:)/"), [
90 2
                'context' => ['meta.declaration', '!symbol', '!comment'],
91
                'priority' => 2
92
            ]),
93
94 2
            'call' => new Rule(new RegexMatcher("/($identifier)\\s*\\((?:(?P<string>[a-z].*?)|.*?)\\)/", [
95 2
                1 => Token::NAME,
96 2
                'string' => 'string.argument'
97
            ]), [
98 2
                'context' => ['meta.declaration', '!comment', '!string', '!keyword']
99
            ]),
100
101
            'constant.color' => [
102 2
                new Rule(new RegexMatcher("/(#[0-9a-f]{3,6})/i"), [
103 2
                    'priority' => 2,
104
                    'context'  => ['meta.declaration', '!comment']
105
                ]),
106 2
                new Rule(new WordMatcher([
107 2
                    'white', 'silver', 'gray', 'black', 'red', 'maroon', 'yellow', 'olive',
108
                    'lime', 'green', 'aqua', 'teal', 'blue', 'navy', 'fuchsia', 'purple'
109
                ]), [
110 2
                    'context'  => ['meta.declaration', '!comment', '!symbol', '!variable']
111
                ]),
112
            ],
113
114 2
            'operator' => new Rule(new WordMatcher(['>', '+', '*', '!important'], ['separated' => false]), [
115 2
                'context' => $this->everywhere()
116
            ]),
117
118 2
            'operator.punctuation' => new Rule(new SubStringMatcher(';'), [
119 2
                'context' => $this->everywhere()
120
            ]),
121
122 2
            'comment' => new Rule(new CommentMatcher([], [['/*', '*/']]), ['context' => $this->everywhere()])
123
        ]);
124 2
    }
125
126
    /**
127
     * Unique language identifier, for example 'php'
128
     *
129
     * @return string
130
     */
131 3
    public function getIdentifier()
132
    {
133 3
        return 'css';
134
    }
135
136 2
    protected function everywhere()
137
    {
138 2
        static $validator;
139 2
        if (!$validator) {
140
            $validator = new Validator(['!string', '!comment']);
141
        }
142
143 2
        return $validator;
144
    }
145
146
    public static function getMetadata()
147
    {
148
        return [
149
            'name'      => ['css'],
150
            'mime'      => ['text/css'],
151
            'extension' => ['*.css']
152
        ];
153
    }
154
}
155