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 ColumnRestrictionModel extends Model |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * SQL table name. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | const TABLE = 'column_has_restrictions'; |
||
| 27 | |||
| 28 | const RULE_ALLOW_TASK_CREATION = 'allow.task_creation'; |
||
| 29 | const RULE_ALLOW_TASK_OPEN_CLOSE = 'allow.task_open_close'; |
||
| 30 | const RULE_BLOCK_TASK_CREATION = 'block.task_creation'; |
||
| 31 | const RULE_BLOCK_TASK_OPEN_CLOSE = 'block.task_open_close'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Get rules. |
||
| 35 | * |
||
| 36 | * @return array |
||
| 37 | */ |
||
| 38 | View Code Duplication | public function getRules() |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Fetch one restriction. |
||
| 50 | * |
||
| 51 | * @param int $project_id |
||
| 52 | * @param int $restriction_id |
||
| 53 | * |
||
| 54 | * @return array|null |
||
| 55 | */ |
||
| 56 | public function getById($project_id, $restriction_id) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get all project column restrictions. |
||
| 78 | * |
||
| 79 | * @param int $project_id |
||
| 80 | * |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | public function getAll($project_id) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get restrictions. |
||
| 111 | * |
||
| 112 | * @param int $project_id |
||
| 113 | * @param string $role |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | View Code Duplication | public function getAllByRole($project_id, $role) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Create a new column restriction. |
||
| 137 | * |
||
| 138 | * @param int $project_id |
||
| 139 | * @param int $role_id |
||
| 140 | * @param int $column_id |
||
| 141 | * @param int $rule |
||
| 142 | * |
||
| 143 | * @return bool|int |
||
| 144 | */ |
||
| 145 | View Code Duplication | public function create($project_id, $role_id, $column_id, $rule) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Remove a permission. |
||
| 159 | * |
||
| 160 | * @param int $restriction_id |
||
| 161 | * |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | public function remove($restriction_id) |
||
| 168 | } |
||
| 169 |
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.