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 |
||
| 15 | class Intern |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Run an experiment, and retrieve the result. |
||
| 19 | * |
||
| 20 | * @param \Scientist\Experiment $experiment |
||
| 21 | * |
||
| 22 | * @return \Scientist\Report|\Scientist\Report[] |
||
| 23 | */ |
||
| 24 | 14 | public function run(Experiment $experiment) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Run the control callback, and record its execution state. |
||
| 52 | * |
||
| 53 | * @param \Scientist\Experiment $experiment |
||
| 54 | * |
||
| 55 | * @return \Scientist\Result|\Scientist\Result[] |
||
| 56 | */ |
||
| 57 | 14 | protected function runControl(Experiment $experiment) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Run trial callbacks and record their execution state. |
||
| 81 | * |
||
| 82 | * @param \Scientist\Experiment $experiment |
||
| 83 | * |
||
| 84 | * @return \Scientist\Result[] |
||
| 85 | */ |
||
| 86 | 13 | protected function runTrials(Experiment $experiment) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Determine whether trial results match the control. |
||
| 115 | * |
||
| 116 | * @param \Scientist\Matchers\Matcher $matcher |
||
| 117 | * @param \Scientist\Result $control |
||
| 118 | * @param \Scientist\Result[] $trials |
||
| 119 | * @param int|null $key |
||
| 120 | */ |
||
| 121 | 13 | protected function determineMatches( |
|
| 139 | } |
||
| 140 |
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.