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 (#199)
by Cristian
01:22
created

UserCrudController::addUserFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 9.0036
c 0
b 0
f 0
cc 1
nc 1
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\Http\Request;
9
use Illuminate\Support\Facades\Hash;
10
11
class UserCrudController extends CrudController
12
{
13
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
14
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { store as traitStore; }
15
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { update as traitUpdate; }
16
    use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
17
18
    public function setup()
19
    {
20
        $this->crud->setModel(config('backpack.permissionmanager.models.user'));
21
        $this->crud->setEntityNameStrings(trans('backpack::permissionmanager.user'), trans('backpack::permissionmanager.users'));
22
        $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\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...
23
    }
24
25
    public function setupListOperation()
26
    {
27
        $this->crud->setColumns([
28
            [
29
                'name'  => 'name',
30
                'label' => trans('backpack::permissionmanager.name'),
31
                'type'  => 'text',
32
            ],
33
            [
34
                'name'  => 'email',
35
                'label' => trans('backpack::permissionmanager.email'),
36
                'type'  => 'email',
37
            ],
38
            [ // n-n relationship (with pivot table)
39
               'label'     => trans('backpack::permissionmanager.roles'), // Table column heading
40
               'type'      => 'select_multiple',
41
               'name'      => 'roles', // the method that defines the relationship in your Model
42
               'entity'    => 'roles', // the method that defines the relationship in your Model
43
               'attribute' => 'name', // foreign key attribute that is shown to user
44
               'model'     => config('permission.models.role'), // foreign key model
45
            ],
46
            [ // n-n relationship (with pivot table)
47
               'label'     => trans('backpack::permissionmanager.extra_permissions'), // Table column heading
48
               'type'      => 'select_multiple',
49
               'name'      => 'permissions', // the method that defines the relationship in your Model
50
               'entity'    => 'permissions', // the method that defines the relationship in your Model
51
               'attribute' => 'name', // foreign key attribute that is shown to user
52
               'model'     => config('permission.models.permission'), // foreign key model
53
            ],
54
        ]);
55
    }
56
57
    public function setupCreateOperation()
58
    {
59
        $this->addUserFields();
60
        $this->crud->setValidation(StoreRequest::class);
61
    }
62
63
    public function setupUpdateOperation()
64
    {
65
        $this->addUserFields();
66
        $this->crud->setValidation(UpdateRequest::class);
67
    }
68
69
    /**
70
     * Store a newly created resource in the database.
71
     *
72
     * @return \Illuminate\Http\RedirectResponse
73
     */
74 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...
75
    {
76
        $this->crud->request = $this->crud->validateRequest();
77
        $this->crud->request = $this->handlePasswordInput($this->crud->request);
78
        $this->crud->unsetValidation(); // validation has already been run
79
80
        return $this->traitStore();
0 ignored issues
show
Documentation Bug introduced by
The method traitStore does not exist on object<Backpack\Permissi...ers\UserCrudController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
81
    }
82
83
    /**
84
     * Update the specified resource in the database.
85
     *
86
     * @return \Illuminate\Http\RedirectResponse
87
     */
88 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...
89
    {
90
        $this->crud->request = $this->crud->validateRequest();
91
        $this->crud->request = $this->handlePasswordInput($this->crud->request);
92
        $this->crud->unsetValidation(); // validation has already been run
93
94
        return $this->traitUpdate();
0 ignored issues
show
Bug introduced by
The method traitUpdate() does not exist on Backpack\PermissionManag...lers\UserCrudController. Did you maybe mean update()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
95
    }
96
97
    /**
98
     * Handle password input fields.
99
     */
100
    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...
101
    {
102
        // Remove fields not present on the user.
103
        $request->request->remove('password_confirmation');
104
        $request->request->remove('roles_show');
105
        $request->request->remove('permissions_show');
106
107
        // Encrypt password if specified.
108
        if ($request->input('password')) {
109
            $request->request->set('password', Hash::make($request->input('password')));
110
        } else {
111
            $request->request->remove('password');
112
        }
113
114
        return $request;
115
    }
116
117
    protected function addUserFields()
118
    {
119
        $this->crud->addFields([
120
            [
121
                'name'  => 'name',
122
                'label' => trans('backpack::permissionmanager.name'),
123
                'type'  => 'text',
124
            ],
125
            [
126
                'name'  => 'email',
127
                'label' => trans('backpack::permissionmanager.email'),
128
                'type'  => 'email',
129
            ],
130
            [
131
                'name'  => 'password',
132
                'label' => trans('backpack::permissionmanager.password'),
133
                'type'  => 'password',
134
            ],
135
            [
136
                'name'  => 'password_confirmation',
137
                'label' => trans('backpack::permissionmanager.password_confirmation'),
138
                'type'  => 'password',
139
            ],
140
            [
141
            // two interconnected entities
142
            'label'             => trans('backpack::permissionmanager.user_role_permission'),
143
            'field_unique_name' => 'user_role_permission',
144
            'type'              => 'checklist_dependency',
145
            'name'              => 'roles_and_permissions', // the methods that defines the relationship in your Model
146
            'subfields'         => [
147
                    'primary' => [
148
                        'label'            => trans('backpack::permissionmanager.roles'),
149
                        'name'             => 'roles', // the method that defines the relationship in your Model
150
                        'entity'           => 'roles', // the method that defines the relationship in your Model
151
                        'entity_secondary' => 'permissions', // the method that defines the relationship in your Model
152
                        'attribute'        => 'name', // foreign key attribute that is shown to user
153
                        'model'            => config('permission.models.role'), // foreign key model
154
                        'pivot'            => true, // on create&update, do you need to add/delete pivot table entries?]
155
                        'number_columns'   => 3, //can be 1,2,3,4,6
156
                    ],
157
                    'secondary' => [
158
                        'label'          => ucfirst(trans('backpack::permissionmanager.permission_singular')),
159
                        'name'           => 'permissions', // the method that defines the relationship in your Model
160
                        'entity'         => 'permissions', // the method that defines the relationship in your Model
161
                        'entity_primary' => 'roles', // the method that defines the relationship in your Model
162
                        'attribute'      => 'name', // foreign key attribute that is shown to user
163
                        'model'          => config('permission.models.permission'), // foreign key model
164
                        'pivot'          => true, // on create&update, do you need to add/delete pivot table entries?]
165
                        'number_columns' => 3, //can be 1,2,3,4,6
166
                    ],
167
                ],
168
            ],
169
        ]);
170
    }
171
}
172