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) |
||
45 | |||
46 | /** |
||
47 | * Counts current query. |
||
48 | * |
||
49 | * @return int |
||
50 | */ |
||
51 | public function count() |
||
73 | |||
74 | /** |
||
75 | * Check if model use SoftDeletes trait |
||
76 | * |
||
77 | * @return boolean |
||
78 | */ |
||
79 | protected function modelUseSoftDeletes() |
||
87 | |||
88 | /** |
||
89 | * Change withTrashed flag value. |
||
90 | * |
||
91 | * @param bool $withTrashed |
||
92 | * @return $this |
||
93 | */ |
||
94 | public function withTrashed($withTrashed = true) |
||
100 | |||
101 | /** |
||
102 | * Change onlyTrashed flag value. |
||
103 | * |
||
104 | * @param bool $onlyTrashed |
||
105 | * @return $this |
||
106 | */ |
||
107 | public function onlyTrashed($onlyTrashed = true) |
||
113 | } |
||
114 |
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: