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 ( 2cd6d2...274790 )
by Cristian
15s queued 12s
created

RoleCrudController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 151
Duplicated Lines 20.53 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 31
loc 151
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 20 20 4
B setupListOperation() 0 58 2
A setupCreateOperation() 0 8 1
A setupUpdateOperation() 0 8 1
A addFields() 0 27 2
A getGuardTypes() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\RoleStoreCrudRequest as StoreRequest;
7
use Backpack\PermissionManager\app\Http\Requests\RoleUpdateCrudRequest as UpdateRequest;
8
9
// VALIDATION
10
11
class RoleCrudController extends CrudController
12
{
13
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
14
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
15
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
16
    use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
17
18 View Code Duplication
    public function setup()
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...
19
    {
20
        $this->role_model = $role_model = config('backpack.permissionmanager.models.role');
0 ignored issues
show
Bug introduced by
The property role_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...
21
        $this->permission_model = $permission_model = config('backpack.permissionmanager.models.permission');
0 ignored issues
show
Bug introduced by
The property permission_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...
Unused Code introduced by
$permission_model is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
22
23
        $this->crud->setModel($role_model);
24
        $this->crud->setEntityNameStrings(trans('backpack::permissionmanager.role'), trans('backpack::permissionmanager.roles'));
25
        $this->crud->setRoute(backpack_url('role'));
0 ignored issues
show
Bug introduced by
It seems like backpack_url('role') 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...
26
27
        // deny access according to configuration file
28
        if (config('backpack.permissionmanager.allow_role_create') == false) {
29
            $this->crud->denyAccess('create');
0 ignored issues
show
Documentation introduced by
'create' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30
        }
31
        if (config('backpack.permissionmanager.allow_role_update') == false) {
32
            $this->crud->denyAccess('update');
0 ignored issues
show
Documentation introduced by
'update' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
33
        }
34
        if (config('backpack.permissionmanager.allow_role_delete') == false) {
35
            $this->crud->denyAccess('delete');
0 ignored issues
show
Documentation introduced by
'delete' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
        }
37
    }
38
39
    public function setupListOperation()
40
    {
41
        /**
42
         * Show a column for the name of the role.
43
         */
44
        $this->crud->addColumn([
45
            'name'  => 'name',
46
            'label' => trans('backpack::permissionmanager.name'),
47
            'type'  => 'text',
48
        ]);
49
50
        /**
51
         * Show a column with the number of users that have that particular role.
52
         *
53
         * Note: To account for the fact that there can be thousands or millions
54
         * of users for a role, we did not use the `relationship_count` column,
55
         * but instead opted to append a fake `user_count` column to
56
         * the result, using Laravel's `withCount()` method.
57
         * That way, no users are loaded.
58
         */
59
        $this->crud->query->withCount('users');
60
        $this->crud->addColumn([
61
            'label'     => trans('backpack::permissionmanager.users'),
62
            'type'      => 'text',
63
            'name'      => 'users_count',
64
            'wrapper'   => [
65
                'href' => function ($crud, $column, $entry, $related_key) {
0 ignored issues
show
Unused Code introduced by
The parameter $related_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...
66
                    return backpack_url('user?role='.$entry->getKey());
67
                },
68
            ],
69
            'suffix'    => ' users',
70
        ]);
71
72
        /**
73
         * In case multiple guards are used, show a column for the guard.
74
         */
75
        if (config('backpack.permissionmanager.multiple_guards')) {
76
            $this->crud->addColumn([
77
                'name'  => 'guard_name',
78
                'label' => trans('backpack::permissionmanager.guard_type'),
79
                'type'  => 'text',
80
            ]);
81
        }
82
83
        /**
84
         * Show the exact permissions that role has.
85
         */
86
        $this->crud->addColumn([
87
            // n-n relationship (with pivot table)
88
            'label'     => ucfirst(trans('backpack::permissionmanager.permission_plural')),
89
            'type'      => 'select_multiple',
90
            'name'      => 'permissions', // the method that defines the relationship in your Model
91
            'entity'    => 'permissions', // the method that defines the relationship in your Model
92
            'attribute' => 'name', // foreign key attribute that is shown to user
93
            'model'     => $this->permission_model, // foreign key model
94
            'pivot'     => true, // on create&update, do you need to add/delete pivot table entries?
95
        ]);
96
    }
97
98
    public function setupCreateOperation()
99
    {
100
        $this->addFields();
101
        $this->crud->setValidation(StoreRequest::class);
102
103
        //otherwise, changes won't have effect
104
        \Cache::forget('spatie.permission.cache');
105
    }
106
107
    public function setupUpdateOperation()
108
    {
109
        $this->addFields();
110
        $this->crud->setValidation(UpdateRequest::class);
111
112
        //otherwise, changes won't have effect
113
        \Cache::forget('spatie.permission.cache');
114
    }
115
116
    private function addFields()
117
    {
118
        $this->crud->addField([
119
            'name'  => 'name',
120
            'label' => trans('backpack::permissionmanager.name'),
121
            'type'  => 'text',
122
        ]);
123
124
        if (config('backpack.permissionmanager.multiple_guards')) {
125
            $this->crud->addField([
126
                'name'    => 'guard_name',
127
                'label'   => trans('backpack::permissionmanager.guard_type'),
128
                'type'    => 'select_from_array',
129
                'options' => $this->getGuardTypes(),
130
            ]);
131
        }
132
133
        $this->crud->addField([
134
            'label'     => ucfirst(trans('backpack::permissionmanager.permission_plural')),
135
            'type'      => 'checklist',
136
            'name'      => 'permissions',
137
            'entity'    => 'permissions',
138
            'attribute' => 'name',
139
            'model'     => $this->permission_model,
140
            'pivot'     => true,
141
        ]);
142
    }
143
144
    /*
145
     * Get an array list of all available guard types
146
     * that have been defined in app/config/auth.php
147
     *
148
     * @return array
149
     **/
150 View Code Duplication
    private function getGuardTypes()
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...
151
    {
152
        $guards = config('auth.guards');
153
154
        $returnable = [];
155
        foreach ($guards as $key => $details) {
156
            $returnable[$key] = $key;
157
        }
158
159
        return $returnable;
160
    }
161
}
162