We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 2 |
Paths | 2 |
Total Lines | 58 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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) { |
||
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 | |||
162 |
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.