Completed
Pull Request — master (#78)
by Brandon
02:03
created

ActivityLogDataTable   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 103
ccs 0
cts 30
cp 0
rs 10
c 2
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B dataTable() 0 22 5
A query() 0 5 1
A html() 0 7 1
A getColumns() 0 10 1
A getBuilderParameters() 0 17 1
A filename() 0 4 1
1
<?php
2
3
namespace App\DataTables;
4
5
use App\User;
6
use Yajra\DataTables\Services\DataTable;
7
use Spatie\Activitylog\Models\Activity;
8
9
class ActivityLogDataTable extends DataTable
10
{
11
    /**
12
     * Build DataTable class.
13
     *
14
     * @return \Yajra\DataTables\DataTableAbstract
15
     */
16
    public function dataTable($query)
17
    {
18
        return datatables($query)
1 ignored issue
show
Bug introduced by
The method editColumn does only exist in Yajra\DataTables\DataTableAbstract, but not in Yajra\DataTables\DataTables.

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...
19
            ->editColumn('causer_id', function ($activity) {
20
                if ($activity->causer_id) {
21
                    return '<a href="/' . ($activity->causer_type == "App\User" ? 'user' : 'device') . '/' . $activity->causer_id . '">' . $activity->causer->name . '</a>';
22
                } else {
23
                    return 'Application';
24
                }
25
            })
26
            ->editColumn('subject_id', function ($activity) {
27
                if ($activity->subject_id) {
28
                    return '<a href="/' . ($activity->subject_type == "App\User" ? 'user' : 'device') . '/' . $activity->subject_id . '">' . $activity->subject->name . '</a>';
29
                } else {
30
                    return 'None';
31
                }
32
            })
33
            ->editColumn('properties', function ($activity) {
34
                return $activity->properties;
35
            })
36
            ->rawColumns(['causer_id', 'subject_id', 'properties']);
37
    }
38
39
    /**
40
     * Get query source of dataTable.
41
     *
42
     * @return \Illuminate\Database\Eloquent\Builder
43
     */
44
    public function query()
45
    {
46
        $query = Activity::query();
47
        return $this->applyScopes($query);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->applyScopes($query); of type Illuminate\Database\Quer...tabase\Eloquent\Builder adds the type Illuminate\Database\Query\Builder to the return on line 47 which is incompatible with the return type documented by App\DataTables\ActivityLogDataTable::query of type Illuminate\Database\Eloquent\Builder.
Loading history...
48
    }
49
50
    /**
51
     * Optional method if you want to use html builder.
52
     *
53
     * @return \Yajra\DataTables\Html\Builder
54
     */
55
    public function html()
56
    {
57
        return $this->builder()
58
            ->columns($this->getColumns())
59
            ->minifiedAjax()
60
            ->parameters($this->getBuilderParameters());
61
    }
62
63
    /**
64
     * Get columns.
65
     *
66
     * @return array
67
     */
68
    protected function getColumns()
69
    {
70
        return [
71
            'created_at',
72
            [ 'name' => 'causer_id', 'data' => 'causer_id', 'title' => 'Actor', 'render' => null, 'searchable' => true, 'orderable' => true, 'exportable' => true, 'printable' => true, 'footer' => '' ],
73
            [ 'name' => 'description', 'data' => 'description', 'title' => 'Action', 'render' => null, 'searchable' => true, 'orderable' => true, 'exportable' => true, 'printable' => true, 'footer' => '' ],
74
            [ 'name' => 'subject_id', 'data' => 'subject_id', 'title' => 'Subject', 'render' => null, 'searchable' => true, 'orderable' => true, 'exportable' => true, 'printable' => true, 'footer' => '' ],
75
            [ 'name' => 'properties', 'data' => 'properties', 'title' => 'Changes', 'render' => null, 'searchable' => true, 'orderable' => false, 'exportable' => true, 'printable' => true, 'footer' => '' ]
76
        ];
77
    }
78
    
79
    /**
80
     * Get builder parameters.
81
     *
82
     * @return array
83
     */
84
    protected function getBuilderParameters()
85
    {
86
        return [
87
            'dom'     => 'Bfrtip',
88
            'order'   => [ [ 0, 'desc' ] ],
89
            'buttons' => [
90
                'export',
91
                'print',
92
                'reset',
93
                'reload',
94
            ],
95
            'paging' => true,
96
            'searching' => true,
97
            'info' => true,
98
            'searchDelay' => 500,
99
        ];
100
    }
101
102
    /**
103
     * Get filename for export.
104
     *
105
     * @return string
106
     */
107
    protected function filename()
108
    {
109
        return 'activitylog_' . time();
110
    }
111
}
112