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 TableHelper |
||
| 15 | { |
||
| 16 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 17 | /** |
||
| 18 | * Array with rows for table. |
||
| 19 | * |
||
| 20 | * @var \array[] |
||
| 21 | */ |
||
| 22 | private $rows = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Audit columns from config file. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private $auditColumns; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Table options from audit schema. |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | private $auditTableOptions; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Table options from data schema. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $dataTableOptions; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Full option. |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | private $fullOption; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Check existing separator. |
||
| 54 | * |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | private $existSeparator = false; |
||
| 58 | |||
| 59 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 60 | /** |
||
| 61 | * Object constructor. |
||
| 62 | * |
||
| 63 | * @param string $dataSchema Data schema name. |
||
| 64 | * @param string $auditSchema Audit schema name. |
||
| 65 | * @param string $tableName The table name. |
||
| 66 | * @param array[] $theAuditColumns Audit columns from config file. |
||
| 67 | * @param bool $fullOption If set append table options to rows. |
||
| 68 | */ |
||
| 69 | public function __construct($dataSchema, $auditSchema, $tableName, $theAuditColumns, $fullOption) |
||
| 76 | |||
| 77 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 78 | /** |
||
| 79 | * Append row with table option. |
||
| 80 | * |
||
| 81 | * @param string $theOption The option. |
||
| 82 | * @param null|string $theName Display name. |
||
| 83 | */ |
||
| 84 | public function appendTableOption($theOption, $theName = null) |
||
| 104 | |||
| 105 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 106 | /** |
||
| 107 | * Appends rows. |
||
| 108 | * |
||
| 109 | * @param \array[] $theRows Rows array. |
||
| 110 | */ |
||
| 111 | public function appendRows($theRows) |
||
| 122 | |||
| 123 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 124 | /** |
||
| 125 | * Add highlighting to columns. |
||
| 126 | */ |
||
| 127 | public function addHighlighting() |
||
| 184 | |||
| 185 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 186 | /** |
||
| 187 | * Get rows. |
||
| 188 | * |
||
| 189 | * @return \array[] |
||
| 190 | */ |
||
| 191 | public function getRows() |
||
| 195 | |||
| 196 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 197 | } |
||
| 198 | |||
| 200 |
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.