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 ProjectDailyColumnStatsModel extends Model |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * SQL table name. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | const TABLE = 'project_daily_column_stats'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Update daily totals for the project and for each column. |
||
| 30 | * |
||
| 31 | * "total" is the number open of tasks in the column |
||
| 32 | * "score" is the sum of tasks score in the column |
||
| 33 | * |
||
| 34 | * @param int $project_id Project id |
||
| 35 | * @param string $date Record date (YYYY-MM-DD) |
||
| 36 | * |
||
| 37 | * @return bool |
||
| 38 | */ |
||
| 39 | View Code Duplication | public function updateTotals($project_id, $date) |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Count the number of recorded days for the data range. |
||
| 61 | * |
||
| 62 | * @param int $project_id Project id |
||
| 63 | * @param string $from Start date (ISO format YYYY-MM-DD) |
||
| 64 | * @param string $to End date |
||
| 65 | * |
||
| 66 | * @return int |
||
| 67 | */ |
||
| 68 | public function countDays($project_id, $from, $to) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get aggregated metrics for the project within a data range. |
||
| 79 | * |
||
| 80 | * [ |
||
| 81 | * ['Date', 'Column1', 'Column2'], |
||
| 82 | * ['2014-11-16', 2, 5], |
||
| 83 | * ['2014-11-17', 20, 15], |
||
| 84 | * ] |
||
| 85 | * |
||
| 86 | * @param int $project_id Project id |
||
| 87 | * @param string $from Start date (ISO format YYYY-MM-DD) |
||
| 88 | * @param string $to End date |
||
| 89 | * @param string $field Column to aggregate |
||
| 90 | * |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | public function getAggregatedMetrics($project_id, $from, $to, $field = 'total') |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Fetch metrics. |
||
| 103 | * |
||
| 104 | * @param int $project_id Project id |
||
| 105 | * @param string $from Start date (ISO format YYYY-MM-DD) |
||
| 106 | * @param string $to End date |
||
| 107 | * |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | public function getMetrics($project_id, $from, $to) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Build aggregate. |
||
| 122 | * |
||
| 123 | * @param array $metrics |
||
| 124 | * @param array $columns |
||
| 125 | * @param string $field |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | private function buildAggregate(array &$metrics, array &$columns, $field) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Build one row of the aggregate. |
||
| 144 | * |
||
| 145 | * @param array $metrics |
||
| 146 | * @param array $column_ids |
||
| 147 | * @param string $day |
||
| 148 | * @param string $field |
||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | private function buildRowAggregate(array &$metrics, array &$column_ids, $day, $field) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Find the value in the metrics. |
||
| 165 | * |
||
| 166 | * @param array $metrics |
||
| 167 | * @param string $day |
||
| 168 | * @param string $column_id |
||
| 169 | * @param string $field |
||
| 170 | * |
||
| 171 | * @return int |
||
| 172 | */ |
||
| 173 | private function findValueInMetrics(array &$metrics, $day, $column_id, $field) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get number of tasks and score by columns. |
||
| 186 | * |
||
| 187 | * @param int $project_id |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | private function getStatsByColumns($project_id) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get number of tasks and score by columns. |
||
| 210 | * |
||
| 211 | * @param int $project_id |
||
| 212 | * |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | View Code Duplication | private function getScoreByColumns($project_id) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Get number of tasks and score by columns. |
||
| 230 | * |
||
| 231 | * @param int $project_id |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | View Code Duplication | private function getTotalByColumns($project_id) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Get task status to use for total calculation. |
||
| 249 | * |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | private function getTaskStatusConfig() |
||
| 260 | } |
||
| 261 |
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.