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 |
||
| 17 | class AuditDataLayer |
||
| 18 | { |
||
| 19 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 20 | /** |
||
| 21 | * The connection to the MySQL instance. |
||
| 22 | * |
||
| 23 | * @var StaticDataLayer |
||
| 24 | */ |
||
| 25 | private static $dl; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The Output decorator. |
||
| 29 | * |
||
| 30 | * @var StratumStyle |
||
| 31 | */ |
||
| 32 | private static $io; |
||
| 33 | |||
| 34 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 35 | /** |
||
| 36 | * Adds new columns to an audit table. |
||
| 37 | * |
||
| 38 | * @param string $auditSchemaName The name of audit schema. |
||
| 39 | * @param string $tableName The name of the table. |
||
| 40 | * @param TableColumnsMetadata $columns The metadata of the new columns. |
||
| 41 | */ |
||
| 42 | 8 | public static function addNewColumns($auditSchemaName, $tableName, $columns) |
|
| 52 | |||
| 53 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 54 | /** |
||
| 55 | * Connects to a MySQL instance. |
||
| 56 | * |
||
| 57 | * Wrapper around [mysqli::__construct](http://php.net/manual/mysqli.construct.php), however on failure an exception |
||
| 58 | * is thrown. |
||
| 59 | * |
||
| 60 | * @param string $host The hostname. |
||
| 61 | * @param string $user The MySQL user name. |
||
| 62 | * @param string $passWord The password. |
||
| 63 | * @param string $database The default database. |
||
| 64 | * @param int $port The port number. |
||
| 65 | */ |
||
| 66 | 9 | public static function connect($host, $user, $passWord, $database, $port = 3306) |
|
| 72 | |||
| 73 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 74 | /** |
||
| 75 | * Creates an audit table. |
||
| 76 | * |
||
| 77 | * @param string $dataSchemaName The name of the data schema. |
||
| 78 | * @param string $auditSchemaName The name of the audit schema. |
||
| 79 | * @param string $tableName The name of the table. |
||
| 80 | * @param TableColumnsMetadata $columns The metadata of the columns of the audit table (i.e. the audit |
||
| 81 | * columns and columns of the data table). |
||
| 82 | */ |
||
| 83 | 5 | public static function createAuditTable($dataSchemaName, $auditSchemaName, $tableName, $columns) |
|
| 90 | |||
| 91 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 92 | /** |
||
| 93 | * Creates a trigger on a table. |
||
| 94 | * |
||
| 95 | * @param string $dataSchemaName The name of the data schema. |
||
| 96 | * @param string $auditSchemaName The name of the audit schema. |
||
| 97 | * @param string $tableName The name of the table. |
||
| 98 | * @param string $triggerAction The trigger action (i.e. INSERT, UPDATE, or DELETE). |
||
| 99 | * @param string $triggerName The name of the trigger. |
||
| 100 | * @param TableColumnsMetadata $auditColumns The audit table columns. |
||
| 101 | * @param TableColumnsMetadata $tableColumns The data table columns. |
||
| 102 | * @param string $skipVariable The skip variable. |
||
| 103 | * @param string[] $additionSql Additional SQL statements. |
||
| 104 | */ |
||
| 105 | 8 | public static function createAuditTrigger($dataSchemaName, |
|
| 128 | |||
| 129 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 130 | /** |
||
| 131 | * Create temp table for getting column type information for audit columns. |
||
| 132 | * |
||
| 133 | * @param string $schemaName The name of the table schema. |
||
| 134 | * @param string $tableName The table name. |
||
| 135 | * @param array[] $auditColumns Audit columns from config file. |
||
| 136 | */ |
||
| 137 | 8 | public static function createTemporaryTable($schemaName, $tableName, $auditColumns) |
|
| 153 | |||
| 154 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 155 | /** |
||
| 156 | * Closes the connection to the MySQL instance, if connected. |
||
| 157 | */ |
||
| 158 | 9 | public static function disconnect() |
|
| 166 | |||
| 167 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 168 | /** |
||
| 169 | * Drop table. |
||
| 170 | * |
||
| 171 | * @param string $schemaName The name of the table schema. |
||
| 172 | * @param string $tableName The name of the table. |
||
| 173 | */ |
||
| 174 | 8 | public static function dropTemporaryTable($schemaName, $tableName) |
|
| 180 | |||
| 181 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 182 | /** |
||
| 183 | * Drops a trigger. |
||
| 184 | * |
||
| 185 | * @param string $triggerSchema The name of the trigger schema. |
||
| 186 | * @param string $triggerName The mame of trigger. |
||
| 187 | */ |
||
| 188 | 3 | public static function dropTrigger($triggerSchema, $triggerName) |
|
| 194 | |||
| 195 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 196 | /** |
||
| 197 | * @param string $query The SQL statement. |
||
| 198 | * |
||
| 199 | * @return int The number of affected rows (if any). |
||
| 200 | */ |
||
| 201 | 8 | public static function executeNone($query) |
|
| 207 | |||
| 208 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 209 | /** |
||
| 210 | * Executes a query that returns 0 or 1 row. |
||
| 211 | * Throws an exception if the query selects 2 or more rows. |
||
| 212 | * |
||
| 213 | * @param string $query The SQL statement. |
||
| 214 | * |
||
| 215 | * @return array|null The selected row. |
||
| 216 | */ |
||
| 217 | public static function executeRow0($query) |
||
| 223 | |||
| 224 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 225 | /** |
||
| 226 | * Executes a query that returns 1 and only 1 row. |
||
| 227 | * Throws an exception if the query selects none, 2 or more rows. |
||
| 228 | * |
||
| 229 | * @param string $query The SQL statement. |
||
| 230 | * |
||
| 231 | * @return array The selected row. |
||
| 232 | */ |
||
| 233 | 5 | public static function executeRow1($query) |
|
| 239 | |||
| 240 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 241 | /** |
||
| 242 | * Executes a query that returns 0 or more rows. |
||
| 243 | * |
||
| 244 | * @param string $query The SQL statement. |
||
| 245 | * |
||
| 246 | * @return \array[] |
||
| 247 | */ |
||
| 248 | 9 | public static function executeRows($query) |
|
| 254 | |||
| 255 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 256 | /** |
||
| 257 | * Executes a query that returns 0 or 1 row. |
||
| 258 | * Throws an exception if the query selects 2 or more rows. |
||
| 259 | * |
||
| 260 | * @param string $query The SQL statement. |
||
| 261 | * |
||
| 262 | * @return int|string|null The selected row. |
||
| 263 | */ |
||
| 264 | public static function executeSingleton0($query) |
||
| 270 | |||
| 271 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 272 | /** |
||
| 273 | * Executes a query that returns 1 and only 1 row with 1 column. |
||
| 274 | * Throws an exception if the query selects none, 2 or more rows. |
||
| 275 | * |
||
| 276 | * @param string $query The SQL statement. |
||
| 277 | * |
||
| 278 | * @return int|string The selected row. |
||
| 279 | */ |
||
| 280 | public static function executeSingleton1($query) |
||
| 286 | |||
| 287 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 288 | /** |
||
| 289 | * Selects metadata of all columns of table. |
||
| 290 | * |
||
| 291 | * @param string $schemaName The name of the table schema. |
||
| 292 | * @param string $tableName The name of the table. |
||
| 293 | * |
||
| 294 | * @return \array[] |
||
| 295 | */ |
||
| 296 | 8 | View Code Duplication | public static function getTableColumns($schemaName, $tableName) |
| 313 | |||
| 314 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 315 | /** |
||
| 316 | * Selects table engine, character_set_name and table_collation. |
||
| 317 | * |
||
| 318 | * @param string $schemaName The name of the table schema. |
||
| 319 | * @param string $tableName The name of the table. |
||
| 320 | * |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | 5 | View Code Duplication | public static function getTableOptions($schemaName, $tableName) |
| 338 | |||
| 339 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 340 | /** |
||
| 341 | * Selects all triggers on a table. |
||
| 342 | * |
||
| 343 | * @param string $schemaName The name of the table schema. |
||
| 344 | * @param string $tableName The name of the table. |
||
| 345 | * |
||
| 346 | * @return \array[] |
||
| 347 | */ |
||
| 348 | 8 | View Code Duplication | public static function getTableTriggers($schemaName, $tableName) |
| 361 | |||
| 362 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 363 | /** |
||
| 364 | * Selects all table names in a schema. |
||
| 365 | * |
||
| 366 | * @param string $schemaName The name of the schema. |
||
| 367 | * |
||
| 368 | * @return \array[] |
||
| 369 | */ |
||
| 370 | 9 | public static function getTablesNames($schemaName) |
|
| 381 | |||
| 382 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 383 | /** |
||
| 384 | * Acquires a write lock on a table. |
||
| 385 | * |
||
| 386 | * @param string $tableName The table name. |
||
| 387 | */ |
||
| 388 | 8 | public static function lockTable($tableName) |
|
| 394 | |||
| 395 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 396 | /** |
||
| 397 | * Sets the Output decorator. |
||
| 398 | * |
||
| 399 | * @param StratumStyle $io The Output decorator. |
||
| 400 | */ |
||
| 401 | 9 | public static function setIo($io) |
|
| 405 | |||
| 406 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 407 | /** |
||
| 408 | * Drop table. |
||
| 409 | * |
||
| 410 | * @param string $schemaName The name of the table schema. |
||
| 411 | * @param string $tableName The name of the table. |
||
| 412 | * |
||
| 413 | * @return \array[] |
||
| 414 | */ |
||
| 415 | public static function showColumns($schemaName, $tableName) |
||
| 421 | |||
| 422 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 423 | /** |
||
| 424 | * Releases all table locks. |
||
| 425 | */ |
||
| 426 | 8 | public static function unlockTables() |
|
| 432 | |||
| 433 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 434 | /** |
||
| 435 | * Logs the query on the console. |
||
| 436 | * |
||
| 437 | * @param string $query The query. |
||
| 438 | */ |
||
| 439 | 9 | private static function logQuery($query) |
|
| 455 | |||
| 456 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 457 | } |
||
| 458 | |||
| 460 |
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.