| Conditions | 11 |
| Paths | 25 |
| Total Lines | 52 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 27 | public function process(ContainerBuilder $container) |
||
| 28 | { |
||
| 29 | if (!$container->hasDefinition('ezpublish_rest.output.visitor.dispatcher')) { |
||
| 30 | return; |
||
| 31 | } |
||
| 32 | |||
| 33 | $definition = $container->getDefinition('ezpublish_rest.output.visitor.dispatcher'); |
||
| 34 | |||
| 35 | $visitors = []; |
||
| 36 | |||
| 37 | foreach ($container->findTaggedServiceIds('ezpublish_rest.output.visitor') as $id => $attributes) { |
||
| 38 | foreach ($attributes as $attribute) { |
||
| 39 | $priority = isset($attribute['priority']) ? $attribute['priority'] : 0; |
||
| 40 | |||
| 41 | if (!isset($attribute['regexps'])) { |
||
| 42 | throw new \LogicException('ezpublish_rest.output.visitor service tag needs a "regexps" attribute to identify the Accept header. None given.'); |
||
| 43 | } |
||
| 44 | |||
| 45 | if (is_array($attribute['regexps'])) { |
||
| 46 | $regexps = $attribute['regexps']; |
||
| 47 | } elseif (is_string($attribute['regexps'])) { |
||
| 48 | try { |
||
| 49 | $regexps = $container->getParameter($attribute['regexps']); |
||
| 50 | } catch (InvalidArgumentException $e) { |
||
| 51 | throw new \LogicException("The regexps attribute of the ezpublish_rest.output.visitor service tag can be a string matching a container parameter name. No parameter {$attribute['regexps']} could be found."); |
||
| 52 | } |
||
| 53 | } else { |
||
| 54 | throw new \LogicException('ezpublish_rest.output.visitor service tag needs a "regexps" attribute, either as an array or a string. Invalid value.'); |
||
| 55 | } |
||
| 56 | |||
| 57 | $visitors[$priority][] = [ |
||
| 58 | 'regexps' => $regexps, |
||
| 59 | 'reference' => new Reference($id), |
||
| 60 | ]; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | // sort by priority and flatten |
||
| 65 | krsort($visitors); |
||
| 66 | $visitors = call_user_func_array('array_merge', $visitors); |
||
| 67 | |||
| 68 | foreach ($visitors as $visitor) { |
||
| 69 | foreach ($visitor['regexps'] as $regexp) { |
||
| 70 | $definition->addMethodCall( |
||
| 71 | 'addVisitor', [ |
||
| 72 | $regexp, |
||
| 73 | $visitor['reference'], |
||
| 74 | ] |
||
| 75 | ); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } |
||
| 80 |