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 | ||
| 12 | trait AggregationTrait | ||
| 13 | { | ||
| 14 | use ExtendedSelectionTrait; | ||
| 15 | protected $field; | ||
| 16 | |||
| 17 | /** | ||
| 18 | * Set the field name to sum over. | ||
| 19 | * | ||
| 20 | * @param string $field | ||
| 21 | * | ||
| 22 | * @return self | ||
| 23 | */ | ||
| 24 | public function field($field) | ||
| 30 | |||
| 31 | /** | ||
| 32 | * Run the query and return the value. | ||
| 33 | * | ||
| 34 | * @return int | ||
| 35 | */ | ||
| 36 | public function run() | ||
| 43 | |||
| 44 | /** | ||
| 45 |      * {@inheritdoc} | ||
| 46 | */ | ||
| 47 | View Code Duplication | public function __invoke() | |
| 54 | |||
| 55 | /** | ||
| 56 | * Build and return the query. | ||
| 57 | * | ||
| 58 | * @return string | ||
| 59 | */ | ||
| 60 | View Code Duplication | public function __toString() | |
| 70 | } | ||
| 71 |