Passed
Branch fuck-54 (abba45)
by Kacper
02:24
created

JavaScript::getRules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 61
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 61
rs 9.5147
cc 2
eloc 37
nc 2
nop 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
 * 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\ContextualToken;
23
use Kadet\Highlighter\Parser\OpenRule;
24
use Kadet\Highlighter\Parser\Rule;
25
use Kadet\Highlighter\Parser\TokenFactory;
26
27
/**
28
 * Class JavaScriptLanguage
29
 *
30
 * @package Kadet\Highlighter\Language
31
 *
32
 * @property bool $variables
33
 */
34
class JavaScript extends Language
35
{
36
    protected $_options = [
37
        'variables' => false,
38
    ];
39
40
    const IDENTIFIER = '[\p{L}\p{Nl}$_][\p{L}\p{Nl}$\p{Mn}\p{Mc}\p{Nd}\p{Pc}]*';
41
42
    public function getRules()
43
    {
44
        $rules = [
45
            'string.single' => new Rule(new SubStringMatcher('\''), [
46
                'context' => ['!keyword.escape', '!comment', '!string', '!keyword.nowdoc'],
47
                'factory' => new TokenFactory(ContextualToken::class),
48
            ]),
49
50
            'string.double' => new Rule(new SubStringMatcher('"'), [
51
                'context' => ['!keyword.escape', '!comment', '!string'],
52
                'factory' => new TokenFactory(ContextualToken::class),
53
            ]),
54
            'variable.property' => new Rule(new RegexMatcher('/(?=(?:\w|\)|\])\s*\.([a-z_]\w*))/i'), [
55
                'priority' => -2
56
            ]),
57
        ];
58
59
        if($this->variables) {
60
            $rules = array_merge($rules, [
61
                'variable' => new Rule(new RegexMatcher('/(' . self::IDENTIFIER . ')/iu'), ['priority' => -10000]),
62
            ]);
63
        }
64
65
        $rules = array_merge($rules, [
66
            'symbol.function' => new Rule(new RegexMatcher('/function\s+([a-z_]\w+)\s*\(/i')),
67
68
            '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'), [
69
                'context' => ['string']
70
            ]),
71
72
            'comment' => new Rule(new CommentMatcher(['//'], [
73
                ['/*', '*/']
74
            ])),
75
76
            'call' => new Rule(new RegexMatcher('/(' . self::IDENTIFIER . ')\s*\(/iu'), ['priority' => -1]),
77
78
            'keyword' => new Rule(new WordMatcher([
79
                'do', 'if', 'in', 'for', 'let', 'new', 'try', 'var', 'case', 'else', 'enum', 'eval', 'false',
80
                'null', 'this', 'true', 'void', 'with', 'break', 'catch', 'class', 'const', 'super', 'throw',
81
                'while', 'yield', 'delete', 'export', 'import', 'public', 'return', 'static', 'switch',
82
                'typeof', 'default', 'extends', 'finally', 'package', 'private', 'continue', 'debugger',
83
                'function', 'arguments', 'interface', 'protected', 'implements', 'instanceof',
84
            ]), ['context' => ['!string', '!comment']]),
85
86
            'number' => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|\d+))/')),
87
88
            'operator.punctuation' => new Rule(new WordMatcher([',', ';'], ['separated' => false]), ['priority' => 0]),
89
            'operator' => new Rule(new RegexMatcher('/(\+{1,2}|-{1,2}|={1,3}|\|{1,2}|&{1,2})/'), ['priority' => 0]),
90
91
            'string.regex' => [
92
                new OpenRule(new RegexMatcher('#(?>[\[=(?:+,!]|^|return|=>|&&|\|\|)\s*(/).*?/#m')),
93
                new Rule(new RegexMatcher('#\/.*(/[gimuy]{0,5})#m'), [
94
                    'priority' => 1,
95
                    'factory' => new TokenFactory(ContextualToken::class),
96
                    'context' => ['!keyword.escape', 'string.regex']
97
                ])
98
            ]
99
        ]);
100
101
        return $rules;
102
    }
103
104
    public function getIdentifier()
105
    {
106
        return 'javascript';
107
    }
108
}