| Conditions | 1 |
| Paths | 1 |
| Total Lines | 84 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 39 | public function setupRules() |
||
| 40 | { |
||
| 41 | $identifier = '\w+'; |
||
| 42 | $number = '[+-]?(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?'; |
||
| 43 | |||
| 44 | $this->rules->addMany([ |
||
| 45 | 'comment' => new Rule(new CommentMatcher(['#'], [])), |
||
| 46 | |||
| 47 | 'keyword' => new Rule(new WordMatcher([ |
||
| 48 | 'case', 'continue', 'do', 'else', 'elsif', 'for', 'foreach', |
||
| 49 | 'if', 'last', 'my', 'next', 'our', 'redo', 'reset', 'then', |
||
| 50 | 'unless', 'until', 'while', 'use', 'print', 'new', 'BEGIN', |
||
| 51 | 'sub', 'CHECK', 'INIT', 'END', 'return', 'exit' |
||
| 52 | ])), |
||
| 53 | |||
| 54 | 'string.single' => new Rule(new SubStringMatcher('\''), [ |
||
| 55 | 'context' => ['!keyword', '!comment', '!string', '!language', '!number'], |
||
| 56 | 'factory' => new TokenFactory(ContextualToken::class), |
||
| 57 | ]), |
||
| 58 | |||
| 59 | 'string.double' => new Rule(new SubStringMatcher('"'), [ |
||
| 60 | 'context' => ['!keyword', '!comment', '!string', '!language', '!number'], |
||
| 61 | 'factory' => new TokenFactory(ContextualToken::class), |
||
| 62 | ]), |
||
| 63 | |||
| 64 | 'keyword.escape' => new Rule(new RegexMatcher('/(\\\.)/'), [ |
||
| 65 | 'context' => ['string'] |
||
| 66 | ]), |
||
| 67 | |||
| 68 | 'string.nowdoc' => new Rule( |
||
| 69 | new RegexMatcher('/<<\s*\'(\w+)\';(?P<string>.*?)\n\1/sm', [ |
||
| 70 | 'string' => Token::NAME, |
||
| 71 | 0 => 'keyword.nowdoc' |
||
| 72 | ]), ['context' => ['!comment']] |
||
| 73 | ), |
||
| 74 | |||
| 75 | 'language.shell' => new Rule(new SubStringMatcher('`'), [ |
||
| 76 | 'context' => ['!keyword.escape', '!comment', '!string', '!keyword.nowdoc'], |
||
| 77 | 'factory' => new TokenFactory(ContextualToken::class), |
||
| 78 | ]), |
||
| 79 | |||
| 80 | 'variable.scalar' => new Rule(new RegexMatcher("/(\\\$$identifier)/")), |
||
| 81 | 'variable.array' => new Rule(new RegexMatcher("/(\\@$identifier)/")), |
||
| 82 | 'variable.hash' => new Rule(new RegexMatcher("/(\\%$identifier)/")), |
||
| 83 | |||
| 84 | 'variable.property' => new Rule(new RegexMatcher("/\\\$$identifier{($identifier)}/")), |
||
| 85 | |||
| 86 | // Stupidly named var? Perl one, for sure. |
||
| 87 | 'variable.special' => new Rule(new RegexMatcher('/([$@%][^\s\w]+[\w]*)/')), |
||
| 88 | |||
| 89 | 'operator' => [ |
||
| 90 | new Rule(new RegexMatcher('/(-[rwxoRWXOezsfdlpSbctugkTBMAC])/')), |
||
| 91 | new Rule(new WordMatcher([ |
||
| 92 | 'not', 'and', 'or', 'xor', 'goto', 'last', 'next', 'redo', 'dump', |
||
| 93 | 'eq', 'ne', 'cmp', 'not', 'and', 'or', 'xor' |
||
| 94 | ], ['atomic' => true])), |
||
| 95 | ], |
||
| 96 | |||
| 97 | 'call' => new Rule(new RegexMatcher('/([a-z]\w+)(?:\s*\(|\s+[$%@"\'`{])/i')), |
||
| 98 | |||
| 99 | 'number' => [ |
||
| 100 | new Rule(new RegexMatcher("/(\\b|\"|')$number\\1/", [ |
||
| 101 | 0 => Token::NAME |
||
| 102 | ]), ['priority' => 5]), |
||
| 103 | ], |
||
| 104 | |||
| 105 | 'string.regex' => [ |
||
| 106 | new OpenRule(new RegexMatcher('#~\s*[ms]?(/).*?/#m'), [ |
||
| 107 | 'context' => Validator::everywhere() |
||
| 108 | ]), |
||
| 109 | new OpenRule(new RegexMatcher('#~\s*(s/).*?/#m')), |
||
| 110 | |||
| 111 | new Rule(new RegexMatcher('#(?=\/.*?(/[gimuy]{0,5}))#m'), [ |
||
| 112 | 'priority' => 1, |
||
| 113 | 'factory' => new TokenFactory(ContextualToken::class), |
||
| 114 | 'context' => ['!keyword.escape', 'string.regex'] |
||
| 115 | ]) |
||
| 116 | ], |
||
| 117 | |||
| 118 | 'symbol.iterator' => [ |
||
| 119 | new Rule(new RegexMatcher('#(<\w+>)#s')) |
||
| 120 | ] |
||
| 121 | ]); |
||
| 122 | } |
||
| 123 | |||
| 144 | } |