Passed
Branch symfony-console (1f7bcb)
by Kacper
03:39
created

PowerShell   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 105
Duplicated Lines 7.62 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 11
Bugs 3 Features 0
Metric Value
c 11
b 3
f 0
dl 8
loc 105
rs 10
wmc 3
lcom 1
cbo 7

3 Methods

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