| Conditions | 5 |
| Paths | 6 |
| Total Lines | 78 |
| 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 namespace Anomaly\UsersModule\User\Permission; |
||
| 26 | public function handle( |
||
| 27 | PermissionFormBuilder $builder, |
||
| 28 | AddonCollection $addons, |
||
| 29 | Translator $translator, |
||
| 30 | Repository $config |
||
| 31 | ) { |
||
| 32 | /* @var UserInterface $user */ |
||
| 33 | $user = $builder->getEntry(); |
||
| 34 | $roles = $user->getRoles(); |
||
| 35 | $inherited = $roles->permissions(); |
||
| 36 | |||
| 37 | $fields = []; |
||
| 38 | |||
| 39 | $namespaces = array_merge(['streams'], $addons->withConfig('permissions')->namespaces()); |
||
| 40 | |||
| 41 | /* |
||
| 42 | * gather all the addons with a |
||
| 43 | * permissions configuration file. |
||
| 44 | * |
||
| 45 | * @var Addon $addon |
||
| 46 | */ |
||
| 47 | foreach ($namespaces as $namespace) { |
||
| 48 | |||
| 49 | foreach ($config->get($namespace . '::permissions', []) as $group => $permissions) { |
||
| 50 | |||
| 51 | /* |
||
| 52 | * Determine the general |
||
| 53 | * form UI components. |
||
| 54 | */ |
||
| 55 | $label = $namespace . '::permission.' . $group . '.name'; |
||
| 56 | |||
| 57 | if (!$translator->has($warning = $namespace . '::permission.' . $group . '.warning')) { |
||
| 58 | $warning = null; |
||
| 59 | } |
||
| 60 | |||
| 61 | if (!$translator->has($instructions = $namespace . '::permission.' . $group . '.instructions')) { |
||
| 62 | $instructions = null; |
||
| 63 | } |
||
| 64 | |||
| 65 | /* |
||
| 66 | * Gather the available |
||
| 67 | * permissions for the group. |
||
| 68 | */ |
||
| 69 | $available = array_combine( |
||
| 70 | array_map( |
||
| 71 | function ($permission) use ($namespace, $group) { |
||
| 72 | return $namespace . '::' . $group . '.' . $permission; |
||
| 73 | }, |
||
| 74 | $permissions |
||
| 75 | ), |
||
| 76 | array_map( |
||
| 77 | function ($permission) use ($namespace, $group) { |
||
| 78 | return $namespace . '::permission.' . $group . '.option.' . $permission; |
||
| 79 | }, |
||
| 80 | $permissions |
||
| 81 | ) |
||
| 82 | ); |
||
| 83 | |||
| 84 | /* |
||
| 85 | * Build the checkboxes field |
||
| 86 | * type to handle the UI. |
||
| 87 | */ |
||
| 88 | $fields[str_replace('.', '_', $namespace . '::' . $group)] = [ |
||
| 89 | 'label' => $label, |
||
| 90 | 'warning' => $warning, |
||
| 91 | 'instructions' => $instructions, |
||
| 92 | 'type' => 'anomaly.field_type.checkboxes', |
||
| 93 | 'value' => array_merge($user->getPermissions(), $inherited), |
||
| 94 | 'config' => [ |
||
| 95 | 'disabled' => $inherited, |
||
| 96 | 'options' => $available, |
||
| 97 | ], |
||
| 98 | ]; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | $builder->setFields($fields); |
||
| 103 | } |
||
| 104 | } |
||
| 105 |