Completed
Push — master ( 6465ca...dc4b6d )
by Kacper
08:36
created

PowerShell::setupRules()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 83
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 61
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 51
nc 1
nop 0
dl 0
loc 83
ccs 61
cts 61
cp 1
crap 1
rs 8.7468
c 0
b 0
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
 * 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\WordMatcher;
21
use Kadet\Highlighter\Parser\Rule;
22
use Kadet\Highlighter\Parser\Token\Token;
23
24
class PowerShell extends GreedyLanguage
25
{
26
    
27
    /**
28
     * Tokenization rules
29
     */
30 1
    public function setupRules()
31
    {
32 1
        $this->rules->addMany([
33 1
            'string' => CommonFeatures::strings(['single' => '\'', 'double' => '"'], [
34 1
                'context' => ['!operator.escape', '!comment', '!string'],
35 1
            ]),
36
37
            'variable' => [
38 1
                new Rule(new RegexMatcher('/(?<!`)(\$(?P<namespace>\w+:)?[a-z_]\w*)/i'), [
39 1
                    'context'  => ['!string.single', '!comment', '!variable'],
40
                    'priority' => 0
41 1
                ]),
42 1
                new Rule(new RegexMatcher('/[^`](\$\{(?P<namespace>\w+:)?.*?\})/i'), [
43 1
                    'context'  => ['!string.single', '!comment', '!variable'],
44
                    'priority' => 0
45 1
                ]),
46 1
            ],
47
48 1
            'variable.splat' => new Rule(new RegexMatcher('/[^`](\@(?P<namespace>\w+:)?[a-z_]\w*)/i'), [
49 1
                'context'  => ['!string.single', '!comment'],
50
                'priority' => 0
51 1
            ]),
52
53 1
            'variable.special' => new Rule(new RegexMatcher('/(\$(?:\$|\^|\?|_|true|false|null))\b/i'), [
54 1
                'priority' => 5,
55 1
                'context'  => ['!string.single', '!comment']
56 1
            ]),
57
58 1
            'variable.scope' => new Rule(null, ['context' => ['*variable']]),
59
60 1
            'comment'             => new Rule(new CommentMatcher(['#'], [['<#', '#>']])),
61 1
            'keyword.doc-section' => new Rule(new RegexMatcher('/[\s\R](\.\w+)/i'), [
62 1
                'context' => ['comment']
63 1
            ]),
64
65 1
            'symbol.dotnet' => new Rule(new RegexMatcher('/(\[[a-z][\w\.]*(?:\[\])?\])/si')),
66
67 1
            'symbol.annotation' => new Rule(
68 1
                new RegexMatcher('/\[([\w\.]+)\s*(?P<arguments>\((?>[^()]+|(?&arguments))*\))\s*\]/si', [
69 1
                    1           => Token::NAME,
70
                    'arguments' => '$.arguments'
71 1
                ])
72 1
            ),
73
74 1
            'keyword' => new Rule(new WordMatcher([
75 1
                'Begin', 'Break', 'Catch', 'Continue', 'Data', 'Do', 'DynamicParam',
76 1
                'Else', 'Elseif', 'End', 'Exit', 'Filter', 'Finally', 'For', 'ForEach',
77 1
                'From', 'Function', 'If', 'In', 'InlineScript', 'Hidden', 'Parallel', 'Param',
78 1
                'Process', 'Return', 'Sequence', 'Switch', 'Throw', 'Trap', 'Try', 'Until', 'While', 'Workflow'
79 1
            ]), ['priority' => 3]),
80
81 1
            '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'), [
82 1
                'context' => ['!string', '!comment'],
83 1
            ]),
84
85 1
            'symbol.parameter' => new Rule(new RegexMatcher('/\s(-\w+:?)\b/i'), [
86 1
                'priority' => 0,
87 1
                'context'  => ['!string', '!comment', '!call']
88 1
            ]),
89
90 1
            'operator.punctuation' => new Rule(new WordMatcher([',', ';', '.', '::', '%'], ['separated' => false]), [
91 1
                'priority' => 0,
92 1
                'context'  => ['!string', '!comment', '!call']
93 1
            ]),
94
95
            'number' => [
96 1
                new Rule(new RegexMatcher('/\b(-?(?:0x[0-9a-f]+|\d+)l?(?:kb|mb|gb|tb|pb)?)\b/i'), [
97 1
                    'priority' => 0,
98 1
                    'context'  => ['!string', '!comment', '!variable', '!call']
99 1
                ]),
100 1
                new Rule(new RegexMatcher('/\b(-?(?>\d+)?\.\d+(?>d|l)(?>e(?:\+|-)?\d+)?(?:kb|mb|gb|tb|pb)?)\b/i'), [
101 1
                    'priority' => 0,
102 1
                    'context'  => ['!string', '!comment', '!variable', '!call']
103 1
                ])
104 1
            ],
105
106 1
            'call' => new Rule(new RegexMatcher(
107
                '/(?<![^`]`)(?<=\n|\{|\(|\}|\||=|;|^|function|filter|^PS>)\s*((?:\w+\\\)?\w[\w-\.]+)/im'
108 1
            ), ['priority' => 2]),
109
110 1
            'delimiter.prompt' => new Rule(new RegexMatcher('/^(PS>)/im'), ['priority' => 4]),
111 1
        ]);
112 1
    }
113
114
    /** {@inheritdoc} */
115 1
    public function getIdentifier()
116
    {
117 1
        return 'PowerShell';
118
    }
119
120 View Code Duplication
    public static function getMetadata()
121
    {
122
        return [
123
            'name'      => ['powershell', 'posh'],
124
            'mime'      => ['text/x-powershell', 'application/x-powershell'],
125
            'extension' => ['*.ps1', '*.psm1', '*.psd1']
126
        ];
127
    }
128
}
129