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 | /** |
||
| 47 | * Object constructor. |
||
| 48 | * |
||
| 49 | * @param string $dataSchema Data schema name. |
||
| 50 | * @param string $auditSchema Audit schema name. |
||
| 51 | * @param string $tableName The table name. |
||
| 52 | * @param array[] $theAuditColumns Audit columns from config file. |
||
| 53 | */ |
||
| 54 | public function __construct($dataSchema, $auditSchema, $tableName, $theAuditColumns) |
||
| 60 | |||
| 61 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 62 | /** |
||
| 63 | * Append row with table option. |
||
| 64 | * |
||
| 65 | * @param string $theOption |
||
| 66 | */ |
||
| 67 | public function appendTableOption($theOption) |
||
| 75 | |||
| 76 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 77 | /** |
||
| 78 | * Appends rows. |
||
| 79 | * |
||
| 80 | * @param \array[] $theRows |
||
| 81 | */ |
||
| 82 | public function appendRows($theRows) |
||
| 93 | |||
| 94 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 95 | /** |
||
| 96 | * Add highlighting to columns. |
||
| 97 | * |
||
| 98 | * @return array[] |
||
|
|
|||
| 99 | */ |
||
| 100 | public function addHighlighting() |
||
| 157 | |||
| 158 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 159 | /** |
||
| 160 | * Get rows. |
||
| 161 | * |
||
| 162 | * @return \array[] |
||
| 163 | */ |
||
| 164 | public function getRows() |
||
| 168 | |||
| 169 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 170 | } |
||
| 171 | |||
| 173 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.