Test Failed
Push — master ( 83eaad...7faa90 )
by Kacper
05:31
created

Php::setupRules()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 176
Code Lines 115

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 86
CRAP Score 3.0007

Importance

Changes 3
Bugs 3 Features 0
Metric Value
cc 3
eloc 115
c 3
b 3
f 0
nc 1
nop 0
dl 0
loc 176
ccs 86
cts 90
cp 0.9556
crap 3.0007
rs 8

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
declare(strict_types=1);
4
5
/**
6
 * Highlighter
7
 *
8
 * Copyright (C) 2016, Some right reserved.
9
 *
10
 * @author Kacper "Kadet" Donat <[email protected]>
11
 *
12
 * Contact with author:
13
 * Xmpp: [email protected]
14
 * E-mail: [email protected]
15
 *
16
 * From Kadet with love.
17
 */
18
19
namespace Kadet\Highlighter\Language;
20
21
use Kadet\Highlighter\Matcher\CommentMatcher;
22
use Kadet\Highlighter\Matcher\DelegateRegexMatcher;
23
use Kadet\Highlighter\Matcher\RegexMatcher;
24
use Kadet\Highlighter\Matcher\WordMatcher;
25
use Kadet\Highlighter\Parser\CloseRule;
26
use Kadet\Highlighter\Parser\OpenRule;
27
use Kadet\Highlighter\Parser\Rule;
28
use Kadet\Highlighter\Parser\Token\LanguageToken;
29
use Kadet\Highlighter\Parser\Token\MetaToken;
30
use Kadet\Highlighter\Parser\Token\Token;
31
use Kadet\Highlighter\Parser\TokenFactory;
32
use Kadet\Highlighter\Parser\TokenFactoryInterface;
33
34
class Php extends GreedyLanguage
35
{
36 2
    public function setupRules()
37
    {
38 1
        $allowedInAttributes = ['*none', '*meta.annotation', '!string', '!comment', '!symbol', '!variable'];
39
40 1
        $annotationNameRule = new Rule(new RegexMatcher('/([\w\\\\]+)(\((?:[^()]|(?2))*\))?/m'), [
41 1
            'name'     => 'symbol.annotation',
42
            'context'  => ['*'],
43 1
            'language' => $this,
44
        ]);
45
46 1
        $this->rules->addMany([
47 1
            'string' => CommonFeatures::strings(['single' => '\'', 'double' => '"'], [
48 1
                'context' => ['!operator.escape', '!comment', '!string', '!expression'],
49
            ]),
50
51 1
            'string.heredoc' => new Rule(new RegexMatcher(
52 1
                '/<<<\s*(\w+)\R(?P<string>.*?)\R\1;/sm',
53 1
                ['string' => Token::NAME, 0 => 'keyword.heredoc']
54 1
            ), ['context' => ['!comment']]),
55 1
            'string.nowdoc'  => new Rule(new RegexMatcher(
56 1
                '/<<<\s*\'(\w+)\'\R(?P<string>.*?)\R\1;/sm',
57 1
                ['string' => Token::NAME, 0 => 'keyword.nowdoc']
58 1
            ), ['context' => ['!comment']]),
59
60 1
            'variable'          => new Rule(new RegexMatcher('/(?:[^\\\]|^)(\$[a-z_]\w*)/i'), [
61 1
                'context' => ['*comment.docblock', '!string.nowdoc', '!string.single', '!comment'],
62
            ]),
63 1
            'variable.property' => new Rule(new RegexMatcher('/(?=(?:\w|\)|\])\s*->([a-z_]\w*))/i'), [
64 1
                'priority' => -2,
65
                'context'  => ['*comment.docblock', '!string.nowdoc', '!string.single', '!comment'],
66
            ]),
67
68 1
            'symbol.function' => new Rule(new RegexMatcher('/function\s+([a-z_]\w+)\s*\(/i')),
69
            'symbol.class'    => [
70 1
                new Rule(new RegexMatcher('/(?:class|new|use|extends)\s+([\w\\\]+)/i')),
71 1
                new Rule(new RegexMatcher('/([\w\\\]+)::/i'), ['context' => ['*meta.annotation', '*none']]),
72 1
                new Rule(
73 1
                    new RegexMatcher('/@(?:var|property(?:-read|-write)?|psalm-[\w-]+)(?:\s+|\s+\$\w+\s+)([^$][\w\\\]+)/i'),
74 1
                    ['context' => ['comment.docblock']]
75
                ),
76
            ],
77
78 1
            'expression.in-string' => new Rule(new RegexMatcher('/(?=(\{\$((?>[^${}]+|(?1))+)\}))/x'), [
79 1
                'context' => ['string'],
80 1
                'factory' => new TokenFactory(LanguageToken::class),
81 1
                'inject'  => $this,
82
            ]),
83
84
            'symbol.class.interface' => [
85 1
                new Rule(new RegexMatcher('/interface\s+([\w\\\]+)/i')),
86 1
                new Rule(new DelegateRegexMatcher(
87 1
                    '/implements\s+((?:[\w\\\]+)(?:,\s*([\w\\\]+))+)/i',
88 1
                    function ($match, TokenFactoryInterface $factory) {
89
                        foreach (preg_split('/,\s*/', $match[1][0], 0, PREG_SPLIT_OFFSET_CAPTURE) as $interface) {
90
                            yield $factory->create(Token::NAME, [
91
                                'pos'    => $match[1][1] + $interface[1],
92
                                'length' => strlen($interface[0]),
93
                            ]);
94
                        }
95 1
                    }
96
                )),
97
            ],
98
99
            'symbol.namespace' => [
100
                /*new Rule(new RegexMatcher('/(\\\{0,2}(?:\w+\\\{1,2})+)\w+/i'), [
101
                    'context' => ['*symbol', '*none']
102
                ]),*/
103
104 1
                new Rule(new RegexMatcher('/namespace\s*(\\\{0,2}(?:\w+\\\{1,2})+\w+);/i'), [
105 1
                    'context' => ['*symbol', '*none'],
106
                ]),
107
            ],
108
109
            'operator.escape' => [
110 1
                new Rule(new RegexMatcher('/(\\\(?:x[0-9a-fA-F]{1,2}|u\{[0-9a-fA-F]{1,6}\}|[0-7]{1,3}|[^\'\\\]))/i'), [
111 1
                    'context' => ['string.double', '!operator.escape'],
112
                ]),
113 1
                new Rule(new RegexMatcher('/(\\\[\'\\\])/i'), [
114 1
                    'context' => ['string', '!operator.escape'],
115
                ]),
116
            ],
117
118 1
            'comment' => new Rule(new CommentMatcher(['//', '#'], [
119 1
                '$.docblock' => ['/**', '*/'],
120
                ['/*', '*/'],
121 1
            ]), ['priority' => 4]),
122
123
            'symbol.annotation' => [
124 1
                new Rule(new RegexMatcher('/(?<=[*\s=,({])(@[\w\\\\-]+)/i'), [
125 1
                    'context' => ['comment.docblock'],
126
                ]),
127
            ],
128
129
            'meta.annotation' => [
130 1
                new Rule(
131 1
                    new DelegateRegexMatcher(
132 1
                        '/#(\[(?>[^\[\]]+|(?1))*\])/m',
133 2
                        function (
134
                            $match,
135
                            TokenFactoryInterface $factory
136
                        ) use (
137 1
                            $annotationNameRule,
138 1
                            $allowedInAttributes
0 ignored issues
show
Unused Code introduced by
The import $allowedInAttributes is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
139
                        ) {
140 1
                            yield $factory->create(
141 1
                                Token::NAME,
142
                                [
143 1
                                    'pos'    => $match[0][1],
144 1
                                    'length' => strlen($match[1][0]),
145
                                ]
146
                            );
147
148
                            // fixme: This should move to some helper? Maybe $offset argument for match?
149 1
                            foreach ($annotationNameRule->match($match[0][0]) as $token) {
150 1
                                $token->pos += $match[0][1];
151 1
                                $token->getEnd()->pos += $match[0][1];
152 1
                                yield $token;
153
                            }
154 2
                        }
155
                    ),
156
                    [
157 1
                        'factory'  => new TokenFactory(MetaToken::class),
158 1
                        'priority' => 10,
159
                    ]
160
                ),
161 1
                new Rule(new RegexMatcher('/(@[\w\\\\](\((?>[^\(\)]+|(?2))*\)))/m'), [
162 1
                    'factory'  => new TokenFactory(MetaToken::class),
163 1
                    'priority' => 10,
164
                    'context'  => ['comment.docblock'],
165
                ]),
166
            ],
167 1
            'call'            => new Rule(new RegexMatcher('/([a-z_]\w*)\s*\(/i'), ['priority' => -1]),
168
169 1
            'constant' => new Rule(new WordMatcher(array_merge([
170 1
                '__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__',
171
                '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__',
172 1
            ], array_keys(get_defined_constants(true)["Core"]))), ['priority' => -2]),
173 1
            'constant.static' => new Rule(new RegexMatcher('/(?:[\w\\\]+::|const\s+)(\w+)/i'), ['priority' => -2]),
174
175 1
            'keyword' => new Rule(new WordMatcher([
176 1
                '__halt_compiler', 'abstract', 'and',
177
                'as', 'break', 'case', 'catch',
178
                'class', 'clone', 'const', 'continue', 'declare',
179
                'default', 'die', 'do', 'echo', 'else', 'elseif',
180
                'empty', 'enddeclare', 'endfor', 'endforeach', 'endif',
181
                'endswitch', 'endwhile', 'enum', 'eval', 'exit', 'extends',
182
                'final', 'finally', 'for', 'foreach', 'function',
183
                'global', 'goto', 'if', 'implements', 'include', 'include_once',
184
                'instanceof', 'insteadof', 'interface', 'isset', 'list',
185
                'namespace', 'new', 'or', 'print', 'private', 'protected',
186
                'public', 'require', 'require_once', 'return', 'static',
187
                'switch', 'throw', 'trait', 'try', 'unset', 'parent', 'self',
188
                'use', 'var', 'while', 'xor', 'yield'
189 1
            ]), ['context' => ['!string', '!variable', '!comment']]),
190
191 1
            'symbol.type' => new Rule(new WordMatcher([
192 1
                'integer', 'float', 'string', 'boolean', 'false', 'never',
193
                'array', 'object', 'callable', 'void', 'mixed', 'true', 'null'
194 1
            ]), ['context' => ['!string', '!variable', '!comment', '!keyword']]),
195
196 1
            'keyword.cast' => new Rule(
197 1
                new RegexMatcher('/(\((?:int|integer|bool|boolean|float|double|real|string|array|object|unset)\))/')
198
            ),
199
200
            'label.argument' => [
201 1
                new Rule(new RegexMatcher('/(\w+):/'), ['context' => $allowedInAttributes]),
202
            ],
203
204 1
            'delimiter' => new Rule(new RegexMatcher('/(<\?php|<\?=|\?>)/')),
205 1
            'number'    => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|\d+))/'), [
206 1
                'context' => $allowedInAttributes,
207
            ]),
208
209 1
            'operator.punctuation' => new Rule(new WordMatcher([',', ';'], ['separated' => false]), [
210 1
                'priority' => 0,
211 1
                'context'  => $allowedInAttributes,
212
            ]),
213
        ]);
214 1
    }
215
216
    /** {@inheritdoc} */
217 6
    public function getEnds($embedded = false)
218
    {
219 6
        return $embedded ? [
220
            new OpenRule(new RegexMatcher('/(<\?php|<\?=)/si'), [
221
                'factory'  => new TokenFactory(LanguageToken::class),
222
                'priority' => 1000,
223
                'context'  => ['*'],
224
                'inject'   => $this,
225
                'language' => null,
226
            ]),
227
            new CloseRule(new RegexMatcher('/(\?>|$)/'), [
228
                'context'  => ['!string', '!comment'],
229
                'priority' => 1000,
230
                'factory'  => new TokenFactory(LanguageToken::class),
231
                'language' => $this,
232
            ]),
233 6
        ] : parent::getEnds(false);
234
    }
235
236 7
    public function getIdentifier()
237
    {
238 7
        return 'php';
239
    }
240
241
    public static function getMetadata()
242
    {
243
        return [
244
            'name'       => ['php'],
245
            'mime'       => ['text/x-php', 'application/x-php'],
246
            'extension'  => ['*.php', '*.phtml', '*.inc', '*.php?'],
247
            'injectable' => true,
248
        ];
249
    }
250
}
251