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 |
||
| 14 | class Audit |
||
| 15 | { |
||
| 16 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 17 | /** |
||
| 18 | * The metadata (additional) audit columns (as stored in the config file). |
||
| 19 | * |
||
| 20 | * @var TableColumnsMetadata |
||
| 21 | */ |
||
| 22 | private $auditColumnsMetadata; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The names of all tables in audit schema. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private $auditSchemaTables; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The content of the configuration file. |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | private $config; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The names of all tables in data schema. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $dataSchemaTables; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The Output decorator. |
||
| 47 | * |
||
| 48 | * @var StratumStyle |
||
| 49 | */ |
||
| 50 | private $io; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * If true remove all column information from config file. |
||
| 54 | * |
||
| 55 | * @var boolean |
||
| 56 | */ |
||
| 57 | private $pruneOption; |
||
| 58 | |||
| 59 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 60 | /** |
||
| 61 | * Object constructor. |
||
| 62 | * |
||
| 63 | * @param array[] $config The content of the configuration file. |
||
| 64 | * @param StratumStyle $io The Output decorator. |
||
| 65 | */ |
||
| 66 | 13 | public function __construct(&$config, $io) |
|
| 71 | |||
| 72 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 73 | /** |
||
| 74 | * Getting list of all tables from information_schema of database from config file. |
||
| 75 | */ |
||
| 76 | 13 | public function listOfTables() |
|
| 82 | |||
| 83 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 84 | /** |
||
| 85 | * The main method: executes the auditing actions for tables. |
||
| 86 | * |
||
| 87 | * @return int The exit status. |
||
| 88 | */ |
||
| 89 | 13 | public function main() |
|
| 106 | |||
| 107 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 108 | /** |
||
| 109 | * Sets the columns metadata of a table in the configuration file. |
||
| 110 | * |
||
| 111 | * @param string $tableName The name of table. |
||
| 112 | * @param TableColumnsMetadata $columns The metadata of the table columns. |
||
| 113 | */ |
||
| 114 | 12 | public function setConfigTableColumns($tableName, $columns) |
|
| 123 | |||
| 124 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 125 | /** |
||
| 126 | * Compares the tables listed in the config file and the tables found in the data schema. |
||
| 127 | */ |
||
| 128 | 13 | public function unknownTables() |
|
| 158 | |||
| 159 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 160 | /** |
||
| 161 | * Drop triggers from obsolete table. |
||
| 162 | * |
||
| 163 | 13 | * @param string $schemaName The schema name. |
|
| 164 | * @param string $tableName The table name. |
||
| 165 | 13 | */ |
|
| 166 | 13 | View Code Duplication | protected function dropTriggersFromObsoleteTable($schemaName, $tableName) |
| 178 | |||
| 179 | 9 | //-------------------------------------------------------------------------------------------------------------------- |
|
| 180 | 9 | /** |
|
| 181 | 9 | * Resolves the canonical column types of the audit table columns. |
|
| 182 | 9 | */ |
|
| 183 | 9 | View Code Duplication | protected function resolveCanonicalAuditColumns() |
| 213 | |||
| 214 | 11 | //-------------------------------------------------------------------------------------------------------------------- |
|
| 215 | /** |
||
| 216 | 12 | * Processed known tables. |
|
| 217 | 12 | * |
|
| 218 | 12 | * @return int The exit status. |
|
| 219 | */ |
||
| 220 | 12 | private function knownTables() |
|
| 274 | |||
| 275 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 276 | } |
||
| 277 | |||
| 279 |
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.