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 (#236)
by
unknown
01:14
created

UserCrudController::addUserFields()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 62

Duplication

Lines 7
Ratio 11.29 %

Importance

Changes 0
Metric Value
dl 7
loc 62
rs 8.829
c 0
b 0
f 0
cc 2
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Backpack\PermissionManager\app\Http\Controllers;
4
5
use Backpack\CRUD\app\Http\Controllers\CrudController;
6
use Backpack\PermissionManager\app\Http\Requests\UserStoreCrudRequest as StoreRequest;
7
use Backpack\PermissionManager\app\Http\Requests\UserUpdateCrudRequest as UpdateRequest;
8
use Illuminate\Support\Facades\Hash;
9
10
class UserCrudController extends CrudController
11
{
12
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
13
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { store as traitStore; }
14
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { update as traitUpdate; }
15
    use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
16
17
    public function setup()
18
    {
19
        $this->crud->setModel(config('backpack.permissionmanager.models.user'));
20
        $this->crud->setEntityNameStrings(trans('backpack::permissionmanager.user'), trans('backpack::permissionmanager.users'));
21
        $this->crud->setRoute(backpack_url('user'));
0 ignored issues
show
Bug introduced by
It seems like backpack_url('user') targeting backpack_url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Backpack\CRUD\app\Librar...l\CrudPanel::setRoute() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
22
    }
23
24
    public function setupListOperation()
25
    {
26
        $this->crud->setColumns([
27
            [
28
                'name'  => 'name',
29
                'label' => trans('backpack::permissionmanager.name'),
30
                'type'  => 'text',
31
            ],
32
            [
33
                'name'  => 'email',
34
                'label' => trans('backpack::permissionmanager.email'),
35
                'type'  => 'email',
36
            ],
37
            [ // n-n relationship (with pivot table)
38
                'label'     => trans('backpack::permissionmanager.roles'), // Table column heading
39
                'type'      => 'select_multiple',
40
                'name'      => 'roles', // the method that defines the relationship in your Model
41
                'entity'    => 'roles', // the method that defines the relationship in your Model
42
                'attribute' => 'name', // foreign key attribute that is shown to user
43
                'model'     => config('permission.models.role'), // foreign key model
44
            ],
45
            [ // n-n relationship (with pivot table)
46
                'label'     => trans('backpack::permissionmanager.extra_permissions'), // Table column heading
47
                'type'      => 'select_multiple',
48
                'name'      => 'permissions', // the method that defines the relationship in your Model
49
                'entity'    => 'permissions', // the method that defines the relationship in your Model
50
                'attribute' => 'name', // foreign key attribute that is shown to user
51
                'model'     => config('permission.models.permission'), // foreign key model
52
            ],
53
        ]);
54
55 View Code Duplication
        if (config('backpack.base.authentication_column') !== 'email') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
            $this->crud->addColumn([
57
                'name'  => config('backpack.base.authentication_column'),
58
                'type'  => 'text',
59
                'label' => config('backpack.base.authentication_column_name'),
60
            ])->afterColumn('name');
61
        }
62
63
        // Role Filter
64
        $this->crud->addFilter(
65
            [
66
                'name'  => 'role',
67
                'type'  => 'dropdown',
68
                'label' => trans('backpack::permissionmanager.role'),
69
            ],
70
            config('permission.models.role')::all()->pluck('name', 'id')->toArray(),
71
            function ($value) { // if the filter is active
72
                $this->crud->addClause('whereHas', 'roles', function ($query) use ($value) {
73
                    $query->where('role_id', '=', $value);
74
                });
75
            }
76
        );
77
78
        // Extra Permission Filter
79
        $this->crud->addFilter(
80
            [
81
                'name'  => 'permissions',
82
                'type'  => 'select2',
83
                'label' => trans('backpack::permissionmanager.extra_permissions'),
84
            ],
85
            config('permission.models.permission')::all()->pluck('name', 'id')->toArray(),
86
            function ($value) { // if the filter is active
87
                $this->crud->addClause('whereHas', 'permissions', function ($query) use ($value) {
88
                    $query->where('permission_id', '=', $value);
89
                });
90
            }
91
        );
92
    }
93
94
    public function setupCreateOperation()
95
    {
96
        $this->addUserFields();
97
        $this->crud->setValidation(StoreRequest::class);
98
    }
99
100
    public function setupUpdateOperation()
101
    {
102
        $this->addUserFields();
103
        $this->crud->setValidation(UpdateRequest::class);
104
    }
105
106
    /**
107
     * Store a newly created resource in the database.
108
     *
109
     * @return \Illuminate\Http\RedirectResponse
110
     */
111 View Code Duplication
    public function store()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113
        $this->crud->setRequest($this->crud->validateRequest());
114
        $this->crud->setRequest($this->handlePasswordInput($this->crud->getRequest()));
115
        $this->crud->unsetValidation(); // validation has already been run
116
117
        return $this->traitStore();
118
    }
119
120
    /**
121
     * Update the specified resource in the database.
122
     *
123
     * @return \Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Backpack\CRUD\app\Http\...ers\Operations\Response?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
124
     */
125 View Code Duplication
    public function update()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        $this->crud->setRequest($this->crud->validateRequest());
128
        $this->crud->setRequest($this->handlePasswordInput($this->crud->getRequest()));
129
        $this->crud->unsetValidation(); // validation has already been run
130
131
        return $this->traitUpdate();
132
    }
133
134
    /**
135
     * Handle password input fields.
136
     */
137
    protected function handlePasswordInput($request)
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...
138
    {
139
        // Remove fields not present on the user.
140
        $request->request->remove('password_confirmation');
141
        $request->request->remove('roles_show');
142
        $request->request->remove('permissions_show');
143
144
        // Encrypt password if specified.
145
        if ($request->input('password')) {
146
            $request->request->set('password', Hash::make($request->input('password')));
147
        } else {
148
            $request->request->remove('password');
149
        }
150
151
        return $request;
152
    }
153
154
    protected function addUserFields()
155
    {
156
        $this->crud->addFields([
157
            [
158
                'name'  => 'name',
159
                'label' => trans('backpack::permissionmanager.name'),
160
                'type'  => 'text',
161
            ],
162
            [
163
                'name'  => 'email',
164
                'label' => trans('backpack::permissionmanager.email'),
165
                'type'  => 'email',
166
            ],
167
            [
168
                'name'  => 'password',
169
                'label' => trans('backpack::permissionmanager.password'),
170
                'type'  => 'password',
171
            ],
172
            [
173
                'name'  => 'password_confirmation',
174
                'label' => trans('backpack::permissionmanager.password_confirmation'),
175
                'type'  => 'password',
176
            ],
177
            [
178
                // two interconnected entities
179
                'label'             => trans('backpack::permissionmanager.user_role_permission'),
180
                'field_unique_name' => 'user_role_permission',
181
                'type'              => 'checklist_dependency',
182
                'name'              => ['roles', 'permissions'],
183
                'subfields'         => [
184
                    'primary' => [
185
                        'label'            => trans('backpack::permissionmanager.roles'),
186
                        'name'             => 'roles', // the method that defines the relationship in your Model
187
                        'entity'           => 'roles', // the method that defines the relationship in your Model
188
                        'entity_secondary' => 'permissions', // the method that defines the relationship in your Model
189
                        'attribute'        => 'name', // foreign key attribute that is shown to user
190
                        'model'            => config('permission.models.role'), // foreign key model
191
                        'pivot'            => true, // on create&update, do you need to add/delete pivot table entries?]
192
                        'number_columns'   => 3, //can be 1,2,3,4,6
193
                    ],
194
                    'secondary' => [
195
                        'label'          => ucfirst(trans('backpack::permissionmanager.permission_singular')),
196
                        'name'           => 'permissions', // the method that defines the relationship in your Model
197
                        'entity'         => 'permissions', // the method that defines the relationship in your Model
198
                        'entity_primary' => 'roles', // the method that defines the relationship in your Model
199
                        'attribute'      => 'name', // foreign key attribute that is shown to user
200
                        'model'          => config('permission.models.permission'), // foreign key model
201
                        'pivot'          => true, // on create&update, do you need to add/delete pivot table entries?]
202
                        'number_columns' => 3, //can be 1,2,3,4,6
203
                    ],
204
                ],
205
            ],
206
        ]);
207
208 View Code Duplication
        if (config('backpack.base.authentication_column') !== 'email') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
            $this->crud->addField([
210
                'name'  => config('backpack.base.authentication_column'),
211
                'type'  => 'text',
212
                'label' => config('backpack.base.authentication_column_name'),
213
            ])->afterField('name');
214
        }
215
    }
216
}
217