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 declare(strict_types=1); |
||
| 22 | View Code Duplication | class MySQL implements ConnectableInterface |
|
|
|
|||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var string $dsn |
||
| 26 | */ |
||
| 27 | protected $dsn; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \PDO $connectionObject |
||
| 31 | */ |
||
| 32 | protected $connectionObject; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array loginData |
||
| 36 | */ |
||
| 37 | private $loginData = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Setter for the DSN string {@see $dsn} |
||
| 41 | * |
||
| 42 | * @param array $dsnArray |
||
| 43 | * @return void |
||
| 44 | */ |
||
| 45 | public function setDsn(array $dsnArray) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Establishes connection |
||
| 52 | * |
||
| 53 | * @param string $username |
||
| 54 | * @param string $password optional |
||
| 55 | * @throws \EmmetBlue\Core\Exception\SQLException |
||
| 56 | * @return void |
||
| 57 | */ |
||
| 58 | public function connect(string $username, string $password="") |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns an instance a pdo intance of the connection object |
||
| 89 | * |
||
| 90 | * @return \PDO |
||
| 91 | */ |
||
| 92 | public function getConnection() : \PDO |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Closes connection |
||
| 109 | */ |
||
| 110 | public function disableConnection() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Sets attributes for the PDO object |
||
| 117 | * |
||
| 118 | * @param \PDO $attribute |
||
| 119 | * @param \PDO $value |
||
| 120 | */ |
||
| 121 | public function setAttribute(\PDO $attribute, \PDO $value) |
||
| 125 | } |
||
| 126 |
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.