Completed
Push — develop ( cbad9d...de1065 )
by Abdelrahman
13:09
created

LogsDataTable   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 2
cbo 1
dl 0
loc 88
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A query() 0 6 1
A ajax() 0 6 1
B getBuilderParameters() 0 31 1
A getColumns() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\DataTables;
6
7
use Cortex\Foundation\Models\Log;
8
use Cortex\Foundation\Transformers\LogTransformer;
9
10
class LogsDataTable extends AbstractDataTable
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    protected $model = Log::class;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected $transformer = LogTransformer::class;
21
22
    /**
23
     * Get the query object to be processed by dataTables.
24
     *
25
     * @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...
26
     */
27
    public function query()
28
    {
29
        $query = $this->resource->activity();
0 ignored issues
show
Documentation introduced by
The property resource does not exist on object<Cortex\Foundation...taTables\LogsDataTable>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
30
31
        return $this->applyScopes($query);
32
    }
33
34
    /**
35
     * Display ajax response.
36
     *
37
     * @return \Illuminate\Http\JsonResponse
38
     */
39
    public function ajax()
40
    {
41
        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...
42
            ->setTransformer($this->transformer)
43
            ->make(true);
44
    }
45
46
    /**
47
     * Get default builder parameters.
48
     *
49
     * @return array
50
     */
51
    protected function getBuilderParameters(): array
52
    {
53
        return [
54
            'keys' => true,
55
            'retrieve' => true,
56
            'autoWidth' => false,
57
            'order' => $this->order,
58
            'dom' => $this->dom,
59
            'drawCallback' => "function (settings) {
60
                var api = this.api();
61
62
                $('#{$this->id} tbody td.dt-details-control').on('click', function () {
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Cortex\Foundation...taTables\LogsDataTable>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
63
                    var tr = $(this).closest('tr');
64
                    var row = api.row(tr);
65
66
                    if (row.child.isShown()) {
67
                        row.child.hide();
68
                        tr.removeClass('shown');
69
                    } else {
70
                        row.child(dtFormatLogDetails(row.data().properties)).show();
71
                        tr.addClass('shown');
72
                    }
73
                });
74
            }",
75
            'buttons' => [
76
                'print', 'reset', 'reload', 'export',
77
                ['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...
78
                ['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...
79
            ],
80
        ];
81
    }
82
83
    /**
84
     * Get columns.
85
     *
86
     * @return array
87
     */
88
    protected function getColumns(): array
89
    {
90
        return [
91
            'details' => ['title' => '', 'data' => null, 'defaultContent' => '', 'class' => 'dt-details-control', 'searchable' => false, 'orderable' => false],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 159 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...
92
            'causer' => ['title' => trans('cortex/foundation::common.causer'), 'name' => 'causer.username', 'searchable' => false, 'orderable' => false, 'render' => 'full.causer_route ? "<a href=\""+full.causer_route+"\">"+data+"</a>" : data', 'responsivePriority' => 0],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 271 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...
93
            'description' => ['title' => trans('cortex/foundation::common.description'), 'orderable' => false],
94
            'created_at' => ['title' => trans('cortex/foundation::common.date'), 'render' => "moment(data).format('MMM Do, YYYY - hh:mm:ss A')"],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 145 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