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 | class DatabaseService implements ServiceInterface |
||
22 | { |
||
23 | use Filterable, Sortable; |
||
24 | |||
25 | /** |
||
26 | * Contains Laravel Application instance. |
||
27 | * |
||
28 | * @var Application |
||
29 | */ |
||
30 | protected $app; |
||
31 | |||
32 | /** |
||
33 | * Contains a repository instance. |
||
34 | * |
||
35 | * @var Repository |
||
36 | */ |
||
37 | protected $repository; |
||
38 | |||
39 | /** |
||
40 | * Contains model instance for fetch, and simple fetch methods. |
||
41 | * |
||
42 | * @var mixed |
||
43 | */ |
||
44 | protected $instance; |
||
45 | |||
46 | /** |
||
47 | * Create a new database service instance. |
||
48 | * |
||
49 | * @param Application $app |
||
50 | * @param Repository $repository |
||
51 | */ |
||
52 | 120 | public function __construct(Application $app, Repository $repository) |
|
57 | |||
58 | /** |
||
59 | * Get all of the models from the database. |
||
60 | * |
||
61 | * @param string[] $columns |
||
62 | * |
||
63 | * @return Collection |
||
64 | */ |
||
65 | 12 | public function all(array $columns = ['*']) |
|
69 | |||
70 | /** |
||
71 | * Create a new basic where query clause on model. |
||
72 | * |
||
73 | * @param string|array $column |
||
74 | * @param string $operator |
||
75 | * @param mixed $value |
||
76 | * @param string $boolean |
||
77 | * @param string[] $columns |
||
78 | * |
||
79 | * @return Collection |
||
80 | */ |
||
81 | 20 | public function where($column, $operator = '=', $value = null, $boolean = 'and', array $columns = ['*']) |
|
85 | |||
86 | /** |
||
87 | * Paginate the given query. |
||
88 | * |
||
89 | * @param int $perPage |
||
90 | * @param string[] $columns |
||
91 | * |
||
92 | * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator |
||
93 | */ |
||
94 | 8 | public function paginate($perPage = 15, array $columns = ['*']) |
|
98 | |||
99 | /** |
||
100 | * Save a new model and return the instance. |
||
101 | * |
||
102 | * @param array $attributes |
||
103 | * |
||
104 | * @return Eloquent |
||
105 | */ |
||
106 | 6 | public function create(array $attributes = []) |
|
110 | |||
111 | /** |
||
112 | * Save or update the model in the database. |
||
113 | * |
||
114 | * @param mixed $identifier |
||
115 | * @param array $attributes |
||
116 | * |
||
117 | * @return Eloquent |
||
118 | */ |
||
119 | 6 | public function update($identifier, array $attributes = []) |
|
133 | |||
134 | /** |
||
135 | * Delete the model from the database. |
||
136 | * |
||
137 | * @param int $identifier |
||
138 | * |
||
139 | * @return bool|null |
||
140 | */ |
||
141 | 6 | public function delete($identifier) |
|
147 | |||
148 | /** |
||
149 | * Find a model by its primary key. |
||
150 | * |
||
151 | * @param int $identifier |
||
152 | * @param string[] $columns |
||
153 | * |
||
154 | * @return Eloquent |
||
155 | */ |
||
156 | 28 | public function find($identifier, array $columns = ['*']) |
|
160 | |||
161 | /** |
||
162 | * Find a model by its specified column and value. |
||
163 | * |
||
164 | * @param mixed $column |
||
165 | * @param mixed $value |
||
166 | * @param string[] $columns |
||
167 | * |
||
168 | * @return Eloquent |
||
169 | */ |
||
170 | 8 | public function findBy($column, $value, array $columns = ['*']) |
|
174 | |||
175 | /** |
||
176 | * Find a model by its specified columns and values presented as array. |
||
177 | * |
||
178 | * @param array $wheres |
||
179 | * @param string[] $columns |
||
180 | * |
||
181 | * @return Eloquent |
||
182 | */ |
||
183 | 8 | public function findWhere(array $wheres, array $columns = ['*']) |
|
187 | |||
188 | /** |
||
189 | * Find a models by its primary key. |
||
190 | * |
||
191 | * @param array $identifiers |
||
192 | * @param string[] $columns |
||
193 | * |
||
194 | * @return Collection |
||
195 | */ |
||
196 | public function findMany($identifiers, array $columns = ['*']) |
||
200 | |||
201 | /** |
||
202 | * Search the models in search of words in a given phrase to the specified columns. |
||
203 | * |
||
204 | * @param string $search |
||
205 | * @param array $columns |
||
206 | * @param float $threshold |
||
207 | * |
||
208 | * @return Builder |
||
209 | */ |
||
210 | public function search($search, array $columns = [], $threshold = null) |
||
217 | |||
218 | /** |
||
219 | * Returns total count of whole collection. |
||
220 | * |
||
221 | * @return int |
||
222 | */ |
||
223 | 4 | public function count() |
|
229 | |||
230 | /** |
||
231 | * Fetch collection ordered and filtrated by specified columns for specified page as paginator. |
||
232 | * |
||
233 | * @param int $page |
||
234 | * @param int $perPage |
||
235 | * @param array $columns |
||
236 | * @param array $filter |
||
237 | * @param array $sort |
||
238 | * @param string $search |
||
239 | * |
||
240 | * @return LengthAwarePaginator |
||
241 | */ |
||
242 | 12 | public function fetch( |
|
269 | |||
270 | /** |
||
271 | * Fetch collection ordered and filtrated by specified columns for specified page. |
||
272 | * |
||
273 | * @param int $page |
||
274 | * @param int $perPage |
||
275 | * @param array $columns |
||
276 | * @param array $filter |
||
277 | * @param array $sort |
||
278 | * @param string $search |
||
279 | * |
||
280 | * @return Collection |
||
281 | */ |
||
282 | 4 | public function simpleFetch( |
|
301 | |||
302 | /** |
||
303 | * Counts results for given query. |
||
304 | * |
||
305 | * @param Builder $query |
||
306 | * |
||
307 | * @return int |
||
308 | */ |
||
309 | 12 | protected function countResults(Builder $query) |
|
322 | |||
323 | /** |
||
324 | * Replace alias name in where closure with sub-queries from select. |
||
325 | * |
||
326 | * Example: |
||
327 | * |
||
328 | * SELECT table.*, (SELECT 1 FROM other_table ot WHERE ot.id = table.id) AS exists |
||
329 | * WHERE exists = 1; |
||
330 | * |
||
331 | * Will be converted to: |
||
332 | * |
||
333 | * SELECT table.*, (SELECT 1 FROM other_table ot WHERE ot.id = table.id) AS exists |
||
334 | * WHERE (SELECT 1 FROM other_table ot WHERE ot.id = table.id) = 1; |
||
335 | * |
||
336 | * @param Builder $query |
||
337 | * |
||
338 | * @return Builder |
||
339 | */ |
||
340 | 16 | protected function parseAliases(Builder $query) |
|
354 | |||
355 | /** |
||
356 | * Get sub queries and aliases from select statement. |
||
357 | * |
||
358 | * @param Builder $query |
||
359 | * |
||
360 | * @return array |
||
361 | */ |
||
362 | 2 | protected function getAliases(Builder $query) |
|
374 | |||
375 | /** |
||
376 | * Replace aliases in where statement with sub queries. |
||
377 | * |
||
378 | * @param Builder $query |
||
379 | * @param array $aliases |
||
380 | * |
||
381 | * @return void |
||
382 | */ |
||
383 | protected function replaceAliases(Builder $query, $aliases) |
||
391 | } |
||
392 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: