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 |
||
| 21 | final class HasOne extends AbstractRelation |
||
| 22 | { |
||
| 23 | View Code Duplication | protected function initQuery(Query $query): Query |
|
| 36 | |||
| 37 | public function getResults() |
||
| 46 | |||
| 47 | public function save(Model $model): Model |
||
| 54 | |||
| 55 | public function create(array $values = []): Model |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Attaches a child model to this model. |
||
| 67 | * |
||
| 68 | * @param Model $model child model |
||
| 69 | * |
||
| 70 | * @throws ModelException when the operation fails |
||
| 71 | * |
||
| 72 | * @return $this |
||
| 73 | */ |
||
| 74 | public function attach(Model $model) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Detaches the child model from this model. |
||
| 84 | * |
||
| 85 | * @throws ModelException when the operation fails |
||
| 86 | * |
||
| 87 | * @return $this |
||
| 88 | */ |
||
| 89 | public function detach() |
||
| 100 | } |
||
| 101 |
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.