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 |
||
| 20 | class AverageLeadCycleTimeAnalytic extends Base |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Build report. |
||
| 24 | * |
||
| 25 | * @param int $project_id Project id |
||
| 26 | * |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | public function build($project_id) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Calculate average. |
||
| 55 | * |
||
| 56 | * @param array &$stats |
||
| 57 | * @param string $field |
||
| 58 | * |
||
| 59 | * @return float |
||
| 60 | */ |
||
| 61 | private function calculateAverage(array &$stats, $field) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Calculate lead time. |
||
| 72 | * |
||
| 73 | * @param array &$task |
||
| 74 | * |
||
| 75 | * @return int |
||
| 76 | */ |
||
| 77 | private function calculateLeadTime(array &$task) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Calculate cycle time. |
||
| 87 | * |
||
| 88 | * @param array &$task |
||
| 89 | * |
||
| 90 | * @return int |
||
| 91 | */ |
||
| 92 | private function calculateCycleTime(array &$task) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the 1000 last created tasks. |
||
| 107 | * |
||
| 108 | * @param int $project_id |
||
| 109 | * |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | View Code Duplication | private function getTasks($project_id) |
|
| 122 | } |
||
| 123 |
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.