| Conditions | 17 |
| Paths | 22 |
| Total Lines | 41 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 10 | public function __construct($configuration) |
||
| 11 | { |
||
| 12 | // setup roles |
||
| 13 | foreach ($configuration['roles'] as $role => $parents) { |
||
| 14 | $this->addRole(new Role($role), $parents); |
||
| 15 | } |
||
| 16 | // setup resources |
||
| 17 | if (array_key_exists('resources', $configuration)) { |
||
| 18 | foreach ($configuration['resources'] as $resource => $parent) { |
||
| 19 | $this->addResource(new Resource($resource), $parent); |
||
| 20 | } |
||
| 21 | } |
||
| 22 | foreach ($configuration['guards'] as $guardType => $guardRules) { |
||
| 23 | if (!in_array($guardType, ['resources', 'routes', 'callables'])) { |
||
| 24 | throw new \Exception('Error Processing Request'); |
||
| 25 | } |
||
| 26 | foreach ($guardRules as $rule) { |
||
| 27 | if ('callables' === $guardType && 2 !== count($rule)) { |
||
| 28 | throw new \Exception('Error Processing Request'); |
||
| 29 | } |
||
| 30 | if ('callables' !== $guardType && 3 !== count($rule)) { |
||
| 31 | if (('resources' === $guardType && 2 !== count($rule)) || 'routes' === $guardType) { |
||
| 32 | throw new \Exception('Error Processing Request'); |
||
| 33 | } else { |
||
| 34 | $rule[] = null; |
||
| 35 | } |
||
| 36 | } |
||
| 37 | list($resource, $roles) = $rule; |
||
| 38 | $privileges = (3 === count($rule) ? $rule[2] : null); |
||
| 39 | if ('callables' === $guardType) { |
||
| 40 | $resource = 'callable/'.$resource; |
||
| 41 | $this->addResource(new Resource($resource)); |
||
| 42 | } elseif ('routes' === $guardType) { |
||
| 43 | $resource = 'route'.$resource; |
||
| 44 | $this->addResource(new Resource($resource)); |
||
| 45 | $privileges = array_map('strtolower', $privileges); |
||
| 46 | } |
||
| 47 | $this->allow($roles, $resource, $privileges); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | } |
||
| 51 | } |
||
| 52 |