| Conditions | 5 |
| Paths | 1 |
| Total Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 20 | public function dataTable($query) |
||
| 21 | { |
||
| 22 | return datatables() |
||
|
|
|||
| 23 | ->eloquent($query) |
||
| 24 | ->editColumn('role_id', function (User $user) { |
||
| 25 | if (auth()->user()->can('view', $user->role)) { |
||
| 26 | $route = route('microboard.roles.show', $user->role); |
||
| 27 | return "<a href='{$route}'>{$user->role->display_name}</a>"; |
||
| 28 | } |
||
| 29 | |||
| 30 | return $user->role->display_name; |
||
| 31 | }) |
||
| 32 | ->editColumn('name', function (User $user) { |
||
| 33 | return '<div class="media align-items-center">' . |
||
| 34 | '<span class="avatar avatar-sm rounded-circle mr-3">' . |
||
| 35 | '<img alt="' . $user->name . '" src="' . $user->avatar . '"></span>' . |
||
| 36 | '<div class="media-body">' . |
||
| 37 | '<span class="mb-0 d-block">' . $user->name . '</span>' . |
||
| 38 | '<small class="mb-0" style="font-size: 10px;"><i class="fa fa-clock"></i> ' . |
||
| 39 | '<time datetime="'. $user->updated_at .'">' . |
||
| 40 | trans('microboard::users.fields.updated_at') . ' ' . |
||
| 41 | $user->updated_at->diffForHumans() . '</time></small>' . |
||
| 42 | '</div></div>'; |
||
| 43 | }) |
||
| 44 | ->editColumn('created_at', function(User $user) { |
||
| 45 | return "<time datetime='{$user->created_at}'>{$user->created_at->format('d/m/Y')}</time>"; |
||
| 46 | }) |
||
| 47 | ->addColumn('action', function (User $user) { |
||
| 48 | $html = ''; |
||
| 49 | |||
| 50 | if (auth()->user()->can('view', $user)) { |
||
| 51 | $html .= '<a href="' . route('microboard.users.show', $user) . '" ' . |
||
| 52 | 'class="table-action" data-toggle="tooltip" ' . |
||
| 53 | 'data-original-title="' . trans('microboard::users.view.action-button') . '">' . |
||
| 54 | '<i class="fas fa-eye"></i></a>'; |
||
| 55 | } |
||
| 56 | |||
| 57 | if (auth()->user()->can('update', $user)) { |
||
| 58 | $html .= '<a href="' . route('microboard.users.edit', $user) . '" ' . |
||
| 59 | 'class="table-action" data-toggle="tooltip" ' . |
||
| 60 | 'data-original-title="' . trans('microboard::users.edit.action-button') . '">' . |
||
| 61 | '<i class="fas fa-edit"></i></a>'; |
||
| 62 | } |
||
| 63 | |||
| 64 | if (auth()->user()->can('delete', $user)) { |
||
| 65 | $html .= '<form action="' . route('microboard.users.destroy', $user) . '" method="post" class="d-inline-block">' . |
||
| 66 | csrf_field() . method_field('DELETE') . |
||
| 67 | '<button type="submit" data-toggle="tooltip" ' . |
||
| 68 | 'class="bg-transparent border-0 p-0 table-action table-action-delete" ' . |
||
| 69 | 'data-original-title="' . trans('microboard::users.delete.action-button') . '" ' . |
||
| 70 | 'data-modal-title="' . trans('microboard::users.delete.title') . '" ' . |
||
| 71 | 'data-modal-text="' . trans('microboard::users.delete.text') . '" ' . |
||
| 72 | 'data-confirm="' . trans('microboard::users.delete.confirm') . '" ' . |
||
| 73 | 'data-cancel="' . trans('microboard::users.delete.cancel') . '">' . |
||
| 74 | '<i class="fas fa-trash"></i></button></form>'; |
||
| 75 | } |
||
| 76 | |||
| 77 | return $html; |
||
| 78 | }) |
||
| 79 | ->escapeColumns(['*']); |
||
| 80 | } |
||
| 81 | |||
| 151 |
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: