ArticlesDataTable   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 169
Duplicated Lines 9.47 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 16
loc 169
rs 10
c 0
b 0
f 0
wmc 6
lcom 2
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
B ajax() 0 29 1
A query() 0 7 1
A html() 0 7 1
A getColumns() 0 70 1
A getBuilderParameters() 16 16 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 Yajra\CMS\DataTables;
4
5
use Yajra\CMS\Entities\Article;
6
use Yajra\Datatables\Services\DataTable;
7
8
class ArticlesDataTable extends DataTable
9
{
10
    /**
11
     * Display ajax response.
12
     *
13
     * @return \Illuminate\Http\JsonResponse
14
     */
15
    public function ajax()
16
    {
17
        return $this->datatables
18
            ->eloquent($this->query())
19
            ->addColumn('action', 'administrator.articles.datatables.action')
20
            ->editColumn('published', function (Article $article) {
21
                return dt_check($article->published);
22
            })
23
            ->editColumn('authenticated', function (Article $article) {
24
                return dt_check($article->authenticated);
25
            })
26
            ->editColumn('is_page', function (Article $article) {
27
                return dt_check($article->is_page);
28
            })
29
            ->editColumn('hits', function (Article $article) {
30
                return '<span class="label bg-purple">' . $article->hits . '</span>';
31
            })
32
            ->editColumn('title', function (Article $article) {
33
                return view('administrator.articles.datatables.title', compact('article'))->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\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...
34
            })
35
            ->addColumn('plain_title', function (Article $article) {
36
                return $article->title;
37
            })
38
            ->addColumn('slug', function (Article $article) {
39
                return $article->present()->slug;
40
            })
41
            ->rawColumns(['is_page', 'hits', 'title', 'published', 'authenticated', 'action'])
42
            ->make(true);
43
    }
44
45
    /**
46
     * Get the query object to be processed by datatables.
47
     *
48
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
49
     */
50
    public function query()
51
    {
52
        $articles = Article::select('articles.*', 'categories.title as category_title')
53
                           ->join('categories', 'categories.id', '=', 'articles.category_id');
54
55
        return $this->applyScopes($articles);
56
    }
57
58
    /**
59
     * Optional method if you want to use html builder.
60
     *
61
     * @return \Yajra\Datatables\Html\Builder
62
     */
63
    public function html()
64
    {
65
        return $this->builder()
66
                    ->columns($this->getColumns())
67
                    ->ajax('')
68
                    ->parameters($this->getBuilderParameters());
69
    }
70
71
    /**
72
     * Get columns.
73
     *
74
     * @return array
75
     */
76
    private function getColumns()
77
    {
78
        return [
79
            [
80
                'data'  => 'id',
81
                'name'  => 'articles.id',
82
                'title' => trans('cms::article.datatable.columns.id'),
83
                'width' => '20px',
84
            ],
85
            [
86
                'data'  => 'title',
87
                'name'  => 'articles.title',
88
                'title' => trans('cms::article.datatable.columns.title'),
89
            ],
90
            [
91
                'data'    => 'alias',
92
                'name'    => 'articles.alias',
93
                'visible' => false,
94
            ],
95
            [
96
                'data'    => 'categories.title',
97
                'title'   => trans('cms::article.datatable.columns.category'),
98
                'visible' => false,
99
                'data'    => 'category_title',
100
            ],
101
            [
102
                'data'  => 'published',
103
                'name'  => 'articles.published',
104
                'width' => '20px',
105
                'title' => '<i class="fa fa-check-circle" data-toggle="tooltip" data-title="' . trans('cms::article.datatable.columns.published') . '"></i>',
106
            ],
107
            [
108
                'data'  => 'authenticated',
109
                'name'  => 'articles.authenticated',
110
                'width' => '20px',
111
                'title' => '<i class="fa fa-key" data-toggle="tooltip" data-title="' . trans('cms::article.datatable.columns.authenticated') . '"></i>',
112
            ],
113
            [
114
                'data'  => 'order',
115
                'name'  => 'articles.order',
116
                'width' => '20px',
117
                'title' => '<i class="fa fa-list" data-toggle="tooltip" data-title="' . trans('cms::article.datatable.columns.order') . '"></i>',
118
            ],
119
            [
120
                'data'  => 'hits',
121
                'name'  => 'articles.hits',
122
                'width' => '20px',
123
                'title' => '<i class="fa fa-eye" data-toggle="tooltip" data-title="' . trans('cms::article.datatable.columns.hits') . '"></i>',
124
            ],
125
            [
126
                'data'  => 'is_page',
127
                'name'  => 'articles.is_page',
128
                'title' => trans('cms::article.datatable.columns.is_page'),
129
            ],
130
            [
131
                'data'  => 'updated_at',
132
                'name'  => 'articles.updated_at',
133
                'title' => trans('cms::article.datatable.columns.updated_at'),
134
                'width' => '100px',
135
            ],
136
            [
137
                'data'       => 'action',
138
                'title'      => trans('cms::article.datatable.columns.action'),
139
                'width'      => '134px',
140
                'searchable' => false,
141
                'orderable'  => false,
142
                'className'  => 'text-center',
143
            ],
144
        ];
145
    }
146
147
    /**
148
     * @return array
149
     */
150 View Code Duplication
    protected function getBuilderParameters()
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...
151
    {
152
        return [
153
            'stateSave' => true,
154
            'buttons'   => [
155
                [
156
                    'extend' => 'create',
157
                    'text'   => '<i class="fa fa-plus"></i>&nbsp;&nbsp;' . trans('cms::article.datatable.buttons.create'),
158
                ],
159
                'export',
160
                'print',
161
                'reset',
162
                'reload',
163
            ],
164
        ];
165
    }
166
167
    /**
168
     * Get filename for export.
169
     *
170
     * @return string
171
     */
172
    protected function filename()
173
    {
174
        return 'articles';
175
    }
176
}
177