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 |
||
| 9 | class BinLogServerInfo |
||
| 10 | { |
||
| 11 | const MYSQL_VERSION_MARIADB = 'MariaDB'; |
||
| 12 | const MYSQL_VERSION_PERCONA = 'Percona'; |
||
| 13 | const MYSQL_VERSION_GENERIC = 'MySQL'; |
||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | private static $serverInfo = []; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @param string $data |
||
| 21 | * @param string $version |
||
| 22 | */ |
||
| 23 | 54 | public static function parsePackage($data, $version) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | 54 | public static function getSalt() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @see http://stackoverflow.com/questions/37317869/determine-if-mysql-or-percona-or-mariadb |
||
| 103 | * @param string $version |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | 54 | private static function parseVersion($version) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | public static function getVersion() |
||
| 124 | { |
||
| 125 | return self::$serverInfo['version_name']; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | public static function isMariaDb() |
||
| 132 | { |
||
| 133 | return self::MYSQL_VERSION_MARIADB === self::getVersion(); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | public static function isPercona() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public static function isGeneric() |
||
| 151 | } |
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.