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 |
||
| 9 | class MenuItemsDataTable extends DataTable |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var \Yajra\CMS\Entities\Navigation |
||
| 13 | */ |
||
| 14 | protected $navigation; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Display ajax response. |
||
| 18 | * |
||
| 19 | * @return \Illuminate\Http\JsonResponse |
||
| 20 | */ |
||
| 21 | public function ajax() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Get the query object to be processed by datatables. |
||
| 45 | * |
||
| 46 | * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
||
| 47 | */ |
||
| 48 | public function query() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param \Yajra\CMS\Entities\Navigation $navigation |
||
| 57 | * @return $this |
||
| 58 | */ |
||
| 59 | public function forNavigation(Navigation $navigation) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Optional method if you want to use html builder. |
||
| 68 | * |
||
| 69 | * @return \Yajra\Datatables\Html\Builder |
||
| 70 | */ |
||
| 71 | View Code Duplication | public function html() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Get columns. |
||
| 82 | * |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | private function getColumns() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | View Code Duplication | protected function getBuilderParameters() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Get filename for export. |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | protected function filename() |
||
| 146 | } |
||
| 147 |
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: