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 Builder extends EloquentBuilder |
||
10 | { |
||
11 | /** |
||
12 | * Create a collection of models from plain arrays. |
||
13 | * |
||
14 | * @param \Cassandra\Rows|array $rows |
||
15 | * |
||
16 | * @return Collection |
||
17 | */ |
||
18 | 25 | public function hydrateRows($rows) |
|
24 | |||
25 | /** |
||
26 | * Execute the query as a "select" statement. |
||
27 | * |
||
28 | * @param array $columns |
||
29 | * |
||
30 | * @return Collection |
||
31 | * |
||
32 | * @throws \Exception |
||
33 | */ |
||
34 | 7 | public function getPage($columns = ['*']) |
|
40 | |||
41 | /** |
||
42 | * Get the hydrated models without eager loading. |
||
43 | * |
||
44 | * @param array $columns |
||
45 | * |
||
46 | * @return Collection |
||
47 | * |
||
48 | * @throws \Exception |
||
49 | */ |
||
50 | 7 | View Code Duplication | public function getModelsPage($columns = ['*']) |
67 | |||
68 | /** |
||
69 | * Execute the query as a "select" statement. |
||
70 | * |
||
71 | * @param array $columns |
||
72 | * |
||
73 | * @return \Illuminate\Database\Eloquent\Collection|static[] |
||
74 | * |
||
75 | * @throws \Exception |
||
76 | */ |
||
77 | 19 | public function get($columns = ['*']) |
|
83 | |||
84 | /** |
||
85 | * Get the hydrated models without eager loading. |
||
86 | * |
||
87 | * @param array $columns |
||
88 | * |
||
89 | * @return \Illuminate\Database\Eloquent\Collection|static[] |
||
90 | * |
||
91 | * @throws \Exception |
||
92 | */ |
||
93 | 19 | View Code Duplication | public function getModels($columns = ['*']) |
110 | |||
111 | /** |
||
112 | * Add the "updated at" column to an array of values. |
||
113 | * |
||
114 | * @param array $values |
||
115 | * @return array |
||
116 | */ |
||
117 | 2 | protected function addUpdatedAtColumn(array $values) |
|
138 | |||
139 | |||
140 | } |
||
141 |
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.