Completed
Push — develop ( 545fca...728614 )
by Abdelrahman
01:45
created

AbstractDataTable::getBuilderParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\DataTables;
6
7
use Yajra\DataTables\Services\DataTable;
8
9
abstract class AbstractDataTable extends DataTable
10
{
11
    /**
12
     * The model class.
13
     *
14
     * @var string
15
     */
16
    protected $model;
17
18
    /**
19
     * The transformer class.
20
     *
21
     * @var string
22
     */
23
    protected $transformer;
24
25
    /**
26
     * Get columns.
27
     *
28
     * @return array
29
     */
30
    abstract protected function getColumns();
31
32
    /**
33
     * Get the query object to be processed by dataTables.
34
     *
35
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Illuminate\Database\Que...tabase\Eloquent\Builder.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
36
     */
37
    public function query()
38
    {
39
        $query = app($this->model)->query();
40
41
        return $this->applyScopes($query);
42
    }
43
44
    /**
45
     * Display ajax response.
46
     *
47
     * @return \Illuminate\Http\JsonResponse
48
     */
49
    public function ajax()
50
    {
51
        $transformer = app($this->transformer);
52
53
        return datatables()->eloquent($this->query())
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...
54
                           ->setTransformer($transformer)
55
                           ->make(true);
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
                    ->minifiedAjax()
67
                    ->columns($this->getColumns())
68
                    ->parameters($this->getBuilderParameters());
69
    }
70
71
    /**
72
     * Process DataTables needed render output.
73
     *
74
     * @param string $view
75
     * @param array  $data
76
     * @param array  $mergeData
77
     *
78
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
79
     */
80
    public function render($view, $data = [], $mergeData = [])
81
    {
82
        if ($this->request()->ajax() && $this->request()->wantsJson()) {
83
            return app()->call([$this, 'ajax']);
84
        }
85
86
        if (($action = $this->request()->get('action')) && in_array($action, $this->actions)) {
87
            if ($action === 'print') {
88
                return app()->call([$this, 'printPreview']);
89
            }
90
91
            return app()->call([$this, $action]);
92
        }
93
94
        return view($view, array_merge($this->attributes, $data), $mergeData)->with($this->dataTableVariable, $this->getHtmlBuilder());
0 ignored issues
show
Bug introduced by
The method with 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...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
95
    }
96
97
    /**
98
     * Get default builder parameters.
99
     *
100
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,boolean|str...string,string>|string>>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
101
     */
102
    protected function getBuilderParameters()
103
    {
104
        return [
105
            'keys' => true,
106
            'autoWidth' => false,
107
            'dom' => "<'row'<'col-sm-6'B><'col-sm-6'f>> <'row'r><'row'<'col-sm-12't>> <'row'<'col-sm-5'i><'col-sm-7'p>>",
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
108
            'buttons' => [
109
                ['extend' => 'create', 'text' => '<i class="fa fa-plus"></i> '.trans('cortex/foundation::common.new')], 'print', 'reset', 'reload', 'export',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 157 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
110
                ['extend' => 'colvis', 'text' => '<i class="fa fa-columns"></i> '.trans('cortex/foundation::common.columns').' <span class="caret"/>'],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 151 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
111
                ['extend' => 'pageLength', 'text' => '<i class="fa fa-list-ol"></i> '.trans('cortex/foundation::common.limit').' <span class="caret"/>'],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 153 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
112
            ],
113
        ];
114
    }
115
116
    /**
117
     * Get filename for export.
118
     *
119
     * @return string
120
     */
121
    protected function filename()
122
    {
123
        $resource = str_plural(mb_strtolower(array_last(str_replace('Contract', '', explode(class_exists($this->model) ? '\\' : '.', $this->model)))));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 151 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
124
125
        return $resource.'-export-'.date('Y-m-d').'-'.time();
126
    }
127
}
128