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 |
||
8 | class EloquentRepository extends AbstractRepository |
||
9 | { |
||
10 | /** |
||
11 | * $converter. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $compiler = EloquentCompiler::class; |
||
16 | |||
17 | /** |
||
18 | * __construct. |
||
19 | * |
||
20 | * @param \Illuminate\Database\Eloquent\Model $model |
||
21 | */ |
||
22 | 25 | public function __construct(Model $model) |
|
26 | |||
27 | /** |
||
28 | * get. |
||
29 | * |
||
30 | * @param \Recca0120\Repository\Criteria|array $criteria |
||
31 | * @param int $limit |
||
32 | * @param int $offset |
||
33 | * @return \Illuminate\Support\Collection |
||
34 | */ |
||
35 | 5 | View Code Duplication | public function get($criteria = [], $columns = ['*'], $limit = null, $offset = null) |
49 | |||
50 | /** |
||
51 | * paginate. |
||
52 | * |
||
53 | * @param mixed $criteria |
||
54 | * @param string $perPage |
||
55 | * @param int $pageName |
||
56 | * @param int $page |
||
57 | * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator |
||
58 | */ |
||
59 | 3 | View Code Duplication | public function paginate($criteria = [], $perPage = null, $columns = ['*'], $pageName = 'page', $page = null) |
66 | |||
67 | /** |
||
68 | * simplePaginate. |
||
69 | * |
||
70 | * @param mixed $criteria |
||
71 | * @param string $perPage |
||
72 | * @param int $pageName |
||
73 | * @param int $page |
||
74 | * @return \Illuminate\Contracts\Pagination\Paginator |
||
75 | */ |
||
76 | 1 | View Code Duplication | public function simplePaginate($criteria = [], $perPage = null, $columns = ['*'], $pageName = 'page', $page = null) |
83 | |||
84 | /** |
||
85 | * first. |
||
86 | * |
||
87 | * @param \Recca0120\Repository\Criteria|array $criteria |
||
88 | * @return mixed |
||
89 | */ |
||
90 | 2 | public function first($criteria = [], $columns = ['*']) |
|
96 | |||
97 | /** |
||
98 | * find. |
||
99 | * |
||
100 | * @param int $id |
||
101 | * @return mixed |
||
102 | */ |
||
103 | 4 | View Code Duplication | public function find($id, $columns = ['*']) |
110 | |||
111 | /** |
||
112 | * newInstance. |
||
113 | * |
||
114 | * @param array $attributes |
||
115 | * @return \Illuminate\Database\Eloquent |
||
116 | */ |
||
117 | 4 | View Code Duplication | public function newInstance($attributes = []) |
124 | |||
125 | /** |
||
126 | * create. |
||
127 | * |
||
128 | * @param array $attributes |
||
129 | * @param bool $forceFill |
||
130 | * @return mixed |
||
131 | */ |
||
132 | 2 | View Code Duplication | public function create($attributes, $forceFill = false) |
140 | |||
141 | /** |
||
142 | * update. |
||
143 | * |
||
144 | * @param array $attributes |
||
145 | * @param int $id |
||
146 | * @param bool $forceFill |
||
147 | * @return mixed |
||
148 | */ |
||
149 | 2 | View Code Duplication | public function update($attributes, $id, $forceFill = false) |
157 | |||
158 | /** |
||
159 | * delete. |
||
160 | * |
||
161 | * @param int $id |
||
162 | * @return bool |
||
163 | */ |
||
164 | 1 | public function delete($id) |
|
168 | |||
169 | /** |
||
170 | * destroy. |
||
171 | * |
||
172 | * @param \Recca0120\Repository\Criteria|array $criteria |
||
173 | * @return int |
||
174 | */ |
||
175 | public function destroy($criteria = []) |
||
181 | |||
182 | /** |
||
183 | * chunk. |
||
184 | * |
||
185 | * @param Criteria|array $criteria |
||
186 | * @param int $count |
||
187 | * @param callable $callback |
||
188 | * @return \Illuminate\Support\Collection |
||
189 | */ |
||
190 | 2 | public function chunk($criteria, $count, callable $callback) |
|
196 | } |
||
197 |
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.