| Conditions | 17 |
| Paths | 267 |
| Total Lines | 101 |
| Code Lines | 42 |
| Lines | 11 |
| Ratio | 10.89 % |
| 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 |
||
| 47 | public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
||
| 48 | { |
||
| 49 | $config = $this->getConfig($serviceLocator, $requestedName); |
||
| 50 | $events = $serviceLocator->get($config['service']); |
||
|
|
|||
| 51 | |||
| 52 | $events->setIdentifiers($config['identifiers']); |
||
| 53 | |||
| 54 | if (method_exists($events, 'setEventPrototype')) { |
||
| 55 | $event = $serviceLocator->has($config['event']) ? $serviceLocator->get($config['event']) : new $config['event'](); |
||
| 56 | $events->setEventPrototype($event); |
||
| 57 | } |
||
| 58 | else { |
||
| 59 | $events->setEventClass($config['event']); |
||
| 60 | } |
||
| 61 | |||
| 62 | /* |
||
| 63 | * [ |
||
| 64 | * 'listeners' => [ |
||
| 65 | * 'listenerclassorservice' => event, |
||
| 66 | * 'listenerclassorservice' => [ event, event => priority ], |
||
| 67 | * 'listeneraggregate', |
||
| 68 | * 'listeneraggregate' => priority |
||
| 69 | * ] |
||
| 70 | * [ |
||
| 71 | * 'listeners' => [ |
||
| 72 | * 'event' => [ |
||
| 73 | * listener, |
||
| 74 | * listener => priority, (= [ 'priority' => priority ] ) |
||
| 75 | * listener => 'methodName', (= [ 'method' => methodName ] ) |
||
| 76 | * listener => bool, (= [ 'lazy' => bool ] ) |
||
| 77 | * listener => [ 'method' => name ], |
||
| 78 | * listener => [ 'method' => name, 'priority' => priority, 'lazy' => bool ] |
||
| 79 | * ], |
||
| 80 | * ..., |
||
| 81 | * '*' => [ |
||
| 82 | * aggragte, |
||
| 83 | * aggregate => $priority, |
||
| 84 | * aggregate => [ 'priority' => priority ] |
||
| 85 | * ], |
||
| 86 | * ] |
||
| 87 | */ |
||
| 88 | |||
| 89 | $aggregate = false; |
||
| 90 | |||
| 91 | foreach ($config['listeners'] as $event => $listeners) { |
||
| 92 | $isAggregate = '*' == $event; |
||
| 93 | |||
| 94 | foreach ($listeners as $listener => $options) { |
||
| 95 | /* Normalize options */ |
||
| 96 | if (is_int($listener)) { |
||
| 97 | $listener = $options; |
||
| 98 | $options = []; |
||
| 99 | |||
| 100 | } else if (is_int($options)) { |
||
| 101 | $options = [ 'priority' => $options ]; |
||
| 102 | |||
| 103 | } else if (is_string($options)) { |
||
| 104 | $options = [ 'method' => $options ]; |
||
| 105 | |||
| 106 | } else if (!is_array($options)) { |
||
| 107 | $options = [ 'lazy' => (bool) $options ]; |
||
| 108 | |||
| 109 | } |
||
| 110 | |||
| 111 | $options = array_merge([ 'method' => null, 'priority' => 0, 'lazy' => false ], $options); |
||
| 112 | |||
| 113 | if ($options['lazy'] && !$isAggregate) { |
||
| 114 | $aggregate = $aggregate ?: $serviceLocator->get('Core/Listener/DeferredListenerAggregate'); |
||
| 115 | $aggregate->setHook($event, $listener, $options['method'], $options['priority']); |
||
| 116 | |||
| 117 | continue; |
||
| 118 | } |
||
| 119 | |||
| 120 | View Code Duplication | if ($serviceLocator->has($listener)) { |
|
| 121 | $listener = $serviceLocator->get($listener); |
||
| 122 | |||
| 123 | } else if (class_exists($listener, true)) { |
||
| 124 | $listener = new $listener(); |
||
| 125 | |||
| 126 | } else { |
||
| 127 | throw new \UnexpectedValueException(sprintf( |
||
| 128 | 'Class %s does not exists. Cannot create listener instance.', $listener |
||
| 129 | )); |
||
| 130 | } |
||
| 131 | |||
| 132 | if ($isAggregate) { |
||
| 133 | $listener->attach($events); |
||
| 134 | } else { |
||
| 135 | $callback = $options['method'] ? [ $listener, $options['method'] ] : $listener; |
||
| 136 | $events->attach($event, $callback, $options['priority']); |
||
| 137 | } |
||
| 138 | |||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | if ($aggregate) { |
||
| 143 | $aggregate->attach($events); |
||
| 144 | } |
||
| 145 | |||
| 146 | return $events; |
||
| 147 | } |
||
| 148 | |||
| 166 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.