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 |
||
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() |
|
128 | } |
||
129 |