| Conditions | 2 |
| Paths | 2 |
| Total Lines | 62 |
| Code Lines | 36 |
| 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 |
||
| 36 | protected function showEditPermissions() |
||
| 37 | { |
||
| 38 | $permissionsData = Permission::all(['id', 'name'])->map(function($permission){ |
||
| 39 | $permission['name'] = trans(ucwords($permission['name'])); |
||
| 40 | |||
| 41 | return $permission; |
||
| 42 | })->toArray(); |
||
| 43 | |||
| 44 | $table = $this->columns()->addColumn()->add([Table::class, 'selectable'])->addStyle('cursor', 'pointer'); |
||
| 45 | $table->setModel($this->getModel($permissionsData), false); |
||
| 46 | |||
| 47 | $table->addColumn('name', Table\Column\Text::class, ['caption' => __('Permissions')]); |
||
| 48 | |||
| 49 | $table->addColumn(null, new Table\Column\Template([['i', 'class' => 'indicator arrow circle right icon']]), ['caption' => 'Test'])->setAttr('class', ['right aligned']); |
||
|
|
|||
| 50 | |||
| 51 | $this->getApp()->addStyle(' |
||
| 52 | table.selectable tr:not(.active) .indicator { |
||
| 53 | display: none; |
||
| 54 | }' |
||
| 55 | ); |
||
| 56 | |||
| 57 | $table->on('click', 'tr', $this->reload(new \atk4\ui\JsExpression('$(this).data("id")'))); |
||
| 58 | |||
| 59 | if ($permissionId = $this->permissionId()) { |
||
| 60 | $permission = Permission::findById($permissionId); |
||
| 61 | |||
| 62 | $this->putModuleVariable('permission', $permissionId); |
||
| 63 | |||
| 64 | $table->js(true)->find("tr[data-id=$permission->id]")->addClass('active'); |
||
| 65 | |||
| 66 | $rolesData = $permission->roles()->get()->map(function($role) { |
||
| 67 | $role['name'] = trans($role['name']); |
||
| 68 | |||
| 69 | return $role; |
||
| 70 | })->toArray(); |
||
| 71 | |||
| 72 | $column = $this->columns()->addColumn(); |
||
| 73 | $rolesTable = Table::addTo($column); |
||
| 74 | $rolesTable->setModel($this->getModel($rolesData), false); |
||
| 75 | |||
| 76 | $rolesTable->addColumn('name', [Table\Column\Text::class], ['caption' => __('Roles allowed to :permission', ['permission' => $permission->name])]); |
||
| 77 | |||
| 78 | $roleActions = $rolesTable->addColumn(null, [Table\Column\ActionButtons::class]); |
||
| 79 | $roleActions->addButton($this->deleteButton(), function($jQuery, $roleId) use ($permission) { |
||
| 80 | Role::findById($roleId)->revokePermissionTo($permission); |
||
| 81 | |||
| 82 | return $this->reload(); |
||
| 83 | }, __('Revoke permission to role?')); |
||
| 84 | |||
| 85 | $usersData = $permission->users()->get()->toArray(); |
||
| 86 | |||
| 87 | $usersTable = Table::addTo($column); |
||
| 88 | $usersTable->setModel($this->getModel($usersData), false); |
||
| 89 | |||
| 90 | $usersTable->addColumn('name', [Table\Column\Text::class], ['caption' => __('Users allowed to :permission', ['permission' => $permission->name])]); |
||
| 91 | |||
| 92 | $userActions = $usersTable->addColumn(null, [Table\Column\ActionButtons::class]); |
||
| 93 | $userActions->addButton($this->deleteButton(), function($jQuery, $userId) use ($permission) { |
||
| 94 | User::find($userId)->revokePermissionTo($permission); |
||
| 95 | |||
| 96 | return $this->reload(); |
||
| 97 | }, __('Revoke permission to user?')); |
||
| 98 | } |
||
| 179 |