Completed
Push — master ( dc4b6d...04cf1c )
by Kacper
04:06
created

PowerShell   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 112
Duplicated Lines 7.14 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 93.42%

Importance

Changes 0
Metric Value
dl 8
loc 112
rs 10
c 0
b 0
f 0
ccs 71
cts 76
cp 0.9342
wmc 3
lcom 1
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
B setupRules() 0 90 1
A getIdentifier() 0 4 1
A getMetadata() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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