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 | final class BelongsTo extends AbstractRelation |
||
| 21 | { |
||
| 22 | View Code Duplication | protected function initQuery(Query $query): Query |
|
| 35 | |||
| 36 | public function getResults() |
||
| 45 | |||
| 46 | public function save(Model $model): Model |
||
| 53 | |||
| 54 | public function create(array $values = []): Model |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Attaches this model to an owning model. |
||
| 67 | * |
||
| 68 | * @param Model $model owning model |
||
| 69 | * |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | public function attach(Model $model) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Detaches this model from the owning model. |
||
| 82 | * |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | public function detach() |
||
| 92 | } |
||
| 93 |
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.