| Conditions | 14 |
| Paths | 27 |
| Total Lines | 70 |
| Code Lines | 39 |
| 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 |
||
| 32 | public function get($id) |
||
| 33 | { |
||
| 34 | /** @psalm-suppress TypeDoesNotContainType */ |
||
| 35 | if (!is_string($id)) { |
||
| 36 | throw new InvalidArgumentException( |
||
| 37 | sprintf( |
||
| 38 | 'ID must be a string, %s given.', |
||
| 39 | get_debug_type($id) |
||
| 40 | ) |
||
| 41 | ); |
||
| 42 | } |
||
| 43 | |||
| 44 | if ($id === StateResetter::class) { |
||
| 45 | $resetters = []; |
||
| 46 | foreach ($this->containers as $container) { |
||
| 47 | if ($container->has(StateResetter::class)) { |
||
| 48 | $resetters[] = $container->get(StateResetter::class); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | $stateResetter = new StateResetter($this); |
||
| 52 | $stateResetter->setResetters($resetters); |
||
| 53 | |||
| 54 | return $stateResetter; |
||
| 55 | } |
||
| 56 | |||
| 57 | if (TagHelper::isTagAlias($id)) { |
||
| 58 | $tags = []; |
||
| 59 | foreach ($this->containers as $container) { |
||
| 60 | if (!$container instanceof Container) { |
||
| 61 | continue; |
||
| 62 | } |
||
| 63 | if ($container->has($id)) { |
||
| 64 | /** @psalm-suppress MixedArgument `Container::get()` always return array for tag */ |
||
| 65 | array_unshift($tags, $container->get($id)); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | /** @psalm-suppress MixedArgument `Container::get()` always return array for tag */ |
||
| 70 | return array_merge(...$tags); |
||
| 71 | } |
||
| 72 | |||
| 73 | foreach ($this->containers as $container) { |
||
| 74 | if ($container->has($id)) { |
||
| 75 | /** @psalm-suppress MixedReturnStatement */ |
||
| 76 | return $container->get($id); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | // Collect details from containers |
||
| 81 | $exceptions = []; |
||
| 82 | foreach ($this->containers as $container) { |
||
| 83 | $hasException = false; |
||
| 84 | try { |
||
| 85 | $container->get($id); |
||
| 86 | } catch (Throwable $t) { |
||
| 87 | $hasException = true; |
||
| 88 | $exceptions[] = [$t, $container]; |
||
| 89 | } finally { |
||
| 90 | if (!$hasException) { |
||
| 91 | $exceptions[] = [ |
||
| 92 | new RuntimeException( |
||
| 93 | 'Container "has()" returned false, but no exception was thrown from "get()".' |
||
| 94 | ), |
||
| 95 | $container, |
||
| 96 | ]; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | throw new CompositeNotFoundException($exceptions); |
||
| 102 | } |
||
| 134 |