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 DiffTableHelper |
||
| 14 | { |
||
| 15 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 16 | /** |
||
| 17 | * Audit columns from config file. |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private $auditColumns; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Audit options from audit schema. |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | private $auditTableOptions; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Audit options from data schema. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $dataTableOptions; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Check existing separator. |
||
| 39 | * |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | private $existSeparator = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Full option. |
||
| 46 | * |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | private $fullOption; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Array with rows for table. |
||
| 53 | * |
||
| 54 | * @var \array[] |
||
| 55 | */ |
||
| 56 | private $rows = []; |
||
| 57 | |||
| 58 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 59 | /** |
||
| 60 | * Object constructor. |
||
| 61 | * |
||
| 62 | * @param string $dataSchema Data schema name. |
||
| 63 | * @param string $auditSchema Audit schema name. |
||
| 64 | * @param string $tableName The table name. |
||
| 65 | * @param array[] $theAuditColumns Audit columns from config file. |
||
| 66 | * @param bool $fullOption If set append table options to rows. |
||
| 67 | */ |
||
| 68 | public function __construct($dataSchema, $auditSchema, $tableName, $theAuditColumns, $fullOption) |
||
| 75 | |||
| 76 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 77 | /** |
||
| 78 | * Add highlighting to columns. |
||
| 79 | */ |
||
| 80 | public function addHighlighting() |
||
| 145 | |||
| 146 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 147 | /** |
||
| 148 | * Appends rows. |
||
| 149 | * |
||
| 150 | * @param array[] $theRows Rows array. |
||
| 151 | */ |
||
| 152 | public function appendRows($theRows) |
||
| 163 | |||
| 164 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 165 | /** |
||
| 166 | * Append row with table option. |
||
| 167 | * |
||
| 168 | * @param string $theOption The option. |
||
| 169 | * @param null|string $theName Display name. |
||
| 170 | */ |
||
| 171 | public function appendTableOption($theOption, $theName = null) |
||
| 191 | |||
| 192 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 193 | /** |
||
| 194 | * Get rows. |
||
| 195 | * |
||
| 196 | * @return \array[] |
||
| 197 | */ |
||
| 198 | public function getRows() |
||
| 202 | |||
| 203 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 204 | } |
||
| 205 | |||
| 207 |
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.