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 |
||
| 13 | class AuditDiffTable |
||
| 14 | { |
||
| 15 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 16 | /** |
||
| 17 | * |
||
| 18 | * |
||
| 19 | * @var array[] |
||
| 20 | */ |
||
| 21 | private $auditColumns; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Audit database schema. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $auditSchema; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Data database schema. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $dataSchema; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Difference between data and audit tables. |
||
| 39 | * |
||
| 40 | * @var DiffTableColumns |
||
| 41 | */ |
||
| 42 | private $diffColumns; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Table name. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $tableName; |
||
| 50 | |||
| 51 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 52 | /** |
||
| 53 | * Object constructor. |
||
| 54 | * |
||
| 55 | * @param string $dataSchema Data database schema. |
||
| 56 | * @param string $auditSchema Audit database schema. |
||
| 57 | * @param string $tableName Table name. |
||
| 58 | * @param array[] $auditColumns Audit columns from config file. |
||
| 59 | */ |
||
| 60 | public function __construct($dataSchema, $auditSchema, $tableName, $auditColumns) |
||
| 74 | |||
| 75 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 76 | /** |
||
| 77 | * Return diff columns. |
||
| 78 | * |
||
| 79 | * @return array[] |
||
| 80 | */ |
||
| 81 | public function getDiffColumns() |
||
| 85 | |||
| 86 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 87 | /** |
||
| 88 | * Check full and return array without new or obsolete columns if full not set. * |
||
| 89 | * |
||
| 90 | * @return array[] |
||
| 91 | */ |
||
| 92 | public function removeMatchingColumns() |
||
| 124 | |||
| 125 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 126 | /** |
||
| 127 | * Add not null to audit columns if it not nullable. |
||
| 128 | * |
||
| 129 | * @param array $theColumns Audit columns. |
||
| 130 | * |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | private function addNotNull($theColumns) |
||
| 152 | |||
| 153 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 154 | /** |
||
| 155 | * Get the difference between data and audit tables. |
||
| 156 | * |
||
| 157 | * @param TableColumnsMetadata $dataColumns The table columns from data schema. |
||
| 158 | * @param TableColumnsMetadata $auditColumns The table columns from audit schema. |
||
| 159 | */ |
||
| 160 | private function createDiffArray($dataColumns, $auditColumns) |
||
| 167 | |||
| 168 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 169 | } |
||
| 170 | |||
| 172 |