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 |
||
15 | class EloquentEngine extends QueryBuilderEngine |
||
16 | { |
||
17 | /** |
||
18 | * Select trashed records in count function for models with soft deletes trait. |
||
19 | * By default we do not select soft deleted records. |
||
20 | * |
||
21 | * @var bool |
||
22 | */ |
||
23 | protected $withTrashed = false; |
||
24 | |||
25 | /** |
||
26 | * Select only trashed records in count function for models with soft deletes trait. |
||
27 | * By default we do not select soft deleted records. |
||
28 | * |
||
29 | * @var bool |
||
30 | */ |
||
31 | protected $onlyTrashed = false; |
||
32 | |||
33 | /** |
||
34 | * @param mixed $model |
||
35 | * @param \Yajra\Datatables\Request $request |
||
36 | */ |
||
37 | public function __construct($model, Request $request) |
||
44 | |||
45 | /** |
||
46 | * Counts current query. |
||
47 | * |
||
48 | * @return int |
||
49 | */ |
||
50 | public function count() |
||
72 | |||
73 | /** |
||
74 | * Check if model use SoftDeletes trait. |
||
75 | * |
||
76 | * @return boolean |
||
77 | */ |
||
78 | protected function modelUseSoftDeletes() |
||
82 | |||
83 | /** |
||
84 | * Change withTrashed flag value. |
||
85 | * |
||
86 | * @param bool $withTrashed |
||
87 | * @return $this |
||
88 | */ |
||
89 | public function withTrashed($withTrashed = true) |
||
95 | |||
96 | /** |
||
97 | * Change onlyTrashed flag value. |
||
98 | * |
||
99 | * @param bool $onlyTrashed |
||
100 | * @return $this |
||
101 | */ |
||
102 | public function onlyTrashed($onlyTrashed = true) |
||
108 | |||
109 | /** |
||
110 | * If column name could not be resolved then use primary key. |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | protected function getPrimaryKeyName() |
||
118 | } |
||
119 |
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: