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 | ||
| 27 | class ConnectionConfigurator | ||
| 28 | { | ||
| 29 | protected $configuration; | ||
| 30 | |||
| 31 | /** | ||
| 32 | * __construct | ||
| 33 | * | ||
| 34 | * Initialize configuration. | ||
| 35 | * | ||
| 36 | * @param array $dsn | ||
| 37 | */ | ||
| 38 | public function __construct($dsn, $persist = false) | ||
| 49 | |||
| 50 | /** | ||
| 51 | * addConfiguration | ||
| 52 | * | ||
| 53 | * Add configuration settings. If settings exist, they are overridden. | ||
| 54 | * | ||
| 55 | * @param array $configuration | ||
| 56 | * @return Connection $this | ||
| 57 | */ | ||
| 58 | public function addConfiguration(array $configuration) | ||
| 70 | |||
| 71 | /** | ||
| 72 | * set | ||
| 73 | * | ||
| 74 | * Set a new configuration setting. | ||
| 75 | * | ||
| 76 | * @param string $name | ||
| 77 | * @param mixed $value | ||
| 78 | * @return ConnectionConfigurator $this | ||
| 79 | */ | ||
| 80 | public function set($name, $value) | ||
| 93 | |||
| 94 | |||
| 95 | /** | ||
| 96 | * parseDsn() | ||
| 97 | * | ||
| 98 | * Sets the different parameters from the DSN. | ||
| 99 | * | ||
| 100 | * @return Connection $this | ||
| 101 | * @throws ConnectionException | ||
| 102 | */ | ||
| 103 | private function parseDsn() | ||
| 169 | |||
| 170 | /** | ||
| 171 | * getConnectionString | ||
| 172 | * | ||
| 173 | * Return the connection string. | ||
| 174 | * | ||
| 175 | * @return string | ||
| 176 | */ | ||
| 177 | public function getConnectionString() | ||
| 202 | |||
| 203 | /** | ||
| 204 | * getPersist | ||
| 205 | * | ||
| 206 | * Return whether or not to persist the connection to the DB. | ||
| 207 | * | ||
| 208 | * @return bool | ||
| 209 | */ | ||
| 210 | public function getPersist() | ||
| 214 | |||
| 215 | /** | ||
| 216 | * getDefaultConfiguration | ||
| 217 | * | ||
| 218 | * Standalone, default configuration. | ||
| 219 | * | ||
| 220 | * @return array | ||
| 221 | */ | ||
| 222 | protected function getDefaultConfiguration() | ||
| 226 | |||
| 227 | /** | ||
| 228 | * getConfiguration | ||
| 229 | * | ||
| 230 | * Return current configuration settings. | ||
| 231 | * | ||
| 232 | * @return array | ||
| 233 | * @throws ConnectionException | ||
| 234 | */ | ||
| 235 | public function getConfiguration() | ||
| 250 | } | ||
| 251 |