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
Push — master ( 00ccd2...5062f6 )
by Cristian
03:42
created

Read::enableExportButtons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\PanelTraits;
4
5
trait Read
6
{
7
    /*
8
    |--------------------------------------------------------------------------
9
    |                                   READ
10
    |--------------------------------------------------------------------------
11
    */
12
13
    /**
14
     * Find and retrieve an entry in the database or fail.
15
     *
16
     * @param  [int] The id of the row in the db to fetch.
17
     *
18
     * @return [Eloquent Collection] The row in the db.
0 ignored issues
show
Documentation introduced by
The doc-type Eloquent">[Eloquent could not be parsed: Unknown type name "[" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
19
     */
20
    public function getEntry($id)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
21
    {
22
        $entry = $this->model->findOrFail($id);
0 ignored issues
show
Bug introduced by
The property model 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...
23
24
        return $entry->withFakes();
25
    }
26
27
    /**
28
     * Make the query JOIN all relationships used in the columns, too,
29
     * so there will be less database queries overall.
30
     */
31
    public function autoEagerLoadRelationshipColumns()
32
    {
33
        $relationships = $this->getColumnsRelationships();
0 ignored issues
show
Bug introduced by
It seems like getColumnsRelationships() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
34
35
        if (count($relationships)) {
36
            $this->with($relationships);
0 ignored issues
show
Bug introduced by
It seems like with() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
        }
38
    }
39
40
    /**
41
     * Get all entries from the database.
42
     *
43
     * @return [Collection of your model]
0 ignored issues
show
Documentation introduced by
The doc-type Collection">[Collection could not be parsed: Unknown type name "[" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
44
     */
45
    public function getEntries()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
46
    {
47
        $this->autoEagerLoadRelationshipColumns();
48
49
        $entries = $this->query->get();
0 ignored issues
show
Bug introduced by
The property query 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...
50
51
        // add the fake columns for each entry
52
        foreach ($entries as $key => $entry) {
53
            $entry->addFakes($this->getFakeColumnsAsArray());
0 ignored issues
show
Bug introduced by
It seems like getFakeColumnsAsArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
        }
55
56
        return $entries;
57
    }
58
59
    /**
60
     * Get the fields for the create or update forms.
61
     *
62
     * @param  [form] create / update / both - defaults to 'both'
63
     * @param  [integer] the ID of the entity to be edited in the Update form
64
     *
65
     * @return [array] all the fields that need to be shown and their information
0 ignored issues
show
Documentation introduced by
The doc-type [array] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
66
     */
67
    public function getFields($form, $id = false)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
68
    {
69
        switch (strtolower($form)) {
70
            case 'create':
71
                return $this->getCreateFields();
0 ignored issues
show
Bug introduced by
It seems like getCreateFields() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
72
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
73
74
            case 'update':
75
                return $this->getUpdateFields($id);
0 ignored issues
show
Bug introduced by
It seems like getUpdateFields() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
76
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
77
78
            default:
79
                return $this->getCreateFields();
0 ignored issues
show
Bug introduced by
It seems like getCreateFields() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
80
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
81
        }
82
    }
83
84
    /**
85
     * Check if the create/update form has upload fields.
86
     * Upload fields are the ones that have "upload" => true defined on them.
87
     * @param  [form] create / update / both - defaults to 'both'
88
     * @param  [id] id of the entity - defaults to false
89
     * @return bool
90
     */
91
    public function hasUploadFields($form, $id = false)
92
    {
93
        $fields = $this->getFields($form, $id);
94
        $upload_fields = array_where($fields, function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
            return isset($value['upload']) && $value['upload'] == true;
96
        });
97
98
        return count($upload_fields) ? true : false;
99
    }
100
101
    /**
102
     * Enable the DETAILS ROW functionality:.
103
     *
104
     * In the table view, show a plus sign next to each entry.
105
     * When clicking that plus sign, an AJAX call will bring whatever content you want from the EntityCrudController::showDetailsRow($id) and show it to the user.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 162 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
    public function enableDetailsRow()
108
    {
109
        $this->details_row = true;
0 ignored issues
show
Bug introduced by
The property details_row 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...
110
    }
111
112
    /**
113
     * Disable the DETAILS ROW functionality:.
114
     */
115
    public function disableDetailsRow()
116
    {
117
        $this->details_row = false;
118
    }
119
120
    /**
121
     * Set the number of rows that should be show on the table page (list view).
122
     */
123
    public function setDefaultPageLength($value)
124
    {
125
        $this->default_page_length = $value;
0 ignored issues
show
Bug introduced by
The property default_page_length 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...
126
    }
127
128
    /**
129
     * Get the number of rows that should be show on the table page (list view).
130
     */
131
    public function getDefaultPageLength()
132
    {
133
        // return the custom value for this crud panel, if set using setPageLength()
134
        if ($this->default_page_length) {
135
            return $this->default_page_length;
136
        }
137
138
        // otherwise return the default value in the config file
139
        if (config('backpack.crud.default_page_length')) {
140
            return config('backpack.crud.default_page_length');
141
        }
142
143
        return 25;
144
    }
145
146
    /*
147
    |--------------------------------------------------------------------------
148
    |                                AJAX TABLE
149
    |--------------------------------------------------------------------------
150
    */
151
152
    /**
153
     * Tell the list view to use AJAX for loading multiple rows.
154
     */
155
    public function enableAjaxTable()
156
    {
157
        $this->ajax_table = true;
0 ignored issues
show
Bug introduced by
The property ajax_table 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...
158
    }
159
160
    /**
161
     * Check if ajax is enabled for the table view.
162
     * @return bool
163
     */
164
    public function ajaxTable()
165
    {
166
        return $this->ajax_table;
167
    }
168
169
    /**
170
     * Get the HTML of the cells in a table row, for a certain DB entry.
171
     * @param  Entity $entry A db entry of the current entity;
172
     * @return array         Array of HTML cell contents.
173
     */
174
    public function getRowViews($entry)
175
    {
176
        $response = [];
177
        foreach ($this->columns as $key => $column) {
0 ignored issues
show
Bug introduced by
The property columns 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...
178
            $response[] = $this->getCellView($column, $entry);
179
        }
180
181
        return $response;
182
    }
183
184
    /**
185
     * Get the HTML of a cell, using the column types.
186
     * @param  array $column
187
     * @param  Entity $entry   A db entry of the current entity;
188
     * @return HTML
189
     */
190
    public function getCellView($column, $entry)
191
    {
192
        if (! isset($column['type'])) {
193
            return \View::make('crud::columns.text')->with('crud', $this)->with('column', $column)->with('entry', $entry)->render();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 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...
194
        } else {
195
            if (view()->exists('vendor.backpack.crud.columns.'.$column['type'])) {
196
                return \View::make('vendor.backpack.crud.columns.'.$column['type'])->with('crud', $this)->with('column', $column)->with('entry', $entry)->render();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 163 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...
197
            } else {
198
                if (view()->exists('crud::columns.'.$column['type'])) {
0 ignored issues
show
Bug introduced by
The method exists does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

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...
199
                    return \View::make('crud::columns.'.$column['type'])->with('crud', $this)->with('column', $column)->with('entry', $entry)->render();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 152 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...
200
                } else {
201
                    return \View::make('crud::columns.text')->with('crud', $this)->with('column', $column)->with('entry', $entry)->render();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 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...
202
                }
203
            }
204
        }
205
    }
206
207
    /*
208
    |--------------------------------------------------------------------------
209
    |                                EXPORT BUTTONS
210
    |--------------------------------------------------------------------------
211
    */
212
213
    /**
214
     * Tell the list view to show the DataTables export buttons.
215
     */
216
    public function enableExportButtons()
217
    {
218
        $this->export_buttons = true;
0 ignored issues
show
Bug introduced by
The property export_buttons 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...
219
    }
220
221
    /**
222
     * Check if export buttons are enabled for the table view.
223
     * @return bool
224
     */
225
    public function exportButtons()
226
    {
227
        return $this->export_buttons;
228
    }
229
}
230