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 |
||
| 13 | class MetaCharacters |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @const Bit value that indicates whether a meta character represents a single character |
||
| 17 | */ |
||
| 18 | const IS_CHAR = 1; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @const Bit value that indicates whether a meta character represents a quantifiable expression |
||
| 22 | */ |
||
| 23 | const IS_QUANTIFIABLE = 2; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array Map of meta values and the expression they represent |
||
| 27 | */ |
||
| 28 | protected $exprs = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var InputInterface |
||
| 32 | */ |
||
| 33 | protected $input; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array Map of meta characters' codepoints and their value |
||
| 37 | */ |
||
| 38 | protected $meta = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param InputInterface $input |
||
| 42 | */ |
||
| 43 | 11 | public function __construct(InputInterface $input) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Add a meta character to the list |
||
| 50 | * |
||
| 51 | * @param string $char Meta character |
||
| 52 | * @param string $expr Regular expression |
||
| 53 | * @return void |
||
| 54 | */ |
||
| 55 | 11 | public function add($char, $expr) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Get the expression associated with a meta value |
||
| 76 | * |
||
| 77 | * @param integer $metaValue |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getExpression($metaValue) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get the value set for a meta character |
||
| 92 | * |
||
| 93 | * @param string $char |
||
| 94 | * @return integer |
||
| 95 | */ |
||
| 96 | public function getValue($char) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Return whether a given value represents a single character |
||
| 108 | * |
||
| 109 | * @param integer $value |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | 9 | public static function isChar($value) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Return whether a given value represents a quantifiable expression |
||
| 119 | * |
||
| 120 | * @param integer $value |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | 9 | public static function isQuantifiable($value) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Return whether a given character is a meta character |
||
| 130 | * |
||
| 131 | * @param string $char |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | public function isMeta($char) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * |
||
| 141 | * |
||
| 142 | * @param array[] $strings |
||
| 143 | * @return array[] |
||
| 144 | */ |
||
| 145 | 9 | public function replaceMeta(array $strings) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Compute and return a value for given expression |
||
| 165 | * |
||
| 166 | * Values are meant to be a unique negative integer. The last 2 bits indicate whether the |
||
| 167 | * expression is quantifiable and/or represents a single character. |
||
| 168 | * |
||
| 169 | * @param string $expr Regular expression |
||
| 170 | * @return integer |
||
| 171 | */ |
||
| 172 | 9 | protected function computeValue($expr) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Test whether given expression represents a single character usable in a character class |
||
| 189 | * |
||
| 190 | * @param string $expr |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | 9 | protected function exprIsChar($expr) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Test whether given expression is quantifiable |
||
| 224 | * |
||
| 225 | * @param string $expr |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | 9 | protected function exprIsQuantifiable($expr) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Validate a meta character |
||
| 235 | * |
||
| 236 | * @param string $char |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | protected function validateChar($char) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Validate a regular expression |
||
| 250 | * |
||
| 251 | * @param string $expr |
||
| 252 | * @return void |
||
| 253 | */ |
||
| 254 | protected function validateExpr($expr) |
||
| 261 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.