| Conditions | 1 |
| Paths | 1 |
| Total Lines | 91 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 37 | public function setupRules() |
||
| 38 | { |
||
| 39 | $standard = new Validator(['!string', '!comment']); |
||
| 40 | |||
| 41 | $this->rules->addMany([ |
||
| 42 | 'keyword' => new Rule(new WordMatcher([ |
||
| 43 | 'and', 'del', 'from', 'not', 'while', 'as', 'elif', 'global', 'or', 'with', 'assert', 'else', 'if', |
||
| 44 | 'pass', 'yield', 'break', 'except', 'import', 'print', 'class', 'exec', 'in', 'raise', 'continue', |
||
| 45 | 'finally', 'is', 'return', 'def', 'for', 'lambda', 'try', |
||
| 46 | ])), |
||
| 47 | |||
| 48 | 'operator' => new Rule( |
||
| 49 | new RegexMatcher('/([-+%=]=?|!=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~])|\b(or|and|not)\b/'), [ |
||
| 50 | 'priority' => -1 |
||
| 51 | ] |
||
| 52 | ), |
||
| 53 | |||
| 54 | 'expression' => new Rule(new RegexMatcher('/\{(\S+)\}/'), [ |
||
| 55 | 'context' => ['string'] |
||
| 56 | ]), |
||
| 57 | |||
| 58 | 'variable' => [ |
||
| 59 | new Rule(new RegexMatcher('/[^\w.]([a-z_]\w*)\.\w/i')), |
||
| 60 | 'property' => new Rule(new RegexMatcher('/(?=(?:\w|\)|\])\s*\.([a-z_]\w*))/i'), [ |
||
| 61 | 'priority' => -2, |
||
| 62 | 'context' => ['*none', '*expression'] |
||
| 63 | ]), |
||
| 64 | ], |
||
| 65 | |||
| 66 | 'symbol' => [ |
||
| 67 | new Rule(new RegexMatcher('/import\s+([a-z_][\w.]*)(?:\s*,\s*([a-z_][\w.]*))*/i', [ |
||
| 68 | 1 => Token::NAME, |
||
| 69 | 2 => Token::NAME, |
||
| 70 | ])), |
||
| 71 | 'library' => new Rule(new RegexMatcher('/from\s+([a-z_][\w.]*)\s+import/i', [ |
||
| 72 | 1 => Token::NAME, |
||
| 73 | ])) |
||
| 74 | ], |
||
| 75 | |||
| 76 | 'keyword.escape' => new Rule(new RegexMatcher('/(\\\(?:.|[0-7]{3}|x\x{2}))/'), [ |
||
| 77 | 'context' => Validator::everywhere() |
||
| 78 | ]), |
||
| 79 | |||
| 80 | 'comment' => new Rule(new CommentMatcher(['#'])), |
||
| 81 | 'constant.special' => [ |
||
| 82 | new Rule(new WordMatcher(['True', 'False', 'NotImplemented', 'Ellipsis'], [ |
||
| 83 | 'case-sensitivity' => true |
||
| 84 | ])), |
||
| 85 | new Rule(new RegexMatcher('/\b(__\w+__)\b/')) |
||
| 86 | ], |
||
| 87 | 'call' => new Rule(new RegexMatcher('/([a-z_]\w*)\s*\(/i'), ['priority' => 2]), |
||
| 88 | |||
| 89 | 'meta.newline' => new CloseRule(new RegexMatcher('/()\r?\n/'), [ |
||
| 90 | 'factory' => new TokenFactory(TerminatorToken::class), |
||
| 91 | 'context' => ['!keyword.escape', 'string.single-line'], |
||
| 92 | 'closes' => ['string.single-line.double', 'string.single-line.single'] |
||
| 93 | ]), |
||
| 94 | |||
| 95 | 'number' => new Rule( |
||
| 96 | new RegexMatcher('/(-?(?:0[bo])?(?:(?:\d|0x[\da-f])[\da-f]*\.?\d*|\.\d+)(?:e[+-]?\d+)?j?)\b/') |
||
| 97 | ), |
||
| 98 | |||
| 99 | 'string' => [ |
||
| 100 | 'single-line' => [ |
||
| 101 | 'double' => new Rule(new RegexMatcher('/(?:^|[^"])(")(?=[^"]|$)/'), [ |
||
| 102 | 'factory' => new TokenFactory(ContextualToken::class), |
||
| 103 | 'context' => $standard, |
||
| 104 | ]), |
||
| 105 | 'single' => new Rule(new RegexMatcher('/(?:^|[^\'])(\')(?=[^\']|$)/'), [ |
||
| 106 | 'factory' => new TokenFactory(ContextualToken::class), |
||
| 107 | 'context' => $standard, |
||
| 108 | ]), |
||
| 109 | ], |
||
| 110 | 'multi-line' => [ |
||
| 111 | 'double' => new Rule(new SubStringMatcher('"""'), [ |
||
| 112 | 'factory' => new TokenFactory(ContextualToken::class), |
||
| 113 | 'context' => $standard, |
||
| 114 | 'priority' => 2, |
||
| 115 | ]), |
||
| 116 | 'single' => new Rule(new SubStringMatcher('\'\'\''), [ |
||
| 117 | 'factory' => new TokenFactory(ContextualToken::class), |
||
| 118 | 'context' => $standard, |
||
| 119 | 'priority' => 2, |
||
| 120 | ]), |
||
| 121 | ] |
||
| 122 | ], |
||
| 123 | |||
| 124 | 'symbol.function' => new Rule(new RegexMatcher('/def\s+([a-z_]\w+)\s*\(/i')), |
||
| 125 | 'symbol.class' => new Rule(new RegexMatcher('/class\s+([a-z_]\w+)/i')), |
||
| 126 | ]); |
||
| 127 | } |
||
| 128 | |||
| 139 |