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 |
||
9 | class WhereScope implements Scope |
||
10 | { |
||
11 | /** |
||
12 | * Apply the scope to a given Eloquent query builder. |
||
13 | * |
||
14 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
15 | * @param \Illuminate\Database\Eloquent\Model $model |
||
16 | * @return void |
||
17 | */ |
||
18 | public function apply(Builder $builder, Model $model) |
||
24 | |||
25 | /** |
||
26 | * @param Builder $builder |
||
27 | * @param Model $model |
||
28 | * @return \Illuminate\Database\Query\Builder |
||
29 | */ |
||
30 | private function changeQuery(Builder $builder, Model $model) |
||
53 | |||
54 | /** |
||
55 | * @param $where |
||
56 | * @param $translatableColumns |
||
57 | * @param $locale |
||
58 | * @return mixed |
||
59 | */ |
||
60 | private function iterateOnBasic($where, $translatableColumns, $locale) |
||
71 | |||
72 | /** |
||
73 | * @param $where |
||
74 | * @param $translatableColumns |
||
75 | * @param $locale |
||
76 | * @return mixed |
||
77 | */ |
||
78 | private function iterateOnNested($where, $translatableColumns, $locale) |
||
94 | } |
||
95 |
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.