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 |
||
| 19 | class ColumnMoveRestrictionModel extends Model |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * SQL table name. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | const TABLE = 'column_has_move_restrictions'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Fetch one restriction. |
||
| 30 | * |
||
| 31 | * @param int $project_id |
||
| 32 | * @param int $restriction_id |
||
| 33 | * |
||
| 34 | * @return array|null |
||
| 35 | */ |
||
| 36 | View Code Duplication | public function getById($project_id, $restriction_id) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Get all project column restrictions. |
||
| 60 | * |
||
| 61 | * @param int $project_id |
||
| 62 | * |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | View Code Duplication | public function getAll($project_id) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Get all sortable column Ids. |
||
| 88 | * |
||
| 89 | * @param int $project_id |
||
| 90 | * @param string $role |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public function getSortableColumns($project_id, $role) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Create a new column restriction. |
||
| 107 | * |
||
| 108 | * @param int $project_id |
||
| 109 | * @param int $role_id |
||
| 110 | * @param int $src_column_id |
||
| 111 | * @param int $dst_column_id |
||
| 112 | * |
||
| 113 | * @return bool|int |
||
| 114 | */ |
||
| 115 | public function create($project_id, $role_id, $src_column_id, $dst_column_id) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Remove a permission. |
||
| 129 | * |
||
| 130 | * @param int $restriction_id |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | public function remove($restriction_id) |
||
| 138 | } |
||
| 139 |
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.