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