Completed
Push — master ( 20a6e3...887da8 )
by Abdelrahman
04:09 queued 02:12
created

AbilitiesDataTable::ajax()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\DataTables\Adminarea;
6
7
use Cortex\Auth\Models\Ability;
8
use Cortex\Foundation\DataTables\AbstractDataTable;
9
use Cortex\Auth\Transformers\Adminarea\AbilityTransformer;
10
11
class AbilitiesDataTable extends AbstractDataTable
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    protected $model = Ability::class;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected $transformer = AbilityTransformer::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
        $currentUser = $this->request->user($this->request->route('guard'));
31
32
        $query = $currentUser->can('superadmin') ? app($this->model)->query() : app($this->model)->query()->whereIn('id', $currentUser->getAbilities()->pluck('id')->toArray());
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 176 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...
33
34
        return $this->applyScopes($query);
35
    }
36
37
    /**
38
     * Display ajax response.
39
     *
40
     * @return \Illuminate\Http\JsonResponse
41
     */
42
    public function ajax()
43
    {
44
        return datatables($this->query())
0 ignored issues
show
Bug introduced by
The method setTransformer 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...
45
            ->setTransformer(app($this->transformer))
46
            ->orderColumn('title', 'title->"$.'.app()->getLocale().'" $1')
47
            ->make(true);
48
    }
49
50
    /**
51
     * Get columns.
52
     *
53
     * @return array
54
     */
55
    protected function getColumns(): array
56
    {
57
        $link = config('cortex.foundation.route.locale_prefix')
58
            ? '"<a href=\""+routes.route(\'adminarea.abilities.edit\', {ability: full.id, locale: \''.$this->request->segment(1).'\'})+"\">"+data+"</a>"'
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...
59
            : '"<a href=\""+routes.route(\'adminarea.abilities.edit\', {ability: full.id})+"\">"+data+"</a>"';
60
61
        return [
62
            'title' => ['title' => trans('cortex/auth::common.title'), 'render' => $link, 'responsivePriority' => 0],
63
            'name' => ['title' => trans('cortex/auth::common.name')],
64
            'created_at' => ['title' => trans('cortex/auth::common.created_at'), 'render' => "moment(data).format('YYYY-MM-DD, hh:mm:ss A')"],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 142 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...
65
            'updated_at' => ['title' => trans('cortex/auth::common.updated_at'), 'render' => "moment(data).format('YYYY-MM-DD, hh:mm:ss A')"],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 142 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...
66
        ];
67
    }
68
}
69