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 namespace Anomaly\Streams\Platform\Model; |
||
20 | class EloquentCriteria |
||
21 | { |
||
22 | |||
23 | use Hookable; |
||
24 | use DispatchesJobs; |
||
25 | |||
26 | /** |
||
27 | * Additional available methods. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $available = [ |
||
32 | 'whereBetween', |
||
33 | 'whereNotBetween', |
||
34 | 'whereIn', |
||
35 | 'whereNotIn', |
||
36 | 'whereNull', |
||
37 | 'whereNotNull', |
||
38 | 'whereDate', |
||
39 | 'whereMonth', |
||
40 | 'whereDay', |
||
41 | 'whereYear', |
||
42 | 'whereColumn', |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * Safe builder methods. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | private $disabled = [ |
||
51 | 'delete', |
||
52 | 'update', |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * The query builder. |
||
57 | * |
||
58 | * @var Builder|\Illuminate\Database\Query\Builder |
||
59 | */ |
||
60 | protected $query; |
||
61 | |||
62 | /** |
||
63 | * Set the get method. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $method; |
||
68 | |||
69 | /** |
||
70 | * Create a new EntryCriteria instance. |
||
71 | * |
||
72 | * @param Builder $query |
||
73 | * @param string $method |
||
74 | */ |
||
75 | public function __construct(Builder $query, $method = 'get') |
||
80 | |||
81 | /** |
||
82 | * Get the paginated entries. |
||
83 | * |
||
84 | * @param int $perPage |
||
85 | * @param array $columns |
||
86 | * @param string $pageName |
||
87 | * @return array|\ArrayAccess|\IteratorAggregate|Presenter |
||
88 | */ |
||
89 | public function paginate($perPage = 15, array $columns = ['*'], $pageName = 'page') |
||
93 | |||
94 | /** |
||
95 | * Return a new search criteria. |
||
96 | * |
||
97 | * @param string $term |
||
98 | * @return SearchCriteria |
||
99 | */ |
||
100 | public function search($term) |
||
107 | |||
108 | /** |
||
109 | * Get the entries. |
||
110 | * |
||
111 | * @param array $columns |
||
112 | * @return Collection|Presenter|EntryPresenter |
||
113 | */ |
||
114 | public function get(array $columns = ['*']) |
||
118 | |||
119 | /** |
||
120 | * Get the entry count. |
||
121 | * |
||
122 | * @param array $columns |
||
123 | * @return int |
||
124 | */ |
||
125 | public function count(array $columns = ['*']) |
||
129 | |||
130 | /** |
||
131 | * Get the aggregate sum. |
||
132 | * |
||
133 | * @param $column |
||
134 | * @return int |
||
135 | */ |
||
136 | public function sum($column) |
||
140 | |||
141 | /** |
||
142 | * Get the aggregate max. |
||
143 | * |
||
144 | * @param $column |
||
145 | * @return int |
||
146 | */ |
||
147 | public function max($column) |
||
151 | |||
152 | /** |
||
153 | * Get the aggregate min. |
||
154 | * |
||
155 | * @param $column |
||
156 | * @return int |
||
157 | */ |
||
158 | public function min($column) |
||
162 | |||
163 | /** |
||
164 | * Get the aggregate avg. |
||
165 | * |
||
166 | * @param $column |
||
167 | * @return int |
||
168 | */ |
||
169 | public function avg($column) |
||
173 | |||
174 | /** |
||
175 | * Find an entry. |
||
176 | * |
||
177 | * @param $identifier |
||
178 | * @param array $columns |
||
179 | * @return Presenter|EntryPresenter |
||
180 | */ |
||
181 | public function find($identifier, array $columns = ['*']) |
||
185 | |||
186 | /** |
||
187 | * Find an entry by column value. |
||
188 | * |
||
189 | * @param $column |
||
190 | * @param $value |
||
191 | * @param array $columns |
||
192 | * @return Presenter|EntryPresenter |
||
193 | */ |
||
194 | public function findBy($column, $value, array $columns = ['*']) |
||
200 | |||
201 | /** |
||
202 | * Return the first entry. |
||
203 | * |
||
204 | * @param array $columns |
||
205 | * @return EloquentModel|EntryInterface |
||
206 | */ |
||
207 | public function first(array $columns = ['*']) |
||
211 | |||
212 | /** |
||
213 | * Return whether the method is safe or not. |
||
214 | * |
||
215 | * @param $name |
||
216 | * @return bool |
||
217 | */ |
||
218 | protected function methodIsSafe($name) |
||
222 | |||
223 | /** |
||
224 | * Return whether the method |
||
225 | * exists on the query or not. |
||
226 | * |
||
227 | * @param $name |
||
228 | * @return bool |
||
229 | */ |
||
230 | protected function methodExists($name) |
||
234 | |||
235 | /** |
||
236 | * Route through __call. |
||
237 | * |
||
238 | * @param $name |
||
239 | * @return Builder|null |
||
240 | */ |
||
241 | public function __get($name) |
||
245 | |||
246 | /** |
||
247 | * Call the method on the query. |
||
248 | * |
||
249 | * @param $name |
||
250 | * @param $arguments |
||
251 | * @return Builder|null |
||
252 | */ |
||
253 | public function __call($name, $arguments) |
||
282 | |||
283 | /** |
||
284 | * Return the string. |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | public function __toString() |
||
292 | } |
||
293 |
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: