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 |
||
| 23 | abstract class PDODriverAbstract extends DBDriverAbstract{ |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Holds the database resource object |
||
| 27 | * |
||
| 28 | * @var PDO |
||
| 29 | */ |
||
| 30 | protected $db; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Some basic PDO options |
||
| 34 | * |
||
| 35 | * @see http://php.net/manual/pdo.getattribute.php PDO::getAttribute |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $pdo_options = [ |
||
| 40 | PDO::ATTR_CASE => PDO::CASE_NATURAL, |
||
| 41 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
| 42 | PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, |
||
| 43 | PDO::ATTR_STRINGIFY_FETCHES => false, |
||
| 44 | PDO::ATTR_EMULATE_PREPARES => false, |
||
| 45 | ]; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The PDO drivername which is being used in the DSN |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $drivername; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * This array holds one or more key=>value pairs to set attribute values for the PDOStatement object that this |
||
| 56 | * method returns. You would most commonly use this to set the PDO::ATTR_CURSOR value to PDO::CURSOR_SCROLL to |
||
| 57 | * request a scrollable cursor. |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $pdo_stmt_options = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Returns a DSN string using the given options |
||
| 65 | * |
||
| 66 | * @link http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#4.4 |
||
| 67 | * |
||
| 68 | * @return string DSN |
||
| 69 | */ |
||
| 70 | protected function getDSN():string{ |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Establishes a database connection and returns the connection object |
||
| 88 | * |
||
| 89 | * @return \chillerlan\Database\Drivers\DBDriverInterface |
||
| 90 | * @throws \chillerlan\Database\DBException |
||
| 91 | */ |
||
| 92 | public function connect():DBDriverInterface{ |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Closes a database connection |
||
| 122 | * |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | public function disconnect():bool{ |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns info about the used php client |
||
| 133 | * |
||
| 134 | * @return string php's database client string |
||
| 135 | */ |
||
| 136 | public function getClientInfo():string{ |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns info about the database server |
||
| 142 | * |
||
| 143 | * @return string database's serverinfo string |
||
| 144 | */ |
||
| 145 | public function getServerInfo():string{ |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param $data |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | public function escape($data){ |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param \PDOStatement $stmt |
||
| 160 | * @param array $values |
||
| 161 | * |
||
| 162 | * @return void |
||
| 163 | */ |
||
| 164 | protected function bindParams(PDOStatement &$stmt, array $values){ |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the last insert id (if present) |
||
| 183 | * |
||
| 184 | * @link http://php.net/manual/pdo.lastinsertid.php |
||
| 185 | * @return string |
||
| 186 | * @codeCoverageIgnore |
||
| 187 | */ |
||
| 188 | protected function insertID():string{ |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param $stmt |
||
| 194 | * @param string|null $index |
||
| 195 | * @param bool $assoc |
||
| 196 | * |
||
| 197 | * @return bool|\chillerlan\Database\DBResult |
||
| 198 | */ |
||
| 199 | protected function __getResult($stmt, string $index = null, bool $assoc = true){ |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $sql |
||
| 210 | * @param string|null $index |
||
| 211 | * @param bool $assoc |
||
| 212 | * |
||
| 213 | * @return bool|\chillerlan\Database\DBResult |
||
| 214 | */ |
||
| 215 | protected function __raw(string $sql, string $index = null, bool $assoc = true){ |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param string $sql |
||
| 221 | * @param array $values |
||
| 222 | * @param string|null $index |
||
| 223 | * @param bool $assoc |
||
| 224 | * |
||
| 225 | * @return bool|\chillerlan\Database\DBResult |
||
| 226 | */ |
||
| 227 | protected function __prepared(string $sql, array $values = [], string $index = null, bool $assoc = true){ |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param string $sql |
||
| 241 | * @param array $values |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | View Code Duplication | protected function __multi(string $sql, array $values){ |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $sql |
||
| 260 | * @param array $data |
||
| 261 | * @param $callback |
||
| 262 | * |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | View Code Duplication | protected function __multi_callback(string $sql, array $data, $callback){ |
|
| 277 | |||
| 278 | } |
||
| 279 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.