Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#710)
by Cristian
07:14
created

AjaxTable::format()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 13
c 1
b 1
f 0
nc 4
nop 1
dl 0
loc 22
rs 9.2
1
<?php
2
3
namespace Backpack\CRUD\app\Http\Controllers\CrudFeatures;
4
5
trait AjaxTable
6
{
7
    /**
8
     * Respond with the JSON of one or more rows, depending on the POST parameters.
9
     * @return JSON Array of cells in HTML form.
10
     */
11
    private $input;
12
    private $totalRows = 0;
13
    private $filteredRows = 0;
14
    private $versionTransformer;
15
16
    /**
17
     * The search function. This function it's called by the data table.
18
     *
19
     * @return array
20
     */
21
    public function search()
0 ignored issues
show
Coding Style introduced by
search uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
22
    {
23
        $this->crud->hasAccessOrFail('list');
0 ignored issues
show
Bug introduced by
The property crud does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
25
        $this->input = $_REQUEST;
26
        $this->totalRows = $this->filteredRows = $this->crud->getEntries()->count();
27
28
        $data = $this->crud->getEntriesWithConditions(
29
            $this->input['length'],
30
            $this->input['start'],
31
            $this->addAjaxOrderBy()[0],
32
            $this->addAjaxOrderBy()[1],
33
            $this->addSearchConditions()
34
        );
35
36
        if ($this->addSearchConditions() !== null) {
37
            $this->filteredRows = $data->count();
38
        }
39
40
        return $this->make($data);
41
    }
42
43
    /**
44
     * Formats the row of the table from the entry(instance of a model).
45
     *
46
     * @param $entry
47
     * @return array
48
     */
49
    private function format($entry)
50
    {
51
        $row_items = $this->crud->getRowViews($entry, $this->crud);
52
53
        // add the buttons as the last column
54
        if ($this->crud->buttons->where('stack', 'line')->count()) {
55
            $row_items[] = \View::make('crud::inc.button_stack', ['stack' => 'line'])
56
                                ->with('crud', $this->crud)
57
                                ->with('entry', $entry)
58
                                ->render();
59
        }
60
61
        // add the details_row buttons as the first column
62
        if ($this->crud->details_row) {
63
            array_unshift($row_items, \View::make('crud::columns.details_row_button')
64
                                           ->with('crud', $this->crud)
65
                                           ->with('entry', $entry)
66
                                           ->render());
67
        }
68
69
        return $row_items;
70
    }
71
72
    /**
73
     * Created the array to be fed to the data table.
74
     * @param $data
75
     * @return array
76
     */
77
    private function make($data)
78
    {
79
        $rows = [];
80
        foreach ($data as $row) {
81
            $rows[] = $this->format($row);
82
        }
83
84
        return [
85
            'draw'            => (isset($this->input['draw']) ? (int) $this->input['draw'] : 0),
86
            'recordsTotal'    => $this->totalRows,
87
            'recordsFiltered' => $this->filteredRows,
88
            'data'            => $rows,
89
        ];
90
    }
91
92
    /**
93
     * Checks of teh user has anything written in the search bar and if so a string with the filtered is returned.
94
     * In case no filter is detected null is returned.
95
     *
96
     * @return null | string $filter
97
     */
98
    private function addSearchConditions()
99
    {
100
        if (isset($this->input['search']) && isset($this->input['search']['value'])) {
101
            $filter = $this->input['search']['value'];
102
            if ($filter !== '') {
103
                return $filter;
104
            }
105
        }
106
    }
107
108
    /**
109
     * Checks if the user tried to order a column and if so an array with the
110
     * column to be order and the direction are returned.
111
     *
112
     * @return array [column, direction]
113
     */
114
    public function addAjaxOrderBy()
115
    {
116
        if (isset($this->input['order']) && isset($this->input['order'][0])) {
117
            $orderBy = $this->input['order'][0]['column'];
118
            if (isset($this->crud->columns[(int) $orderBy]['name'])) {
119
                return [$this->crud->columns[(int) $orderBy]['name'], $this->input['order'][0]['dir']];
120
            }
121
        }
122
123
        return [null, null];
124
    }
125
}
126