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 | abstract class Connection |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * $_database The database name. |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | protected $_database; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * $_host The host name. |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $_host; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * $_password The password to the database server. |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $_password; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * $_pdo The PDO instance of the connection. |
||
| 31 | * @var PDO |
||
| 32 | */ |
||
| 33 | protected $_pdo; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * $_port The port number to the database server. |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $_port; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The username to the database server. |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $_username; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The Connection constructor |
||
| 49 | */ |
||
| 50 | public function __construct() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The method called in the constructor. |
||
| 57 | * @return void |
||
| 58 | */ |
||
| 59 | abstract protected function connect(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Returns the Connection's PDO. |
||
| 63 | * @return PDO PHP Data Objects |
||
| 64 | */ |
||
| 65 | public function getPdo() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Loads variables in the .env file. |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | protected function loadDbEnv() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Loads variables in the .env file and handles exceptions. |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | protected function useDbEnv() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Creates a record in the database. |
||
| 105 | * @param string $table The table where the a new record is made. |
||
| 106 | * @param array $record The record to be made in the database. |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | public function createRecord($table, $record) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Remove a record in the database. |
||
| 148 | * @param string $table The table where the record is removed in the database. |
||
| 149 | * @param string $pk The primary key value of the record. |
||
| 150 | * @return bool Returns boolean true if the record was successfully deleted or else it returns false. |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function deleteRecord($table, $pk) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Returns a particular record in a table. |
||
| 168 | * @param string $table The table of the record. |
||
| 169 | * @param string $pk The primary key value of the record. |
||
| 170 | * @return array An array containing the particular record. |
||
| 171 | */ |
||
| 172 | View Code Duplication | public function findRecord($table, $pk) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Returns all the records in a table. |
||
| 188 | * @param string $table The table inspected for all its records. |
||
| 189 | * @return array All the records in the table. |
||
| 190 | */ |
||
| 191 | public function getAllRecords($table) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns the columns of a table. |
||
| 202 | * @param string $table The table inspected for its columns. |
||
| 203 | * @return array The columns of the table. |
||
| 204 | */ |
||
| 205 | abstract public function getColumns($table); |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns the primary key of a table. |
||
| 209 | * @param string $table The table inspected for its primary key. |
||
| 210 | * @return string The primary key of the table. |
||
| 211 | */ |
||
| 212 | abstract public function getPrimaryKey($table); |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Update a record in the database. |
||
| 216 | * @param string $table The table where the record update is being made. |
||
| 217 | * @param string $pk The primary key value of the record to be updated. |
||
| 218 | * @param array $record The updates to be made to the record in the database. |
||
| 219 | * @return bool Returns boolean true if the record was successfully updated or else it returns false. |
||
| 220 | */ |
||
| 221 | public function updateRecord($table, $pk, $record) |
||
| 250 | } |
||
| 251 |