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 (#141)
by Owen
03:09
created

CrudPanel   B

Complexity

Total Complexity 17

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 199
rs 8.4614
wmc 17
lcom 1
cbo 16

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setModel() 0 9 2
A getModel() 0 4 1
A setRoute() 0 5 1
A setRouteName() 0 11 2
A getRoute() 0 4 1
A setEntityNameStrings() 0 5 1
A getFirstOfItsTypeInArray() 0 6 1
A sync() 0 12 3
A setSort() 0 4 1
A sort() 0 18 4
1
<?php
2
3
namespace Backpack\CRUD;
4
5
use Backpack\CRUD\PanelTraits\Access;
6
use Backpack\CRUD\PanelTraits\AutoSet;
7
use Backpack\CRUD\PanelTraits\Buttons;
8
use Backpack\CRUD\PanelTraits\Columns;
9
use Backpack\CRUD\PanelTraits\Create;
10
use Backpack\CRUD\PanelTraits\Delete;
11
use Backpack\CRUD\PanelTraits\FakeColumns;
12
use Backpack\CRUD\PanelTraits\FakeFields;
13
use Backpack\CRUD\PanelTraits\Fields;
14
use Backpack\CRUD\PanelTraits\Query;
15
use Backpack\CRUD\PanelTraits\Read;
16
use Backpack\CRUD\PanelTraits\Reorder;
17
use Backpack\CRUD\PanelTraits\Update;
18
use Backpack\CRUD\PanelTraits\ViewsAndRestoresRevisions;
19
use Backpack\CRUD\PanelTraits\AutoFocus;
20
21
class CrudPanel
0 ignored issues
show
Coding Style introduced by
The property $entity_name is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $entity_name_plural is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $reorder_label is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $reorder_max_level is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $details_row is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $ajax_table is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $export_buttons is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $create_fields is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $update_fields is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $db_column_types is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $default_page_length is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
22
{
23
    use Create, Read, Update, Delete, Reorder, Access, Columns, Fields, Query, Buttons, AutoSet, FakeFields, FakeColumns, ViewsAndRestoresRevisions, AutoFocus;
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...
24
25
    // --------------
26
    // CRUD variables
27
    // --------------
28
    // These variables are passed to the CRUD views, inside the $crud variable.
29
    // All variables are public, so they can be modified from your EntityCrudController.
30
    // All functions and methods are also public, so they can be used in your EntityCrudController to modify these variables.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 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...
31
32
    // TODO: translate $entity_name and $entity_name_plural by default, with english fallback
33
34
    public $model = "\App\Models\Entity"; // what's the namespace for your entity's model
35
    public $route; // what route have you defined for your entity? used for links.
36
    public $entity_name = 'entry'; // what name will show up on the buttons, in singural (ex: Add entity)
37
    public $entity_name_plural = 'entries'; // what name will show up on the buttons, in plural (ex: Delete 5 entities)
38
39
    public $access = ['list', 'create', 'update', 'delete'/* 'revisions', reorder', 'show', 'details_row' */];
40
41
    public $reorder = false;
42
    public $reorder_label = false;
43
    public $reorder_max_level = 3;
44
45
    public $details_row = false;
46
    public $ajax_table = false;
47
    public $export_buttons = false;
48
49
    public $columns = []; // Define the columns for the table view as an array;
50
    public $create_fields = []; // Define the fields for the "Add new entry" view as an array;
51
    public $update_fields = []; // Define the fields for the "Edit entry" view as an array;
52
53
    public $query;
54
    public $entry;
55
    public $buttons;
56
    public $db_column_types = [];
57
    public $default_page_length = false;
58
59
    // TONE FIELDS - TODO: find out what he did with them, replicate or delete
60
    public $sort = [];
61
62
    // The following methods are used in CrudController or your EntityCrudController to manipulate the variables above.
63
64
    // ------------------------------------------------------
65
    // BASICS - model, route, entity_name, entity_name_plural
66
    // ------------------------------------------------------
67
68
    /**
69
     * This function binds the CRUD to its corresponding Model (which extends Eloquent).
70
     * All Create-Read-Update-Delete operations are done using that Eloquent Collection.
71
     *
72
     * @param [string] Full model namespace. Ex: App\Models\Article
73
     */
74
    public function setModel($model_namespace)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $model_namespace is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
75
    {
76
        if (! class_exists($model_namespace)) {
77
            throw new \Exception('This model does not exist.', 404);
78
        }
79
80
        $this->model = new $model_namespace();
0 ignored issues
show
Documentation Bug introduced by
It seems like new $model_namespace() of type object is incompatible with the declared type string of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81
        $this->query = $this->model->select('*');
82
    }
83
84
    /**
85
     * Get the corresponding Eloquent Model for the CrudController, as defined with the setModel() function;.
86
     *
87
     * @return [Eloquent Collection]
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...
88
     */
89
    public function getModel()
90
    {
91
        return $this->model;
92
    }
93
94
    /**
95
     * Set the route for this CRUD.
96
     * Ex: admin/article.
97
     *
98
     * @param [string] Route name.
99
     * @param [array] Parameters.
100
     */
101
    public function setRoute($route)
102
    {
103
        $this->route = $route;
104
        $this->initButtons();
105
    }
106
107
    /**
108
     * Set the route for this CRUD using the route name.
109
     * Ex: admin.article.
110
     *
111
     * @param [string] Route name.
112
     * @param [array] Parameters.
113
     */
114
    public function setRouteName($route, $parameters = [])
115
    {
116
        $complete_route = $route.'.index';
117
118
        if (! \Route::has($complete_route)) {
119
            throw new \Exception('There are no routes for this route name.', 404);
120
        }
121
122
        $this->route = route($complete_route, $parameters);
123
        $this->initButtons();
124
    }
125
126
    /**
127
     * Get the current CrudController route.
128
     *
129
     * Can be defined in the CrudController with:
130
     * - $this->crud->setRoute('admin/article')
131
     * - $this->crud->setRouteName('admin.article')
132
     * - $this->crud->route = "admin/article"
133
     *
134
     * @return [string]
0 ignored issues
show
Documentation introduced by
The doc-type [string] 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...
135
     */
136
    public function getRoute()
137
    {
138
        return $this->route;
139
    }
140
141
    /**
142
     * Set the entity name in singular and plural.
143
     * Used all over the CRUD interface (header, add button, reorder button, breadcrumbs).
144
     *
145
     * @param [string] Entity name, in singular. Ex: article
146
     * @param [string] Entity name, in plural. Ex: articles
147
     */
148
    public function setEntityNameStrings($singular, $plural)
149
    {
150
        $this->entity_name = $singular;
151
        $this->entity_name_plural = $plural;
152
    }
153
154
    // ----------------------------------
155
    // Miscellaneous functions or methods
156
    // ----------------------------------
157
158
    /**
159
     * Return the first element in an array that has the given 'type' attribute.
160
     *
161
     * @param string $type
162
     * @param array  $array
163
     *
164
     * @return array
165
     */
166
    public function getFirstOfItsTypeInArray($type, $array)
167
    {
168
        return array_first($array, function ($item) use ($type) {
169
            return $item['type'] == $type;
170
        });
171
    }
172
173
    // ------------
174
    // TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED IN THIS FILE
175
    // ------------
176
    //
177
    // TODO:
178
    // - figure out if they are really needed
179
    // - comments inside the function to explain how they work
180
    // - write docblock for them
181
    // - place in the correct section above (CREATE, READ, UPDATE, DELETE, ACCESS, MANIPULATION)
182
183
    public function sync($type, $fields, $attributes)
184
    {
185
        if (! empty($this->{$type})) {
186
            $this->{$type} = array_map(function ($field) use ($fields, $attributes) {
187
                if (in_array($field['name'], (array) $fields)) {
188
                    $field = array_merge($field, $attributes);
189
                }
190
191
                return $field;
192
            }, $this->{$type});
193
        }
194
    }
195
196
    public function setSort($items, $order)
197
    {
198
        $this->sort[$items] = $order;
199
    }
200
201
    public function sort($items)
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...
202
    {
203
        if (array_key_exists($items, $this->sort)) {
204
            $elements = [];
205
206
            foreach ($this->sort[$items] as $item) {
207
                if (is_numeric($key = array_search($item, array_column($this->{$items}, 'name')))) {
208
                    $elements[] = $this->{$items}[$key];
209
                }
210
            }
211
212
            return $this->{$items} = array_merge($elements, array_filter($this->{$items}, function ($item) use ($items) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 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...
213
                return ! in_array($item['name'], $this->sort[$items]);
214
            }));
215
        }
216
217
        return $this->{$items};
218
    }
219
}
220