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 |
||
| 8 | class String_ extends Scalar |
||
| 9 | { |
||
| 10 | /** @var string String value */ |
||
| 11 | public $value; |
||
| 12 | |||
| 13 | protected static $replacements = array( |
||
| 14 | '\\' => '\\', |
||
| 15 | '$' => '$', |
||
| 16 | 'n' => "\n", |
||
| 17 | 'r' => "\r", |
||
| 18 | 't' => "\t", |
||
| 19 | 'f' => "\f", |
||
| 20 | 'v' => "\v", |
||
| 21 | 'e' => "\x1B", |
||
| 22 | ); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Constructs a string scalar node. |
||
| 26 | * |
||
| 27 | * @param string $value Value of the string |
||
| 28 | * @param array $attributes Additional attributes |
||
| 29 | */ |
||
| 30 | public function __construct($value, array $attributes = array()) { |
||
| 34 | |||
| 35 | public function getSubNodeNames() { |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @internal |
||
| 41 | * |
||
| 42 | * Parses a string token. |
||
| 43 | * |
||
| 44 | * @param string $str String token content |
||
| 45 | * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes |
||
| 46 | * |
||
| 47 | * @return string The parsed string |
||
| 48 | */ |
||
| 49 | public static function parse($str, $parseUnicodeEscape = true) { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @internal |
||
| 70 | * |
||
| 71 | * Parses escape sequences in strings (all string types apart from single quoted). |
||
| 72 | * |
||
| 73 | * @param string $str String without quotes |
||
| 74 | * @param null|string $quote Quote type |
||
| 75 | * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes |
||
| 76 | * |
||
| 77 | * @return string String with escape sequences parsed |
||
| 78 | */ |
||
| 79 | public static function parseEscapeSequences($str, $quote, $parseUnicodeEscape = true) { |
||
| 107 | |||
| 108 | private static function codePointToUtf8($num) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @internal |
||
| 127 | * |
||
| 128 | * Parses a constant doc string. |
||
| 129 | * |
||
| 130 | * @param string $startToken Doc string start token content (<<<SMTHG) |
||
| 131 | * @param string $str String token content |
||
| 132 | * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes |
||
| 133 | * |
||
| 134 | * @return string Parsed string |
||
| 135 | */ |
||
| 136 | public static function parseDocString($startToken, $str, $parseUnicodeEscape = true) { |
||
| 147 | } |
||
| 148 |
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.