Passed
Push — master ( ad0973...f9c297 )
by Kacper
02:48
created

PowerShell::setupRules()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 85
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 85
rs 8.6875
cc 1
eloc 52
nc 1
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
 *
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\Token\ContextualToken;
23
use Kadet\Highlighter\Parser\Rule;
24
use Kadet\Highlighter\Parser\Token\Token;
25
use Kadet\Highlighter\Parser\TokenFactory;
26
27
class PowerShell extends Language
28
{
29
    /**
30
     * Tokenization rules
31
     *
32
     * @return \Kadet\Highlighter\Parser\Rule[]|\Kadet\Highlighter\Parser\Rule[][]
33
     */
34
    public function setupRules()
35
    {
36
        $this->rules->addMany([
37
            'string.single' => new Rule(new SubStringMatcher('\''), [
38
                'context' => ['!keyword.escape', '!comment', '!string'],
39
                'factory' => new TokenFactory(ContextualToken::class),
40
            ]),
41
42
            'string.double' => new Rule(new SubStringMatcher('"'), [
43
                'context' => ['!keyword.escape', '!comment', '!string'],
44
                'factory' => new TokenFactory(ContextualToken::class),
45
            ]),
46
47
            'variable' => [
48
                new Rule(new RegexMatcher('/(?<!`)(\$(?P<namespace>\w+:)?[a-z_]\w*)/i'), [
49
                    'context'  => ['!string.single', '!comment', '!variable'],
50
                    'priority' => 0
51
                ]),
52
                new Rule(new RegexMatcher('/[^`](\$\{(?P<namespace>\w+:)?.*?\})/i'), [
53
                    'context'  => ['!string.single', '!comment', '!variable'],
54
                    'priority' => 0
55
                ]),
56
            ],
57
58
            'variable.splat' => new Rule(new RegexMatcher('/[^`](\@(?P<namespace>\w+:)?[a-z_]\w*)/i'), [
59
                'context'  => ['!string.single', '!comment'],
60
                'priority' => 0
61
            ]),
62
63
            'variable.special' => new Rule(new RegexMatcher('/(\$(?:\$|\^|\?|_|true|false|null))\b/i'), [
64
                'priority' => 5,
65
                'context'  => ['!string.single', '!comment']
66
            ]),
67
68
            'variable.scope' => new Rule(null, ['context' => ['*variable']]),
69
70
            'comment'             => new Rule(new CommentMatcher(['#'], [['<#', '#>']])),
71
            'keyword.doc-section' => new Rule(new RegexMatcher('/[\s\n](\.\w+)/i'), [
72
                'context' => ['comment']
73
            ]),
74
75
            'symbol.dotnet' => new Rule(new RegexMatcher('/(\[[a-z][\w\.]*(?:\[\])?\])/si')),
76
77
            'annotation' => new Rule(
78
                new RegexMatcher('/\[([\w\.]+)\s*(?P<arguments>\((?>[^()]+|(?&arguments))*\))\s*\]/si', [
79
                    1           => Token::NAME,
80
                    'arguments' => '$.arguments'
81
                ])
82
            ),
83
84
            'keyword' => new Rule(new WordMatcher([
85
                'Begin', 'Break', 'Catch', 'Continue', 'Data', 'Do', 'DynamicParam',
86
                'Else', 'Elseif', 'End', 'Exit', 'Filter', 'Finally', 'For', 'ForEach',
87
                'From', 'Function', 'If', 'In', 'InlineScript', 'Hidden', 'Parallel', 'Param',
88
                'Process', 'Return', 'Sequence', 'Switch', 'Throw', 'Trap', 'Try', 'Until', 'While', 'Workflow'
89
            ]), ['priority' => 3]),
90
91
            'operator' => new Rule(new RegexMatcher('/(&|\-eq|\-ne|\-gt|\-ge|\-lt|\-le|\-ieq|\-ine|\-igt|\-ige|\-ilt|\-ile|\-ceq|\-cne|\-cgt|\-cge|\-clt|\-cle|\-like|\-notlike|\-match|\-notmatch|\-ilike|\-inotlike|\-imatch|\-inotmatch|\-clike|\-cnotlike|\-cmatch|\-cnotmatch|\-contains|\-notcontains|\-icontains|\-inotcontains|\-ccontains|\-cnotcontains|\-isnot|\-is|\-as|\-replace|\-ireplace|\-creplace|\-and|\-or|\-band|\-bor|\-not|\-bnot|\-f|\-casesensitive|\-exact|\-file|\-regex|\-wildcard)\b/i'), [
92
                'context' => ['!string', '!comment'],
93
            ]),
94
95
            'parameter' => new Rule(new RegexMatcher('/\s(-\w+:?)\b/i'), [
96
                'priority' => 0,
97
                'context'  => ['!string', '!comment', '!call']
98
            ]),
99
100
            'operator.punctuation' => new Rule(new WordMatcher([',', ';', '.', '::', '%'], ['separated' => false]), [
101
                'priority' => 0,
102
                'context'  => ['!string', '!comment', '!call']
103
            ]),
104
105
            'number' => [
106
                new Rule(new RegexMatcher('/\b(-?(?:0x[0-9a-f]+|\d+)l?(?:kb|mb|gb|tb|pb)?)\b/i'), [
107
                    'priority' => 0,
108
                    'context'  => ['!string', '!comment', '!variable', '!call']
109
                ]),
110
                new Rule(new RegexMatcher('/\b(-?(?>\d+)?\.\d+(?>d|l)(?>e(?:\+|-)?\d+)?(?:kb|mb|gb|tb|pb)?)\b/i'), [
111
                    'priority' => 0,
112
                    'context'  => ['!string', '!comment', '!variable', '!call']
113
                ])
114
            ],
115
116
            'call' => new Rule(new RegexMatcher('/(?<![^`]`)(?<=\n|\{|\(|\}|\||=|;|^|function|filter)\s*((?:\w+\\\)?\w[\w-\.]+)/i'), ['priority' => 2])
117
        ]);
118
    }
119
120
    /** {@inheritdoc} */
121
    public function getIdentifier()
122
    {
123
        return 'PowerShell';
124
    }
125
}
126