ActivitiesDataTable::query()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\DataTables;
6
7
use Cortex\Foundation\Transformers\ActivityTransformer;
8
9
/**
10
 * @property \Spatie\Activitylog\Traits\CausesActivity $resource
11
 * @property string                                    $tabs
12
 * @property string                                    $id
13
 */
14
class ActivitiesDataTable extends LogsDataTable
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected $transformer = ActivityTransformer::class;
20
21
    /**
22
     * Get the query object to be processed by dataTables.
23
     *
24
     * @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...
25
     */
26
    public function query()
27
    {
28
        $query = $this->resource->actions();
29
30
        return $this->applyScopes($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Illuminate\Databa...nt\Relations\MorphMany>, but the function expects a object<Illuminate\Databa...abase\Eloquent\Builder>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
31
    }
32
33
    /**
34
     * Display ajax response.
35
     *
36
     * @return \Illuminate\Http\JsonResponse
37
     */
38
    public function ajax()
39
    {
40
        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...
41
            ->setTransformer(app($this->transformer))
42
            ->make(true);
43
    }
44
45
    /**
46
     * Get columns.
47
     *
48
     * @return array
49
     */
50
    protected function getColumns(): array
51
    {
52
        return [
53
            'details' => ['title' => '', 'data' => null, 'defaultContent' => '', 'class' => 'dt-details-control', 'searchable' => false, 'orderable' => false],
54
            'subject' => ['title' => trans('cortex/foundation::common.subject'), 'name' => 'subject.name', 'searchable' => false, 'orderable' => false, 'render' => 'full.subject_route ? "<a href=\""+full.subject_route+"\">"+data+"</a>" : data', 'responsivePriority' => 0],
55
            'description' => ['title' => trans('cortex/foundation::common.description'), 'orderable' => false],
56
            'created_at' => ['title' => trans('cortex/foundation::common.date'), 'render' => "moment(data).format('YYYY-MM-DD, hh:mm:ss A')"],
57
        ];
58
    }
59
}
60