Completed
Push — master ( 08375c...9f214f )
by Mohamed
11:05
created

UserDataTable   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 139
Duplicated Lines 15.11 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 21
loc 139
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B dataTable() 0 61 5
A query() 0 4 1
A html() 21 21 1
A getColumns() 0 15 1
A filename() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Microboard\DataTables;
4
5
use App\User;
6
use Yajra\DataTables\DataTableAbstract;
7
use Yajra\DataTables\Html\Builder;
8
use Yajra\DataTables\Html\Button;
9
use Yajra\DataTables\Html\Column;
10
use Yajra\DataTables\Services\DataTable;
11
12
class UserDataTable extends DataTable
13
{
14
    /**
15
     * Build DataTable class.
16
     *
17
     * @param mixed $query Results from query() method.
18
     * @return DataTableAbstract
19
     */
20
    public function dataTable($query)
21
    {
22
        return datatables()
0 ignored issues
show
Bug introduced by
The method eloquent does only exist in Yajra\DataTables\DataTables, but not in Yajra\DataTables\DataTableAbstract.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
23
            ->eloquent($query)
24
            ->editColumn('role_id', function (User $user) {
25
                if (auth()->user()->can('view', $user->role)) {
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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
82
    /**
83
     * Get query source of dataTable.
84
     *
85
     * @param User $model
86
     * @return \Illuminate\Database\Eloquent\Builder
87
     */
88
    public function query(User $model)
89
    {
90
        return $model->newQuery()->with(['role']);
91
    }
92
93
    /**
94
     * Optional method if you want to use html builder.
95
     *
96
     * @return Builder
97
     */
98 View Code Duplication
    public function html()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        return $this->builder()
101
            ->language(trans('microboard::datatable', []))
102
            ->addTableClass('table table-striped table-hover table-sm align-items-center')
103
            ->columns($this->getColumns())
104
            ->setTableId('user-table')
105
            ->autoWidth(false)
106
            ->orderBy(0)
107
            ->minifiedAjax()
108
            ->dom("<'card'" .
109
                "<'card-header border-0'<'row align-items-center'<'col-12 col-sm-8 col-md-6'<'row no-gutters'<'col-4'l><'col-8'f>>><'col-12 col-sm-4 col-md-6 text-right'B>>>" .
110
                "<'table-responsive't>" .
111
                "<'card-footer'<'row'<'col-12 col-sm-6'i><'col-12 col-sm-6'p>>>" .
112
                "r>")
113
            ->buttons(
114
                Button::make('print')->text('<span>' . trans('microboard::datatable.print') . '</span><i class="fa fa-print"></i>'),
115
                Button::make('excel')->text('<span>' . trans('microboard::datatable.excel') . '</span><i class="fa fa-file-excel"></i>'),
116
                Button::make('reload')->text('<span>' . trans('microboard::datatable.reload') . '</span><i class="fa fa-sync"></i>')
117
            );
118
    }
119
120
    /**
121
     * Get columns.
122
     *
123
     * @return array
124
     */
125
    protected function getColumns()
126
    {
127
        return [
128
            Column::make('id')->title(trans('microboard::users.fields.id'))->width('1%'),
129
            Column::make('name')->title(trans('microboard::users.fields.name'))->width('25%'),
130
            Column::make('role_id')->title(trans('microboard::users.fields.role_id')),
131
            Column::make('email')->title(trans('microboard::users.fields.email')),
132
            Column::make('created_at')->title(trans('microboard::users.fields.created_at')),
133
            Column::computed('action', '')
134
                ->exportable(false)
135
                ->printable(false)
136
                ->width('1%')
137
                ->addClass('text-right')
138
        ];
139
    }
140
141
    /**
142
     * Get filename for export.
143
     *
144
     * @return string
145
     */
146
    protected function filename()
147
    {
148
        return 'User_' . date('YmdHis');
149
    }
150
}
151