Conditions | 21 |
Paths | 649 |
Total Lines | 86 |
Code Lines | 56 |
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 |
||
95 | private function executeCallback(callable $callback, ContainerConfigurator $containerConfigurator, string $path): void |
||
96 | { |
||
97 | $callback = $callback(...); |
||
98 | $arguments = []; |
||
99 | $configBuilders = []; |
||
100 | $r = new \ReflectionFunction($callback); |
||
101 | |||
102 | $excluded = true; |
||
103 | $whenAttributes = $r->getAttributes(When::class, \ReflectionAttribute::IS_INSTANCEOF); |
||
104 | $notWhenAttributes = $r->getAttributes(WhenNot::class, \ReflectionAttribute::IS_INSTANCEOF); |
||
105 | |||
106 | if ($whenAttributes && $notWhenAttributes) { |
||
107 | throw new LogicException('Using both #[When] and #[WhenNot] attributes on the same target is not allowed.'); |
||
108 | } |
||
109 | |||
110 | if (!$whenAttributes && !$notWhenAttributes) { |
||
111 | $excluded = false; |
||
112 | } |
||
113 | |||
114 | foreach ($whenAttributes as $attribute) { |
||
115 | if ($this->env === $attribute->newInstance()->env) { |
||
116 | $excluded = false; |
||
117 | break; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | foreach ($notWhenAttributes as $attribute) { |
||
122 | if ($excluded = $this->env === $attribute->newInstance()->env) { |
||
123 | break; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | if ($excluded) { |
||
128 | return; |
||
129 | } |
||
130 | |||
131 | foreach ($r->getParameters() as $parameter) { |
||
132 | $reflectionType = $parameter->getType(); |
||
133 | if (!$reflectionType instanceof \ReflectionNamedType) { |
||
134 | throw new \InvalidArgumentException(\sprintf('Could not resolve argument "$%s" for "%s". You must typehint it (for example with "%s" or "%s").', $parameter->getName(), $path, ContainerConfigurator::class, ContainerBuilder::class)); |
||
135 | } |
||
136 | $type = $reflectionType->getName(); |
||
137 | |||
138 | switch ($type) { |
||
139 | case ContainerConfigurator::class: |
||
140 | $arguments[] = $containerConfigurator; |
||
141 | break; |
||
142 | case ContainerBuilder::class: |
||
143 | $arguments[] = $this->container; |
||
144 | break; |
||
145 | case FileLoader::class: |
||
146 | case self::class: |
||
147 | $arguments[] = $this; |
||
148 | break; |
||
149 | case 'string': |
||
150 | if (null !== $this->env && 'env' === $parameter->getName()) { |
||
151 | $arguments[] = $this->env; |
||
152 | break; |
||
153 | } |
||
154 | // no break |
||
155 | default: |
||
156 | try { |
||
157 | $configBuilder = $this->configBuilder($type); |
||
158 | } catch (InvalidArgumentException|\LogicException $e) { |
||
159 | throw new \InvalidArgumentException(\sprintf('Could not resolve argument "%s" for "%s".', $type.' $'.$parameter->getName(), $path), 0, $e); |
||
160 | } |
||
161 | $configBuilders[] = $configBuilder; |
||
162 | $arguments[] = $configBuilder; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | // Force load ContainerConfigurator to make env(), param() etc available. |
||
167 | class_exists(ContainerConfigurator::class); |
||
168 | |||
169 | ++$this->importing; |
||
170 | try { |
||
171 | $callback(...$arguments); |
||
172 | } finally { |
||
173 | --$this->importing; |
||
174 | } |
||
175 | |||
176 | foreach ($configBuilders as $configBuilder) { |
||
177 | $this->loadExtensionConfig($configBuilder->getExtensionAlias(), ContainerConfigurator::processValue($configBuilder->toArray())); |
||
178 | } |
||
179 | |||
180 | $this->loadExtensionConfigs(); |
||
181 | } |
||
236 |