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