| Conditions | 38 |
| Paths | 7112 |
| Total Lines | 125 |
| Code Lines | 74 |
| 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 |
||
| 110 | public function registerClasses(Definition $prototype, string $namespace, string $resource, string|array|null $exclude = null, ?string $source = null): void |
||
| 111 | { |
||
| 112 | if (!str_ends_with($namespace, '\\')) { |
||
| 113 | throw new InvalidArgumentException(\sprintf('Namespace prefix must end with a "\\": "%s".', $namespace)); |
||
| 114 | } |
||
| 115 | if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) { |
||
| 116 | throw new InvalidArgumentException(\sprintf('Namespace is not a valid PSR-4 prefix: "%s".', $namespace)); |
||
| 117 | } |
||
| 118 | // This can happen with YAML files |
||
| 119 | if (\is_array($exclude) && \in_array(null, $exclude, true)) { |
||
| 120 | throw new InvalidArgumentException('The exclude list must not contain a "null" value.'); |
||
| 121 | } |
||
| 122 | // This can happen with XML files |
||
| 123 | if (\is_array($exclude) && \in_array('', $exclude, true)) { |
||
| 124 | throw new InvalidArgumentException('The exclude list must not contain an empty value.'); |
||
| 125 | } |
||
| 126 | |||
| 127 | $autoconfigureAttributes = new RegisterAutoconfigureAttributesPass(); |
||
| 128 | $autoconfigureAttributes = $autoconfigureAttributes->accept($prototype) ? $autoconfigureAttributes : null; |
||
| 129 | $classes = $this->findClasses($namespace, $resource, (array) $exclude, $autoconfigureAttributes, $source); |
||
| 130 | |||
| 131 | $getPrototype = static fn () => clone $prototype; |
||
| 132 | $serialized = serialize($prototype); |
||
| 133 | |||
| 134 | // avoid deep cloning if no definitions are nested |
||
| 135 | if (strpos($serialized, 'O:48:"Symfony\Component\DependencyInjection\Definition"', 55) |
||
| 136 | || strpos($serialized, 'O:53:"Symfony\Component\DependencyInjection\ChildDefinition"', 55) |
||
| 137 | ) { |
||
| 138 | // prepare for deep cloning |
||
| 139 | foreach (['Arguments', 'Properties', 'MethodCalls', 'Configurator', 'Factory', 'Bindings'] as $key) { |
||
| 140 | $serialized = serialize($prototype->{'get'.$key}()); |
||
| 141 | |||
| 142 | if (strpos($serialized, 'O:48:"Symfony\Component\DependencyInjection\Definition"') |
||
| 143 | || strpos($serialized, 'O:53:"Symfony\Component\DependencyInjection\ChildDefinition"') |
||
| 144 | ) { |
||
| 145 | $getPrototype = static fn () => $getPrototype()->{'set'.$key}(unserialize($serialized)); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | unset($serialized); |
||
| 150 | |||
| 151 | foreach ($classes as $class => $errorMessage) { |
||
| 152 | if (null === $errorMessage && $autoconfigureAttributes) { |
||
| 153 | $r = $this->container->getReflectionClass($class); |
||
| 154 | if ($r->getAttributes(Exclude::class)[0] ?? null) { |
||
| 155 | $this->addContainerExcludedTag($class, $source); |
||
| 156 | continue; |
||
| 157 | } |
||
| 158 | if ($this->env) { |
||
| 159 | $excluded = true; |
||
| 160 | $whenAttributes = $r->getAttributes(When::class, \ReflectionAttribute::IS_INSTANCEOF); |
||
| 161 | $notWhenAttributes = $r->getAttributes(WhenNot::class, \ReflectionAttribute::IS_INSTANCEOF); |
||
| 162 | |||
| 163 | if ($whenAttributes && $notWhenAttributes) { |
||
| 164 | throw new LogicException(\sprintf('The "%s" class cannot have both #[When] and #[WhenNot] attributes.', $class)); |
||
| 165 | } |
||
| 166 | |||
| 167 | if (!$whenAttributes && !$notWhenAttributes) { |
||
| 168 | $excluded = false; |
||
| 169 | } |
||
| 170 | |||
| 171 | foreach ($whenAttributes as $attribute) { |
||
| 172 | if ($this->env === $attribute->newInstance()->env) { |
||
| 173 | $excluded = false; |
||
| 174 | break; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | foreach ($notWhenAttributes as $attribute) { |
||
| 179 | if ($excluded = $this->env === $attribute->newInstance()->env) { |
||
| 180 | break; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | if ($excluded) { |
||
| 185 | $this->addContainerExcludedTag($class, $source); |
||
| 186 | continue; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | if (interface_exists($class, false)) { |
||
| 192 | $this->interfaces[] = $class; |
||
| 193 | } else { |
||
| 194 | $this->setDefinition($class, $definition = $getPrototype()); |
||
| 195 | if (null !== $errorMessage) { |
||
| 196 | $definition->addError($errorMessage); |
||
| 197 | |||
| 198 | continue; |
||
| 199 | } |
||
| 200 | $definition->setClass($class); |
||
| 201 | |||
| 202 | $interfaces = []; |
||
| 203 | foreach (class_implements($class, false) as $interface) { |
||
| 204 | $this->singlyImplemented[$interface] = ($this->singlyImplemented[$interface] ?? $class) !== $class ? false : $class; |
||
| 205 | $interfaces[] = $interface; |
||
| 206 | } |
||
| 207 | |||
| 208 | if (!$autoconfigureAttributes) { |
||
| 209 | continue; |
||
| 210 | } |
||
| 211 | $r = $this->container->getReflectionClass($class); |
||
| 212 | $defaultAlias = 1 === \count($interfaces) ? $interfaces[0] : null; |
||
| 213 | foreach ($r->getAttributes(AsAlias::class) as $attr) { |
||
| 214 | /** @var AsAlias $attribute */ |
||
| 215 | $attribute = $attr->newInstance(); |
||
| 216 | $alias = $attribute->id ?? $defaultAlias; |
||
| 217 | $public = $attribute->public; |
||
| 218 | if (null === $alias) { |
||
| 219 | throw new LogicException(\sprintf('Alias cannot be automatically determined for class "%s". If you have used the #[AsAlias] attribute with a class implementing multiple interfaces, add the interface you want to alias to the first parameter of #[AsAlias].', $class)); |
||
| 220 | } |
||
| 221 | if (isset($this->aliases[$alias])) { |
||
| 222 | throw new LogicException(\sprintf('The "%s" alias has already been defined with the #[AsAlias] attribute in "%s".', $alias, $this->aliases[$alias])); |
||
| 223 | } |
||
| 224 | $this->aliases[$alias] = new Alias($class, $public); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | foreach ($this->aliases as $alias => $aliasDefinition) { |
||
| 230 | $this->container->setAlias($alias, $aliasDefinition); |
||
| 231 | } |
||
| 232 | |||
| 233 | if ($this->autoRegisterAliasesForSinglyImplementedInterfaces) { |
||
| 234 | $this->registerAliasesForSinglyImplementedInterfaces(); |
||
| 235 | } |
||
| 404 |