Completed
Push — develop ( 76a853...d8706c )
by Abdelrahman
01:19
created

AttributesDataTable::getBuilderParameters()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Attributes\DataTables\Adminarea;
6
7
use Rinvex\Attributes\Contracts\AttributeContract;
8
use Cortex\Foundation\DataTables\AbstractDataTable;
9
use Cortex\Attributes\Transformers\Adminarea\AttributeTransformer;
10
11
class AttributesDataTable extends AbstractDataTable
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    protected $model = AttributeContract::class;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected $transformer = AttributeTransformer::class;
22
23
    /**
24
     * Get the query object to be processed by dataTables.
25
     *
26
     * @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...
27
     */
28
    public function query()
29
    {
30
        $locale = app()->getLocale();
31
        $query = app($this->model)->query()->orderBy('group', 'ASC')->orderBy('sort_order', 'ASC')->orderBy("name->\${$locale}", 'ASC');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 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...
32
33
        return $this->applyScopes($query);
34
    }
35
36
    /**
37
     * Get default builder parameters.
38
     *
39
     * @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...
40
     */
41
    protected function getBuilderParameters()
42
    {
43
        return [
44
            'keys' => true,
45
            'autoWidth' => false,
46
            '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...
47
            'buttons' => [
48
                ['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...
49
                ['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...
50
                ['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...
51
            ],
52
            'drawCallback' => 'function (settings) {
53
                var lastGroup = null;
54
                var api = this.api();
55
                var colspan = api.columns(\':visible\').count();
56
                var rows = api.rows({page:\'current\'}).nodes();
57
58
                api.column(\'group:name\', {page:\'current\'} ).data().each(function (rowGroup, rowIndex) {
59
                    if (lastGroup !== rowGroup) {
60
                        $(rows).eq(rowIndex).before(
61
                            \'<tr class="attribute-group"><td colspan="\'+colspan+\'"><strong>\'+(rowGroup ? rowGroup : "No Group")+\'</strong></td></tr>\'
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 155 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...
62
                        );
63
     
64
                        lastGroup = rowGroup;
65
                    }
66
                });
67
            }',
68
        ];
69
    }
70
71
    /**
72
     * Display ajax response.
73
     *
74
     * @return \Illuminate\Http\JsonResponse
75
     */
76
    public function ajax()
77
    {
78
        $transformer = app($this->transformer);
79
80
        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...
81
                           ->setTransformer($transformer)
82
                           ->orderColumn('name', 'name->"$.'.app()->getLocale().'" $1')
83
                           ->make(true);
84
    }
85
86
    /**
87
     * Get columns.
88
     *
89
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

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...
90
     */
91
    protected function getColumns()
92
    {
93
        $link = config('cortex.foundation.route.locale_prefix')
94
            ? '"<a href=\""+routes.route(\'adminarea.attributes.edit\', {attribute: full.slug, locale: \''.$this->request->segment(1).'\'})+"\">"+data+"</a>"'
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 158 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
            : '"<a href=\""+routes.route(\'adminarea.attributes.edit\', {attribute: full.slug})+"\">"+data+"</a>"';
96
97
        return [
98
            'name' => ['title' => trans('cortex/attributes::common.name'), 'render' => $link, 'responsivePriority' => 0],
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...
99
            'slug' => ['title' => trans('cortex/attributes::common.slug')],
100
            'type' => ['title' => trans('cortex/attributes::common.type')],
101
            'group' => ['title' => trans('cortex/attributes::common.group'), 'visible' => false],
102
            'is_collection' => ['title' => trans('cortex/attributes::common.collection')],
103
            'default' => ['title' => trans('cortex/attributes::common.default')],
104
            'created_at' => ['title' => trans('cortex/attributes::common.created_at'), 'render' => "moment(data).format('MMM Do, YYYY')"],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 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...
105
            'updated_at' => ['title' => trans('cortex/attributes::common.updated_at'), 'render' => "moment(data).format('MMM Do, YYYY')"],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 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...
106
        ];
107
    }
108
}
109