@@ -27,29 +27,29 @@ discard block |
||
| 27 | 27 | MagicCallTrait::class, |
| 28 | 28 | ] : []; |
| 29 | 29 | |
| 30 | - if (\str_contains($className, '\\')) { |
|
| 30 | + if (\str_contains($className, '\\')){ |
|
| 31 | 31 | $classShortName = \substr($className, \strrpos($className, '\\') + 1); |
| 32 | - $classNamespaceStr = 'namespace ' . \substr($className, 0, \strrpos($className, '\\')) . ';'; |
|
| 33 | - } else { |
|
| 32 | + $classNamespaceStr = 'namespace '.\substr($className, 0, \strrpos($className, '\\')).';'; |
|
| 33 | + }else{ |
|
| 34 | 34 | $classShortName = $className; |
| 35 | 35 | $classNamespaceStr = ''; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | $interface = $type->getName(); |
| 39 | 39 | $classBody = []; |
| 40 | - foreach ($type->getMethods() as $method) { |
|
| 41 | - if ($method->isConstructor()) { |
|
| 40 | + foreach ($type->getMethods() as $method){ |
|
| 41 | + if ($method->isConstructor()){ |
|
| 42 | 42 | throw new \LogicException('Constructor is not allowed in a proxy.'); |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - if ($method->isDestructor()) { |
|
| 45 | + if ($method->isDestructor()){ |
|
| 46 | 46 | $classBody[] = self::renderMethod($method); |
| 47 | 47 | continue; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | $hasRefs = false; |
| 51 | - $return = $method->hasReturnType() && (string) $method->getReturnType() === 'void' ? '' : 'return '; |
|
| 52 | - $call = ($method->isStatic() ? '::' : '->') . $method->getName(); |
|
| 51 | + $return = $method->hasReturnType() && (string)$method->getReturnType() === 'void' ? '' : 'return '; |
|
| 52 | + $call = ($method->isStatic() ? '::' : '->').$method->getName(); |
|
| 53 | 53 | $context = $method->isStatic() ? 'null' : '$this->__container_proxy_context'; |
| 54 | 54 | $containerStr = match (false) { |
| 55 | 55 | $attachContainer => 'null', |
@@ -69,12 +69,12 @@ discard block |
||
| 69 | 69 | PHP; |
| 70 | 70 | |
| 71 | 71 | $args = []; |
| 72 | - foreach ($method->getParameters() as $param) { |
|
| 72 | + foreach ($method->getParameters() as $param){ |
|
| 73 | 73 | $hasRefs = $hasRefs || $param->isPassedByReference(); |
| 74 | - $args[] = ($param->isVariadic() ? '...' : '') . '$' . $param->getName(); |
|
| 74 | + $args[] = ($param->isVariadic() ? '...' : '').'$'.$param->getName(); |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | - if (!$hasRefs && !$method->isVariadic()) { |
|
| 77 | + if (!$hasRefs && !$method->isVariadic()){ |
|
| 78 | 78 | $classBody[] = self::renderMethod( |
| 79 | 79 | $method, |
| 80 | 80 | <<<PHP |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | |
| 87 | 87 | $argsStr = \implode(', ', $args); |
| 88 | 88 | |
| 89 | - if ($method->isVariadic()) { |
|
| 89 | + if ($method->isVariadic()){ |
|
| 90 | 90 | $classBody[] = self::renderMethod( |
| 91 | 91 | $method, |
| 92 | 92 | <<<PHP |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | |
| 109 | 109 | $traitsStr = $traits === [] ? '' : \implode( |
| 110 | 110 | "\n ", |
| 111 | - \array_map(static fn(string $trait): string => 'use \\' . \ltrim($trait, '\\') . ';', $traits), |
|
| 111 | + \array_map(static fn(string $trait) : string => 'use \\'.\ltrim($trait, '\\').';', $traits), |
|
| 112 | 112 | ); |
| 113 | 113 | return <<<PHP |
| 114 | 114 | $classNamespaceStr |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | $m->getName(), |
| 132 | 132 | \implode(', ', \array_map(self::renderParameter(...), $m->getParameters())), |
| 133 | 133 | $m->hasReturnType() |
| 134 | - ? ': ' . self::renderParameterTypes($m->getReturnType(), $m->getDeclaringClass()) |
|
| 134 | + ? ': '.self::renderParameterTypes($m->getReturnType(), $m->getDeclaringClass()) |
|
| 135 | 135 | : '', |
| 136 | 136 | $body, |
| 137 | 137 | ); |
@@ -145,8 +145,8 @@ discard block |
||
| 145 | 145 | $param->hasType() ? 'mixed' : '', |
| 146 | 146 | $param->isPassedByReference() ? '&' : '', |
| 147 | 147 | $param->isVariadic() ? '...' : '', |
| 148 | - '$' . $param->getName(), |
|
| 149 | - $param->isOptional() && !$param->isVariadic() ? ' = ' . self::renderDefaultValue($param) : '', |
|
| 148 | + '$'.$param->getName(), |
|
| 149 | + $param->isOptional() && !$param->isVariadic() ? ' = '.self::renderDefaultValue($param) : '', |
|
| 150 | 150 | ), |
| 151 | 151 | ' ', |
| 152 | 152 | ); |
@@ -154,8 +154,8 @@ discard block |
||
| 154 | 154 | |
| 155 | 155 | public static function renderParameterTypes(\ReflectionType $types, \ReflectionClass $class): string |
| 156 | 156 | { |
| 157 | - if ($types instanceof \ReflectionNamedType) { |
|
| 158 | - return ($types->allowsNull() && $types->getName() !== 'mixed' ? '?' : '') . ($types->isBuiltin() |
|
| 157 | + if ($types instanceof \ReflectionNamedType){ |
|
| 158 | + return ($types->allowsNull() && $types->getName() !== 'mixed' ? '?' : '').($types->isBuiltin() |
|
| 159 | 159 | ? $types->getName() |
| 160 | 160 | : self::normalizeClassType($types, $class)); |
| 161 | 161 | } |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | }; |
| 168 | 168 | |
| 169 | 169 | $result = []; |
| 170 | - foreach ($types as $type) { |
|
| 170 | + foreach ($types as $type){ |
|
| 171 | 171 | $result[] = $type->isBuiltin() |
| 172 | 172 | ? $type->getName() |
| 173 | 173 | : self::normalizeClassType($type, $class); |
@@ -178,12 +178,12 @@ discard block |
||
| 178 | 178 | |
| 179 | 179 | public static function renderDefaultValue(\ReflectionParameter $param): string |
| 180 | 180 | { |
| 181 | - if ($param->isDefaultValueConstant()) { |
|
| 181 | + if ($param->isDefaultValueConstant()){ |
|
| 182 | 182 | $result = $param->getDefaultValueConstantName(); |
| 183 | 183 | |
| 184 | - return \explode('::', (string) $result)[0] === 'self' |
|
| 184 | + return \explode('::', (string)$result)[0] === 'self' |
|
| 185 | 185 | ? $result |
| 186 | - : '\\' . $result; |
|
| 186 | + : '\\'.$result; |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | $cut = self::cutDefaultValue($param); |
@@ -197,14 +197,14 @@ discard block |
||
| 197 | 197 | { |
| 198 | 198 | return match($type->getName()) { |
| 199 | 199 | 'static' => 'static', |
| 200 | - 'self' => '\\' . $class->getName(), |
|
| 201 | - default => '\\' . $type->getName(), |
|
| 200 | + 'self' => '\\'.$class->getName(), |
|
| 201 | + default => '\\'.$type->getName(), |
|
| 202 | 202 | }; |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | private static function cutDefaultValue(\ReflectionParameter $param): string |
| 206 | 206 | { |
| 207 | - $string = (string) $param; |
|
| 207 | + $string = (string)$param; |
|
| 208 | 208 | |
| 209 | 209 | return \trim(\substr($string, \strpos($string, '=') + 1, -1)); |
| 210 | 210 | } |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | ): array { |
| 49 | 49 | $state = new ResolvingState($reflection, $parameters); |
| 50 | 50 | |
| 51 | - foreach ($reflection->getParameters() as $parameter) { |
|
| 51 | + foreach ($reflection->getParameters() as $parameter){ |
|
| 52 | 52 | $this->resolveParameter($parameter, $state, $validate) |
| 53 | 53 | or |
| 54 | 54 | throw ArgumentResolvingException::createWithParam($reflection, $parameter->getName()); |
@@ -62,47 +62,47 @@ discard block |
||
| 62 | 62 | $positional = true; |
| 63 | 63 | $variadic = false; |
| 64 | 64 | $parameters = $reflection->getParameters(); |
| 65 | - if (\count($parameters) === 0) { |
|
| 65 | + if (\count($parameters) === 0){ |
|
| 66 | 66 | return; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | $parameter = null; |
| 70 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
| 70 | + while (\count($parameters) > 0 || \count($arguments) > 0){ |
|
| 71 | 71 | // get related argument value |
| 72 | 72 | $key = \key($arguments); |
| 73 | 73 | |
| 74 | 74 | // For a variadic parameter it's no sense - named or positional argument will be sent |
| 75 | 75 | // But you can't send positional argument after named in any case |
| 76 | - if (\is_int($key) && !$positional) { |
|
| 77 | - throw PositionalArgumentException::createWithParam($reflection, (string) $key); |
|
| 76 | + if (\is_int($key) && !$positional){ |
|
| 77 | + throw PositionalArgumentException::createWithParam($reflection, (string)$key); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | $positional = $positional && \is_int($key); |
| 81 | 81 | |
| 82 | - if (!$variadic) { |
|
| 82 | + if (!$variadic){ |
|
| 83 | 83 | $parameter = \array_shift($parameters); |
| 84 | 84 | $variadic = $parameter?->isVariadic() ?? false; |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - if ($parameter === null) { |
|
| 88 | - throw UnknownParameterException::createWithParam($reflection, (string) $key); |
|
| 87 | + if ($parameter === null){ |
|
| 88 | + throw UnknownParameterException::createWithParam($reflection, (string)$key); |
|
| 89 | 89 | } |
| 90 | 90 | $name = $parameter->getName(); |
| 91 | 91 | |
| 92 | - if (($positional || $variadic) && $key !== null) { |
|
| 92 | + if (($positional || $variadic) && $key !== null){ |
|
| 93 | 93 | /** @psalm-suppress ReferenceReusedFromConfusingScope */ |
| 94 | 94 | $value = \array_shift($arguments); |
| 95 | - } elseif ($key === null || !\array_key_exists($name, $arguments)) { |
|
| 96 | - if ($parameter->isOptional()) { |
|
| 95 | + } elseif ($key === null || !\array_key_exists($name, $arguments)){ |
|
| 96 | + if ($parameter->isOptional()){ |
|
| 97 | 97 | continue; |
| 98 | 98 | } |
| 99 | 99 | throw MissingRequiredArgumentException::createWithParam($reflection, $name); |
| 100 | - } else { |
|
| 100 | + }else{ |
|
| 101 | 101 | $value = &$arguments[$name]; |
| 102 | 102 | unset($arguments[$name]); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - if (!$this->validateValueToParameter($parameter, $value)) { |
|
| 105 | + if (!$this->validateValueToParameter($parameter, $value)){ |
|
| 106 | 106 | throw InvalidArgumentException::createWithParam($reflection, $name); |
| 107 | 107 | } |
| 108 | 108 | } |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | |
| 111 | 111 | private function validateValueToParameter(\ReflectionParameter $parameter, mixed $value): bool |
| 112 | 112 | { |
| 113 | - if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) { |
|
| 113 | + if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)){ |
|
| 114 | 114 | return true; |
| 115 | 115 | } |
| 116 | 116 | $type = $parameter->getType(); |
@@ -121,17 +121,17 @@ discard block |
||
| 121 | 121 | $type instanceof \ReflectionIntersectionType => [false, $type->getTypes()], |
| 122 | 122 | }; |
| 123 | 123 | |
| 124 | - foreach ($types as $t) { |
|
| 124 | + foreach ($types as $t){ |
|
| 125 | 125 | \assert($t instanceof \ReflectionNamedType); |
| 126 | - if (!$this->validateValueNamedType($t, $value)) { |
|
| 126 | + if (!$this->validateValueNamedType($t, $value)){ |
|
| 127 | 127 | // If it is TypeIntersection |
| 128 | - if ($or) { |
|
| 128 | + if ($or){ |
|
| 129 | 129 | continue; |
| 130 | 130 | } |
| 131 | 131 | return false; |
| 132 | 132 | } |
| 133 | 133 | // If it is not type intersection then we can skip that value after first successful check |
| 134 | - if ($or) { |
|
| 134 | + if ($or){ |
|
| 135 | 135 | return true; |
| 136 | 136 | } |
| 137 | 137 | } |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | { |
| 147 | 147 | $name = $type->getName(); |
| 148 | 148 | |
| 149 | - if ($type->isBuiltin()) { |
|
| 149 | + if ($type->isBuiltin()){ |
|
| 150 | 150 | return match ($name) { |
| 151 | 151 | 'mixed' => true, |
| 152 | 152 | 'string' => \is_string($value), |
@@ -179,13 +179,13 @@ discard block |
||
| 179 | 179 | |
| 180 | 180 | // Try to resolve parameter by name |
| 181 | 181 | $res = $state->resolveParameterByNameOrPosition($param, $isVariadic); |
| 182 | - if ($res !== [] || $isVariadic) { |
|
| 182 | + if ($res !== [] || $isVariadic){ |
|
| 183 | 183 | // validate |
| 184 | - if ($isVariadic) { |
|
| 185 | - foreach ($res as $k => &$v) { |
|
| 186 | - $this->processArgument($state, $v, validateWith: $validate ? $param : null, key: $k); |
|
| 184 | + if ($isVariadic){ |
|
| 185 | + foreach ($res as $k => &$v){ |
|
| 186 | + $this->processArgument($state, $v, validateWith: $validate ? $param : null, key : $k); |
|
| 187 | 187 | } |
| 188 | - } else { |
|
| 188 | + }else{ |
|
| 189 | 189 | $this->processArgument($state, $res[0], validateWith: $validate ? $param : null); |
| 190 | 190 | } |
| 191 | 191 | |
@@ -193,18 +193,18 @@ discard block |
||
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | $error = null; |
| 196 | - while ($hasType) { |
|
| 196 | + while ($hasType){ |
|
| 197 | 197 | /** @var \ReflectionIntersectionType|\ReflectionUnionType|\ReflectionNamedType $refType */ |
| 198 | 198 | $refType = $param->getType(); |
| 199 | 199 | |
| 200 | - if ($refType::class === \ReflectionNamedType::class) { |
|
| 201 | - if ($refType->isBuiltin()) { |
|
| 200 | + if ($refType::class === \ReflectionNamedType::class){ |
|
| 201 | + if ($refType->isBuiltin()){ |
|
| 202 | 202 | break; |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | if (\interface_exists($refType->getName()) && !empty( |
| 206 | 206 | $attrs = $param->getAttributes(ProxyAttribute::class) |
| 207 | - )) { |
|
| 207 | + )){ |
|
| 208 | 208 | $proxy = Proxy::create( |
| 209 | 209 | new \ReflectionClass($refType->getName()), |
| 210 | 210 | $param, |
@@ -214,23 +214,23 @@ discard block |
||
| 214 | 214 | return true; |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | - try { |
|
| 218 | - if ($this->resolveObject($state, $refType, $param, $validate)) { |
|
| 217 | + try{ |
|
| 218 | + if ($this->resolveObject($state, $refType, $param, $validate)){ |
|
| 219 | 219 | return true; |
| 220 | 220 | } |
| 221 | - } catch (\Throwable $e) { |
|
| 221 | + }catch (\Throwable $e){ |
|
| 222 | 222 | $error = $e; |
| 223 | 223 | } |
| 224 | 224 | break; |
| 225 | 225 | } |
| 226 | 226 | |
| 227 | - if ($refType::class === \ReflectionUnionType::class) { |
|
| 228 | - foreach ($refType->getTypes() as $namedType) { |
|
| 229 | - try { |
|
| 230 | - if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)) { |
|
| 227 | + if ($refType::class === \ReflectionUnionType::class){ |
|
| 228 | + foreach ($refType->getTypes() as $namedType){ |
|
| 229 | + try{ |
|
| 230 | + if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)){ |
|
| 231 | 231 | return true; |
| 232 | 232 | } |
| 233 | - } catch (\Throwable $e) { |
|
| 233 | + }catch (\Throwable $e){ |
|
| 234 | 234 | $error = $e; |
| 235 | 235 | } |
| 236 | 236 | } |
@@ -240,19 +240,19 @@ discard block |
||
| 240 | 240 | throw new UnsupportedTypeException($param->getDeclaringFunction(), $param->getName()); |
| 241 | 241 | } |
| 242 | 242 | |
| 243 | - if ($param->isDefaultValueAvailable()) { |
|
| 243 | + if ($param->isDefaultValueAvailable()){ |
|
| 244 | 244 | $argument = $param->getDefaultValue(); |
| 245 | 245 | $this->processArgument($state, $argument); |
| 246 | 246 | return true; |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | - if ($hasType && $param->allowsNull()) { |
|
| 249 | + if ($hasType && $param->allowsNull()){ |
|
| 250 | 250 | $argument = null; |
| 251 | 251 | $this->processArgument($state, $argument); |
| 252 | 252 | return true; |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | - if ($error === null) { |
|
| 255 | + if ($error === null){ |
|
| 256 | 256 | return false; |
| 257 | 257 | } |
| 258 | 258 | |
@@ -290,15 +290,15 @@ discard block |
||
| 290 | 290 | ResolvingState $state, |
| 291 | 291 | mixed &$value, |
| 292 | 292 | ?\ReflectionParameter $validateWith = null, |
| 293 | - int|string|null $key = null, |
|
| 293 | + int | string | null $key = null, |
|
| 294 | 294 | ): void { |
| 295 | 295 | // Resolve Autowire objects |
| 296 | - if ($value instanceof Autowire) { |
|
| 296 | + if ($value instanceof Autowire){ |
|
| 297 | 297 | $value = $value->resolve($this->factory); |
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | // Validation |
| 301 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
| 301 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)){ |
|
| 302 | 302 | throw InvalidArgumentException::createWithParam( |
| 303 | 303 | $validateWith->getDeclaringFunction(), |
| 304 | 304 | $validateWith->getName(), |
@@ -48,7 +48,8 @@ discard block |
||
| 48 | 48 | ): array { |
| 49 | 49 | $state = new ResolvingState($reflection, $parameters); |
| 50 | 50 | |
| 51 | - foreach ($reflection->getParameters() as $parameter) { |
|
| 51 | + foreach ($reflection->getParameters() as $parameter) |
|
| 52 | + { |
|
| 52 | 53 | $this->resolveParameter($parameter, $state, $validate) |
| 53 | 54 | or |
| 54 | 55 | throw ArgumentResolvingException::createWithParam($reflection, $parameter->getName()); |
@@ -62,47 +63,59 @@ discard block |
||
| 62 | 63 | $positional = true; |
| 63 | 64 | $variadic = false; |
| 64 | 65 | $parameters = $reflection->getParameters(); |
| 65 | - if (\count($parameters) === 0) { |
|
| 66 | + if (\count($parameters) === 0) |
|
| 67 | + { |
|
| 66 | 68 | return; |
| 67 | 69 | } |
| 68 | 70 | |
| 69 | 71 | $parameter = null; |
| 70 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
| 72 | + while (\count($parameters) > 0 || \count($arguments) > 0) |
|
| 73 | + { |
|
| 71 | 74 | // get related argument value |
| 72 | 75 | $key = \key($arguments); |
| 73 | 76 | |
| 74 | 77 | // For a variadic parameter it's no sense - named or positional argument will be sent |
| 75 | 78 | // But you can't send positional argument after named in any case |
| 76 | - if (\is_int($key) && !$positional) { |
|
| 79 | + if (\is_int($key) && !$positional) |
|
| 80 | + { |
|
| 77 | 81 | throw PositionalArgumentException::createWithParam($reflection, (string) $key); |
| 78 | 82 | } |
| 79 | 83 | |
| 80 | 84 | $positional = $positional && \is_int($key); |
| 81 | 85 | |
| 82 | - if (!$variadic) { |
|
| 86 | + if (!$variadic) |
|
| 87 | + { |
|
| 83 | 88 | $parameter = \array_shift($parameters); |
| 84 | 89 | $variadic = $parameter?->isVariadic() ?? false; |
| 85 | 90 | } |
| 86 | 91 | |
| 87 | - if ($parameter === null) { |
|
| 92 | + if ($parameter === null) |
|
| 93 | + { |
|
| 88 | 94 | throw UnknownParameterException::createWithParam($reflection, (string) $key); |
| 89 | 95 | } |
| 90 | 96 | $name = $parameter->getName(); |
| 91 | 97 | |
| 92 | - if (($positional || $variadic) && $key !== null) { |
|
| 98 | + if (($positional || $variadic) && $key !== null) |
|
| 99 | + { |
|
| 93 | 100 | /** @psalm-suppress ReferenceReusedFromConfusingScope */ |
| 94 | 101 | $value = \array_shift($arguments); |
| 95 | - } elseif ($key === null || !\array_key_exists($name, $arguments)) { |
|
| 96 | - if ($parameter->isOptional()) { |
|
| 102 | + } |
|
| 103 | + elseif ($key === null || !\array_key_exists($name, $arguments)) |
|
| 104 | + { |
|
| 105 | + if ($parameter->isOptional()) |
|
| 106 | + { |
|
| 97 | 107 | continue; |
| 98 | 108 | } |
| 99 | 109 | throw MissingRequiredArgumentException::createWithParam($reflection, $name); |
| 100 | - } else { |
|
| 110 | + } |
|
| 111 | + else |
|
| 112 | + { |
|
| 101 | 113 | $value = &$arguments[$name]; |
| 102 | 114 | unset($arguments[$name]); |
| 103 | 115 | } |
| 104 | 116 | |
| 105 | - if (!$this->validateValueToParameter($parameter, $value)) { |
|
| 117 | + if (!$this->validateValueToParameter($parameter, $value)) |
|
| 118 | + { |
|
| 106 | 119 | throw InvalidArgumentException::createWithParam($reflection, $name); |
| 107 | 120 | } |
| 108 | 121 | } |
@@ -110,7 +123,8 @@ discard block |
||
| 110 | 123 | |
| 111 | 124 | private function validateValueToParameter(\ReflectionParameter $parameter, mixed $value): bool |
| 112 | 125 | { |
| 113 | - if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) { |
|
| 126 | + if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) |
|
| 127 | + { |
|
| 114 | 128 | return true; |
| 115 | 129 | } |
| 116 | 130 | $type = $parameter->getType(); |
@@ -121,17 +135,21 @@ discard block |
||
| 121 | 135 | $type instanceof \ReflectionIntersectionType => [false, $type->getTypes()], |
| 122 | 136 | }; |
| 123 | 137 | |
| 124 | - foreach ($types as $t) { |
|
| 138 | + foreach ($types as $t) |
|
| 139 | + { |
|
| 125 | 140 | \assert($t instanceof \ReflectionNamedType); |
| 126 | - if (!$this->validateValueNamedType($t, $value)) { |
|
| 141 | + if (!$this->validateValueNamedType($t, $value)) |
|
| 142 | + { |
|
| 127 | 143 | // If it is TypeIntersection |
| 128 | - if ($or) { |
|
| 144 | + if ($or) |
|
| 145 | + { |
|
| 129 | 146 | continue; |
| 130 | 147 | } |
| 131 | 148 | return false; |
| 132 | 149 | } |
| 133 | 150 | // If it is not type intersection then we can skip that value after first successful check |
| 134 | - if ($or) { |
|
| 151 | + if ($or) |
|
| 152 | + { |
|
| 135 | 153 | return true; |
| 136 | 154 | } |
| 137 | 155 | } |
@@ -146,7 +164,8 @@ discard block |
||
| 146 | 164 | { |
| 147 | 165 | $name = $type->getName(); |
| 148 | 166 | |
| 149 | - if ($type->isBuiltin()) { |
|
| 167 | + if ($type->isBuiltin()) |
|
| 168 | + { |
|
| 150 | 169 | return match ($name) { |
| 151 | 170 | 'mixed' => true, |
| 152 | 171 | 'string' => \is_string($value), |
@@ -179,13 +198,18 @@ discard block |
||
| 179 | 198 | |
| 180 | 199 | // Try to resolve parameter by name |
| 181 | 200 | $res = $state->resolveParameterByNameOrPosition($param, $isVariadic); |
| 182 | - if ($res !== [] || $isVariadic) { |
|
| 201 | + if ($res !== [] || $isVariadic) |
|
| 202 | + { |
|
| 183 | 203 | // validate |
| 184 | - if ($isVariadic) { |
|
| 185 | - foreach ($res as $k => &$v) { |
|
| 204 | + if ($isVariadic) |
|
| 205 | + { |
|
| 206 | + foreach ($res as $k => &$v) |
|
| 207 | + { |
|
| 186 | 208 | $this->processArgument($state, $v, validateWith: $validate ? $param : null, key: $k); |
| 187 | 209 | } |
| 188 | - } else { |
|
| 210 | + } |
|
| 211 | + else |
|
| 212 | + { |
|
| 189 | 213 | $this->processArgument($state, $res[0], validateWith: $validate ? $param : null); |
| 190 | 214 | } |
| 191 | 215 | |
@@ -193,18 +217,22 @@ discard block |
||
| 193 | 217 | } |
| 194 | 218 | |
| 195 | 219 | $error = null; |
| 196 | - while ($hasType) { |
|
| 220 | + while ($hasType) |
|
| 221 | + { |
|
| 197 | 222 | /** @var \ReflectionIntersectionType|\ReflectionUnionType|\ReflectionNamedType $refType */ |
| 198 | 223 | $refType = $param->getType(); |
| 199 | 224 | |
| 200 | - if ($refType::class === \ReflectionNamedType::class) { |
|
| 201 | - if ($refType->isBuiltin()) { |
|
| 225 | + if ($refType::class === \ReflectionNamedType::class) |
|
| 226 | + { |
|
| 227 | + if ($refType->isBuiltin()) |
|
| 228 | + { |
|
| 202 | 229 | break; |
| 203 | 230 | } |
| 204 | 231 | |
| 205 | 232 | if (\interface_exists($refType->getName()) && !empty( |
| 206 | 233 | $attrs = $param->getAttributes(ProxyAttribute::class) |
| 207 | - )) { |
|
| 234 | + )) |
|
| 235 | + { |
|
| 208 | 236 | $proxy = Proxy::create( |
| 209 | 237 | new \ReflectionClass($refType->getName()), |
| 210 | 238 | $param, |
@@ -214,23 +242,33 @@ discard block |
||
| 214 | 242 | return true; |
| 215 | 243 | } |
| 216 | 244 | |
| 217 | - try { |
|
| 218 | - if ($this->resolveObject($state, $refType, $param, $validate)) { |
|
| 245 | + try |
|
| 246 | + { |
|
| 247 | + if ($this->resolveObject($state, $refType, $param, $validate)) |
|
| 248 | + { |
|
| 219 | 249 | return true; |
| 220 | 250 | } |
| 221 | - } catch (\Throwable $e) { |
|
| 251 | + } |
|
| 252 | + catch (\Throwable $e) |
|
| 253 | + { |
|
| 222 | 254 | $error = $e; |
| 223 | 255 | } |
| 224 | 256 | break; |
| 225 | 257 | } |
| 226 | 258 | |
| 227 | - if ($refType::class === \ReflectionUnionType::class) { |
|
| 228 | - foreach ($refType->getTypes() as $namedType) { |
|
| 229 | - try { |
|
| 230 | - if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)) { |
|
| 259 | + if ($refType::class === \ReflectionUnionType::class) |
|
| 260 | + { |
|
| 261 | + foreach ($refType->getTypes() as $namedType) |
|
| 262 | + { |
|
| 263 | + try |
|
| 264 | + { |
|
| 265 | + if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)) |
|
| 266 | + { |
|
| 231 | 267 | return true; |
| 232 | 268 | } |
| 233 | - } catch (\Throwable $e) { |
|
| 269 | + } |
|
| 270 | + catch (\Throwable $e) |
|
| 271 | + { |
|
| 234 | 272 | $error = $e; |
| 235 | 273 | } |
| 236 | 274 | } |
@@ -240,19 +278,22 @@ discard block |
||
| 240 | 278 | throw new UnsupportedTypeException($param->getDeclaringFunction(), $param->getName()); |
| 241 | 279 | } |
| 242 | 280 | |
| 243 | - if ($param->isDefaultValueAvailable()) { |
|
| 281 | + if ($param->isDefaultValueAvailable()) |
|
| 282 | + { |
|
| 244 | 283 | $argument = $param->getDefaultValue(); |
| 245 | 284 | $this->processArgument($state, $argument); |
| 246 | 285 | return true; |
| 247 | 286 | } |
| 248 | 287 | |
| 249 | - if ($hasType && $param->allowsNull()) { |
|
| 288 | + if ($hasType && $param->allowsNull()) |
|
| 289 | + { |
|
| 250 | 290 | $argument = null; |
| 251 | 291 | $this->processArgument($state, $argument); |
| 252 | 292 | return true; |
| 253 | 293 | } |
| 254 | 294 | |
| 255 | - if ($error === null) { |
|
| 295 | + if ($error === null) |
|
| 296 | + { |
|
| 256 | 297 | return false; |
| 257 | 298 | } |
| 258 | 299 | |
@@ -293,12 +334,14 @@ discard block |
||
| 293 | 334 | int|string|null $key = null, |
| 294 | 335 | ): void { |
| 295 | 336 | // Resolve Autowire objects |
| 296 | - if ($value instanceof Autowire) { |
|
| 337 | + if ($value instanceof Autowire) |
|
| 338 | + { |
|
| 297 | 339 | $value = $value->resolve($this->factory); |
| 298 | 340 | } |
| 299 | 341 | |
| 300 | 342 | // Validation |
| 301 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
| 343 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) |
|
| 344 | + { |
|
| 302 | 345 | throw InvalidArgumentException::createWithParam( |
| 303 | 346 | $validateWith->getDeclaringFunction(), |
| 304 | 347 | $validateWith->getName(), |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | */ |
| 88 | 88 | public function resolveType( |
| 89 | 89 | string $alias, |
| 90 | - ?Binding &$binding = null, |
|
| 90 | + ?Binding & $binding = null, |
|
| 91 | 91 | ?object &$singleton = null, |
| 92 | 92 | ?object &$injector = null, |
| 93 | 93 | ?self &$actor = null, |
@@ -96,20 +96,20 @@ discard block |
||
| 96 | 96 | // Aliases to prevent circular dependencies |
| 97 | 97 | $as = []; |
| 98 | 98 | $actor = $this; |
| 99 | - do { |
|
| 99 | + do{ |
|
| 100 | 100 | $bindings = &$actor->state->bindings; |
| 101 | 101 | $singletons = &$actor->state->singletons; |
| 102 | 102 | $injectors = &$actor->state->injectors; |
| 103 | 103 | $binding = $bindings[$alias] ?? null; |
| 104 | - if (\array_key_exists($alias, $singletons)) { |
|
| 104 | + if (\array_key_exists($alias, $singletons)){ |
|
| 105 | 105 | $singleton = $singletons[$alias]; |
| 106 | 106 | $injector = $injectors[$alias] ?? null; |
| 107 | 107 | return \is_object($singleton::class) ? $singleton::class : null; |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - if ($binding !== null) { |
|
| 111 | - if ($followAlias && $binding::class === Alias::class) { |
|
| 112 | - if ($binding->alias === $alias) { |
|
| 110 | + if ($binding !== null){ |
|
| 111 | + if ($followAlias && $binding::class === Alias::class){ |
|
| 112 | + if ($binding->alias === $alias){ |
|
| 113 | 113 | break; |
| 114 | 114 | } |
| 115 | 115 | |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | return $binding->getReturnClass(); |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | - if (\array_key_exists($alias, $injectors)) { |
|
| 127 | + if (\array_key_exists($alias, $injectors)){ |
|
| 128 | 128 | $injector = $injectors[$alias]; |
| 129 | 129 | $binding = $bindings[$alias] ?? null; |
| 130 | 130 | return $alias; |
@@ -132,12 +132,12 @@ discard block |
||
| 132 | 132 | |
| 133 | 133 | // Go to parent scope |
| 134 | 134 | $parent = $actor->scope->getParentActor(); |
| 135 | - if ($parent === null) { |
|
| 135 | + if ($parent === null){ |
|
| 136 | 136 | break; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | $actor = $parent; |
| 140 | - } while (true); |
|
| 140 | + }while (true); |
|
| 141 | 141 | |
| 142 | 142 | return \class_exists($alias) ? $alias : null; |
| 143 | 143 | } |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | public function resolveBinding( |
| 146 | 146 | object $binding, |
| 147 | 147 | string $alias, |
| 148 | - \Stringable|string|null $context, |
|
| 148 | + \Stringable | string | null $context, |
|
| 149 | 149 | array $arguments, |
| 150 | 150 | Tracer $tracer, |
| 151 | 151 | ): mixed { |
@@ -202,18 +202,18 @@ discard block |
||
| 202 | 202 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments, Tracer $tracer) |
| 203 | 203 | { |
| 204 | 204 | $context = $ctx->context; |
| 205 | - try { |
|
| 205 | + try{ |
|
| 206 | 206 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
| 207 | - } catch (\ReflectionException $e) { |
|
| 207 | + }catch (\ReflectionException $e){ |
|
| 208 | 208 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | $injector = $binding->injector; |
| 212 | 212 | |
| 213 | - try { |
|
| 213 | + try{ |
|
| 214 | 214 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
| 215 | 215 | |
| 216 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 216 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
| 217 | 217 | throw new InjectionException( |
| 218 | 218 | \sprintf( |
| 219 | 219 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | static $cache = []; |
| 228 | 228 | $extended = $cache[$injectorInstance::class] ??= ( |
| 229 | 229 | static fn(\ReflectionType $type): bool => |
| 230 | - $type::class === \ReflectionUnionType::class || (string) $type === 'mixed' |
|
| 230 | + $type::class === \ReflectionUnionType::class || (string)$type === 'mixed' |
|
| 231 | 231 | )( |
| 232 | 232 | ($refMethod = new \ReflectionMethod($injectorInstance, 'createInjection')) |
| 233 | 233 | ->getParameters()[1]->getType() |
@@ -237,10 +237,10 @@ discard block |
||
| 237 | 237 | $instance = $injectorInstance->createInjection($reflection, match (true) { |
| 238 | 238 | $asIs => $context, |
| 239 | 239 | $context instanceof \ReflectionParameter => $context->getName(), |
| 240 | - default => (string) $context, |
|
| 240 | + default => (string)$context, |
|
| 241 | 241 | }); |
| 242 | 242 | |
| 243 | - if (!$reflection->isInstance($instance)) { |
|
| 243 | + if (!$reflection->isInstance($instance)){ |
|
| 244 | 244 | throw new InjectionException( |
| 245 | 245 | \sprintf( |
| 246 | 246 | "Invalid injection response for '%s'.", |
@@ -250,12 +250,12 @@ discard block |
||
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | return $instance; |
| 253 | - } catch (TracedContainerException $e) { |
|
| 253 | + }catch (TracedContainerException $e){ |
|
| 254 | 254 | throw isset($injectorInstance) ? $e : $e::createWithTrace(\sprintf( |
| 255 | 255 | 'Can\'t resolve `%s`.', |
| 256 | 256 | $tracer->getRootAlias(), |
| 257 | 257 | ), $tracer->getTraces(), $e); |
| 258 | - } finally { |
|
| 258 | + }finally{ |
|
| 259 | 259 | $this->state->bindings[$ctx->class] ??= $binding; |
| 260 | 260 | } |
| 261 | 261 | } |
@@ -263,22 +263,22 @@ discard block |
||
| 263 | 263 | private function resolveAlias( |
| 264 | 264 | Config\Alias $binding, |
| 265 | 265 | string $alias, |
| 266 | - \Stringable|string|null $context, |
|
| 266 | + \Stringable | string | null $context, |
|
| 267 | 267 | array $arguments, |
| 268 | 268 | Tracer $tracer, |
| 269 | 269 | ): mixed { |
| 270 | - if ($binding->alias === $alias) { |
|
| 270 | + if ($binding->alias === $alias){ |
|
| 271 | 271 | $instance = $this->autowire( |
| 272 | 272 | new Ctx(alias: $alias, class: $binding->alias, context: $context, singleton: $binding->singleton && $arguments === []), |
| 273 | 273 | $arguments, |
| 274 | 274 | $this, |
| 275 | 275 | $tracer, |
| 276 | 276 | ); |
| 277 | - } else { |
|
| 278 | - try { |
|
| 277 | + }else{ |
|
| 278 | + try{ |
|
| 279 | 279 | //Binding is pointing to something else |
| 280 | 280 | $instance = $this->factory->make($binding->alias, $arguments, $context); |
| 281 | - } catch (TracedContainerException $e) { |
|
| 281 | + }catch (TracedContainerException $e){ |
|
| 282 | 282 | throw $e::createWithTrace( |
| 283 | 283 | $alias === $tracer->getRootAlias() |
| 284 | 284 | ? "Can't resolve `{$alias}`." |
@@ -295,9 +295,9 @@ discard block |
||
| 295 | 295 | return $instance; |
| 296 | 296 | } |
| 297 | 297 | |
| 298 | - private function resolveProxy(Config\Proxy $binding, string $alias, \Stringable|string|null $context): mixed |
|
| 298 | + private function resolveProxy(Config\Proxy $binding, string $alias, \Stringable | string | null $context): mixed |
|
| 299 | 299 | { |
| 300 | - if ($context instanceof RetryContext) { |
|
| 300 | + if ($context instanceof RetryContext){ |
|
| 301 | 301 | return $binding->fallbackFactory === null |
| 302 | 302 | ? throw new RecursiveProxyException( |
| 303 | 303 | $alias, |
@@ -308,7 +308,7 @@ discard block |
||
| 308 | 308 | |
| 309 | 309 | $result = Proxy::create(new \ReflectionClass($binding->getReturnClass()), $context, new Attribute\Proxy()); |
| 310 | 310 | |
| 311 | - if ($binding->singleton) { |
|
| 311 | + if ($binding->singleton){ |
|
| 312 | 312 | $this->state->singletons[$alias] = $result; |
| 313 | 313 | } |
| 314 | 314 | |
@@ -318,11 +318,11 @@ discard block |
||
| 318 | 318 | private function resolveShared( |
| 319 | 319 | Config\Shared $binding, |
| 320 | 320 | string $alias, |
| 321 | - \Stringable|string|null $context, |
|
| 321 | + \Stringable | string | null $context, |
|
| 322 | 322 | array $arguments, |
| 323 | 323 | Tracer $tracer, |
| 324 | 324 | ): object { |
| 325 | - if ($arguments !== []) { |
|
| 325 | + if ($arguments !== []){ |
|
| 326 | 326 | // Avoid singleton cache |
| 327 | 327 | return $this->createInstance( |
| 328 | 328 | new Ctx(alias: $alias, class: $binding->value::class, context: $context, singleton: false), |
@@ -332,7 +332,7 @@ discard block |
||
| 332 | 332 | ); |
| 333 | 333 | } |
| 334 | 334 | |
| 335 | - if ($binding->singleton) { |
|
| 335 | + if ($binding->singleton){ |
|
| 336 | 336 | $this->state->singletons[$alias] = $binding->value; |
| 337 | 337 | } |
| 338 | 338 | |
@@ -342,16 +342,16 @@ discard block |
||
| 342 | 342 | private function resolveAutowire( |
| 343 | 343 | Config\Autowire $binding, |
| 344 | 344 | string $alias, |
| 345 | - \Stringable|string|null $context, |
|
| 345 | + \Stringable | string | null $context, |
|
| 346 | 346 | array $arguments, |
| 347 | 347 | Tracer $tracer, |
| 348 | 348 | ): mixed { |
| 349 | 349 | $target = $binding->autowire->alias; |
| 350 | 350 | $ctx = new Ctx(alias: $alias, class: $target, context: $context, singleton: $binding->singleton && $arguments === [] ?: null); |
| 351 | 351 | |
| 352 | - if ($alias === $target) { |
|
| 352 | + if ($alias === $target){ |
|
| 353 | 353 | $instance = $this->autowire($ctx, \array_merge($binding->autowire->parameters, $arguments), $this, $tracer); |
| 354 | - } else { |
|
| 354 | + }else{ |
|
| 355 | 355 | $instance = $binding->autowire->resolve($this->factory, $arguments); |
| 356 | 356 | $this->validateConstraint($instance, $ctx); |
| 357 | 357 | } |
@@ -360,32 +360,32 @@ discard block |
||
| 360 | 360 | } |
| 361 | 361 | |
| 362 | 362 | private function resolveFactory( |
| 363 | - Config\Factory|Config\DeferredFactory $binding, |
|
| 363 | + Config\Factory | Config\DeferredFactory $binding, |
|
| 364 | 364 | string $alias, |
| 365 | - \Stringable|string|null $context, |
|
| 365 | + \Stringable | string | null $context, |
|
| 366 | 366 | array $arguments, |
| 367 | 367 | Tracer $tracer, |
| 368 | 368 | ): mixed { |
| 369 | 369 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton && $arguments === [] ?: null); |
| 370 | - try { |
|
| 370 | + try{ |
|
| 371 | 371 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
| 372 | 372 | ? ($binding->factory)() |
| 373 | 373 | : $this->invoker->invoke($binding->factory, $arguments); |
| 374 | - } catch (NotCallableException $e) { |
|
| 374 | + }catch (NotCallableException $e){ |
|
| 375 | 375 | throw TracedContainerException::createWithTrace( |
| 376 | 376 | \sprintf('Invalid callable binding for `%s`.', $ctx->alias), |
| 377 | 377 | $tracer->getTraces(), |
| 378 | 378 | $e, |
| 379 | 379 | ); |
| 380 | - } catch (TracedContainerException $e) { |
|
| 380 | + }catch (TracedContainerException $e){ |
|
| 381 | 381 | throw $e::createWithTrace( |
| 382 | 382 | \sprintf("Can't resolve `%s`: factory invocation failed.", $tracer->getRootAlias()), |
| 383 | 383 | $tracer->getTraces(), |
| 384 | 384 | $e, |
| 385 | 385 | ); |
| 386 | - } catch (ContainerExceptionInterface $e) { |
|
| 386 | + }catch (ContainerExceptionInterface $e){ |
|
| 387 | 387 | throw $e; |
| 388 | - } catch (\Throwable $e) { |
|
| 388 | + }catch (\Throwable $e){ |
|
| 389 | 389 | throw NotFoundException::createWithTrace( |
| 390 | 390 | \sprintf("Can't resolve `%s` due to factory invocation error: %s", $tracer->getRootAlias(), $e->getMessage()), |
| 391 | 391 | $tracer->getTraces(), |
@@ -393,7 +393,7 @@ discard block |
||
| 393 | 393 | ); |
| 394 | 394 | } |
| 395 | 395 | |
| 396 | - if (\is_object($instance)) { |
|
| 396 | + if (\is_object($instance)){ |
|
| 397 | 397 | $this->validateConstraint($instance, $ctx); |
| 398 | 398 | return $this->registerInstance($ctx, $instance); |
| 399 | 399 | } |
@@ -404,14 +404,14 @@ discard block |
||
| 404 | 404 | private function resolveWeakReference( |
| 405 | 405 | Config\WeakReference $binding, |
| 406 | 406 | string $alias, |
| 407 | - \Stringable|string|null $context, |
|
| 407 | + \Stringable | string | null $context, |
|
| 408 | 408 | array $arguments, |
| 409 | 409 | Tracer $tracer, |
| 410 | 410 | ): ?object { |
| 411 | 411 | $avoidCache = $arguments !== []; |
| 412 | 412 | |
| 413 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
| 414 | - try { |
|
| 413 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)){ |
|
| 414 | + try{ |
|
| 415 | 415 | $tracer->push(false, alias: $alias, source: \WeakReference::class, context: $context); |
| 416 | 416 | |
| 417 | 417 | $object = $this->createInstance( |
@@ -420,17 +420,17 @@ discard block |
||
| 420 | 420 | $this, |
| 421 | 421 | $tracer, |
| 422 | 422 | ); |
| 423 | - if ($avoidCache) { |
|
| 423 | + if ($avoidCache){ |
|
| 424 | 424 | return $object; |
| 425 | 425 | } |
| 426 | 426 | $binding->reference = \WeakReference::create($object); |
| 427 | - } catch (\Throwable) { |
|
| 427 | + }catch (\Throwable){ |
|
| 428 | 428 | throw TracedContainerException::createWithTrace(\sprintf( |
| 429 | 429 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
| 430 | 430 | $tracer->getRootAlias(), |
| 431 | 431 | $alias, |
| 432 | 432 | ), $tracer->getTraces()); |
| 433 | - } finally { |
|
| 433 | + }finally{ |
|
| 434 | 434 | $tracer->pop(); |
| 435 | 435 | } |
| 436 | 436 | } |
@@ -446,13 +446,13 @@ discard block |
||
| 446 | 446 | object $instance, |
| 447 | 447 | Ctx $ctx, |
| 448 | 448 | ): void { |
| 449 | - if ($this->options->checkScope) { |
|
| 449 | + if ($this->options->checkScope){ |
|
| 450 | 450 | // Check scope name |
| 451 | 451 | $ctx->reflection ??= new \ReflectionClass($instance); |
| 452 | 452 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
| 453 | - if ($scopeName !== null) { |
|
| 453 | + if ($scopeName !== null){ |
|
| 454 | 454 | $scope = $this->scope; |
| 455 | - while ($scope->getScopeName() !== $scopeName) { |
|
| 455 | + while ($scope->getScopeName() !== $scopeName){ |
|
| 456 | 456 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
| 457 | 457 | } |
| 458 | 458 | } |
@@ -479,26 +479,26 @@ discard block |
||
| 479 | 479 | Tracer $tracer, |
| 480 | 480 | ): object { |
| 481 | 481 | $class = $ctx->class; |
| 482 | - try { |
|
| 482 | + try{ |
|
| 483 | 483 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 484 | - } catch (\ReflectionException $e) { |
|
| 484 | + }catch (\ReflectionException $e){ |
|
| 485 | 485 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 486 | 486 | } |
| 487 | 487 | |
| 488 | 488 | // Check Scope attribute |
| 489 | 489 | $actor = $fallbackActor ?? $this; |
| 490 | - if ($this->options->checkScope) { # todo |
|
| 490 | + if ($this->options->checkScope){ # todo |
|
| 491 | 491 | $ar = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null); |
| 492 | - if ($ar !== null) { |
|
| 492 | + if ($ar !== null){ |
|
| 493 | 493 | /** @var Attribute\Scope $attr */ |
| 494 | 494 | $attr = $ar->newInstance(); |
| 495 | 495 | $scope = $this->scope; |
| 496 | 496 | $actor = $this; |
| 497 | 497 | // Go through all parent scopes |
| 498 | 498 | $needed = $actor; |
| 499 | - while ($attr->name !== $scope->getScopeName()) { |
|
| 499 | + while ($attr->name !== $scope->getScopeName()){ |
|
| 500 | 500 | $needed = $scope->getParentActor(); |
| 501 | - if ($needed === null) { |
|
| 501 | + if ($needed === null){ |
|
| 502 | 502 | throw new BadScopeException($attr->name, $class); |
| 503 | 503 | } |
| 504 | 504 | |
@@ -511,11 +511,11 @@ discard block |
||
| 511 | 511 | } # todo |
| 512 | 512 | |
| 513 | 513 | // We have to construct class using external injector when we know the exact context |
| 514 | - if ($arguments === [] && $actor->binder->hasInjector($class)) { |
|
| 514 | + if ($arguments === [] && $actor->binder->hasInjector($class)){ |
|
| 515 | 515 | return $actor->resolveInjector($actor->state->bindings[$ctx->class], $ctx, $arguments, $tracer); |
| 516 | 516 | } |
| 517 | 517 | |
| 518 | - if (!$reflection->isInstantiable()) { |
|
| 518 | + if (!$reflection->isInstantiable()){ |
|
| 519 | 519 | $itIs = match (true) { |
| 520 | 520 | $reflection->isEnum() => 'Enum', |
| 521 | 521 | $reflection->isAbstract() => 'Abstract class', |
@@ -529,8 +529,8 @@ discard block |
||
| 529 | 529 | |
| 530 | 530 | $constructor = $reflection->getConstructor(); |
| 531 | 531 | |
| 532 | - if ($constructor !== null) { |
|
| 533 | - try { |
|
| 532 | + if ($constructor !== null){ |
|
| 533 | + try{ |
|
| 534 | 534 | $newScope = $this !== $actor; |
| 535 | 535 | $debug = [ |
| 536 | 536 | 'action' => 'resolve arguments', |
@@ -544,35 +544,35 @@ discard block |
||
| 544 | 544 | $tracer->push($newScope, ...$debug); |
| 545 | 545 | $tracer->push(true); |
| 546 | 546 | $args = $actor->resolver->resolveArguments($constructor, $arguments, $actor->options->validateArguments); |
| 547 | - } catch (\Throwable $e) { |
|
| 547 | + }catch (\Throwable $e){ |
|
| 548 | 548 | throw TracedContainerException::createWithTrace( |
| 549 | 549 | \sprintf( |
| 550 | 550 | "Can't resolve `%s`.", |
| 551 | 551 | $tracer->getRootAlias(), |
| 552 | 552 | ), $tracer->getTraces(), $e |
| 553 | 553 | ); |
| 554 | - } finally { |
|
| 554 | + }finally{ |
|
| 555 | 555 | $tracer->pop($newScope); |
| 556 | 556 | $tracer->pop(false); |
| 557 | 557 | } |
| 558 | - try { |
|
| 558 | + try{ |
|
| 559 | 559 | // Using constructor with resolved arguments |
| 560 | 560 | $tracer->push(false, call: "$class::__construct", arguments: $args); |
| 561 | 561 | $tracer->push(true); |
| 562 | 562 | $instance = new $class(...$args); |
| 563 | - } catch (\TypeError $e) { |
|
| 563 | + }catch (\TypeError $e){ |
|
| 564 | 564 | throw new WrongTypeException($constructor, $e); |
| 565 | - } catch (TracedContainerException $e) { |
|
| 565 | + }catch (TracedContainerException $e){ |
|
| 566 | 566 | throw $e::createWithTrace(\sprintf( |
| 567 | 567 | 'Can\'t resolve `%s`: failed constructing `%s`.', |
| 568 | 568 | $tracer->getRootAlias(), |
| 569 | 569 | $class, |
| 570 | 570 | ), $tracer->getTraces(), $e); |
| 571 | - } finally { |
|
| 571 | + }finally{ |
|
| 572 | 572 | $tracer->pop(true); |
| 573 | 573 | $tracer->pop(false); |
| 574 | 574 | } |
| 575 | - } else { |
|
| 575 | + }else{ |
|
| 576 | 576 | // No constructor specified |
| 577 | 577 | $instance = $reflection->newInstance(); |
| 578 | 578 | } |
@@ -604,12 +604,12 @@ discard block |
||
| 604 | 604 | */ |
| 605 | 605 | private function isSingleton(Ctx $ctx): bool |
| 606 | 606 | { |
| 607 | - if (is_bool($ctx->singleton)) { |
|
| 607 | + if (is_bool($ctx->singleton)){ |
|
| 608 | 608 | return $ctx->singleton; |
| 609 | 609 | } |
| 610 | 610 | |
| 611 | 611 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
| 612 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
| 612 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)){ |
|
| 613 | 613 | return true; |
| 614 | 614 | } |
| 615 | 615 | |
@@ -623,7 +623,7 @@ discard block |
||
| 623 | 623 | * @var Attribute\Finalize|null $attribute |
| 624 | 624 | */ |
| 625 | 625 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
| 626 | - if ($attribute === null) { |
|
| 626 | + if ($attribute === null){ |
|
| 627 | 627 | return null; |
| 628 | 628 | } |
| 629 | 629 | |
@@ -637,10 +637,10 @@ discard block |
||
| 637 | 637 | { |
| 638 | 638 | $scope = $this->scope; |
| 639 | 639 | |
| 640 | - while ($scope !== null) { |
|
| 641 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
| 642 | - if ($instance instanceof $class) { |
|
| 643 | - foreach ($inflectors as $inflector) { |
|
| 640 | + while ($scope !== null){ |
|
| 641 | + foreach ($this->state->inflectors as $class => $inflectors){ |
|
| 642 | + if ($instance instanceof $class){ |
|
| 643 | + foreach ($inflectors as $inflector){ |
|
| 644 | 644 | $instance = $inflector->getParametersCount() > 1 |
| 645 | 645 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
| 646 | 646 | : ($inflector->inflector)($instance); |
@@ -656,9 +656,9 @@ discard block |
||
| 656 | 656 | |
| 657 | 657 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
| 658 | 658 | { |
| 659 | - try { |
|
| 659 | + try{ |
|
| 660 | 660 | $this->resolver->validateArguments($reflection, $arguments); |
| 661 | - } catch (\Throwable) { |
|
| 661 | + }catch (\Throwable){ |
|
| 662 | 662 | return false; |
| 663 | 663 | } |
| 664 | 664 | |
@@ -96,20 +96,25 @@ discard block |
||
| 96 | 96 | // Aliases to prevent circular dependencies |
| 97 | 97 | $as = []; |
| 98 | 98 | $actor = $this; |
| 99 | - do { |
|
| 99 | + do |
|
| 100 | + { |
|
| 100 | 101 | $bindings = &$actor->state->bindings; |
| 101 | 102 | $singletons = &$actor->state->singletons; |
| 102 | 103 | $injectors = &$actor->state->injectors; |
| 103 | 104 | $binding = $bindings[$alias] ?? null; |
| 104 | - if (\array_key_exists($alias, $singletons)) { |
|
| 105 | + if (\array_key_exists($alias, $singletons)) |
|
| 106 | + { |
|
| 105 | 107 | $singleton = $singletons[$alias]; |
| 106 | 108 | $injector = $injectors[$alias] ?? null; |
| 107 | 109 | return \is_object($singleton::class) ? $singleton::class : null; |
| 108 | 110 | } |
| 109 | 111 | |
| 110 | - if ($binding !== null) { |
|
| 111 | - if ($followAlias && $binding::class === Alias::class) { |
|
| 112 | - if ($binding->alias === $alias) { |
|
| 112 | + if ($binding !== null) |
|
| 113 | + { |
|
| 114 | + if ($followAlias && $binding::class === Alias::class) |
|
| 115 | + { |
|
| 116 | + if ($binding->alias === $alias) |
|
| 117 | + { |
|
| 113 | 118 | break; |
| 114 | 119 | } |
| 115 | 120 | |
@@ -124,7 +129,8 @@ discard block |
||
| 124 | 129 | return $binding->getReturnClass(); |
| 125 | 130 | } |
| 126 | 131 | |
| 127 | - if (\array_key_exists($alias, $injectors)) { |
|
| 132 | + if (\array_key_exists($alias, $injectors)) |
|
| 133 | + { |
|
| 128 | 134 | $injector = $injectors[$alias]; |
| 129 | 135 | $binding = $bindings[$alias] ?? null; |
| 130 | 136 | return $alias; |
@@ -132,7 +138,8 @@ discard block |
||
| 132 | 138 | |
| 133 | 139 | // Go to parent scope |
| 134 | 140 | $parent = $actor->scope->getParentActor(); |
| 135 | - if ($parent === null) { |
|
| 141 | + if ($parent === null) |
|
| 142 | + { |
|
| 136 | 143 | break; |
| 137 | 144 | } |
| 138 | 145 | |
@@ -202,18 +209,23 @@ discard block |
||
| 202 | 209 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments, Tracer $tracer) |
| 203 | 210 | { |
| 204 | 211 | $context = $ctx->context; |
| 205 | - try { |
|
| 212 | + try |
|
| 213 | + { |
|
| 206 | 214 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
| 207 | - } catch (\ReflectionException $e) { |
|
| 215 | + } |
|
| 216 | + catch (\ReflectionException $e) |
|
| 217 | + { |
|
| 208 | 218 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 209 | 219 | } |
| 210 | 220 | |
| 211 | 221 | $injector = $binding->injector; |
| 212 | 222 | |
| 213 | - try { |
|
| 223 | + try |
|
| 224 | + { |
|
| 214 | 225 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
| 215 | 226 | |
| 216 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 227 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 228 | + { |
|
| 217 | 229 | throw new InjectionException( |
| 218 | 230 | \sprintf( |
| 219 | 231 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -240,7 +252,8 @@ discard block |
||
| 240 | 252 | default => (string) $context, |
| 241 | 253 | }); |
| 242 | 254 | |
| 243 | - if (!$reflection->isInstance($instance)) { |
|
| 255 | + if (!$reflection->isInstance($instance)) |
|
| 256 | + { |
|
| 244 | 257 | throw new InjectionException( |
| 245 | 258 | \sprintf( |
| 246 | 259 | "Invalid injection response for '%s'.", |
@@ -250,12 +263,16 @@ discard block |
||
| 250 | 263 | } |
| 251 | 264 | |
| 252 | 265 | return $instance; |
| 253 | - } catch (TracedContainerException $e) { |
|
| 266 | + } |
|
| 267 | + catch (TracedContainerException $e) |
|
| 268 | + { |
|
| 254 | 269 | throw isset($injectorInstance) ? $e : $e::createWithTrace(\sprintf( |
| 255 | 270 | 'Can\'t resolve `%s`.', |
| 256 | 271 | $tracer->getRootAlias(), |
| 257 | 272 | ), $tracer->getTraces(), $e); |
| 258 | - } finally { |
|
| 273 | + } |
|
| 274 | + finally |
|
| 275 | + { |
|
| 259 | 276 | $this->state->bindings[$ctx->class] ??= $binding; |
| 260 | 277 | } |
| 261 | 278 | } |
@@ -267,18 +284,24 @@ discard block |
||
| 267 | 284 | array $arguments, |
| 268 | 285 | Tracer $tracer, |
| 269 | 286 | ): mixed { |
| 270 | - if ($binding->alias === $alias) { |
|
| 287 | + if ($binding->alias === $alias) |
|
| 288 | + { |
|
| 271 | 289 | $instance = $this->autowire( |
| 272 | 290 | new Ctx(alias: $alias, class: $binding->alias, context: $context, singleton: $binding->singleton && $arguments === []), |
| 273 | 291 | $arguments, |
| 274 | 292 | $this, |
| 275 | 293 | $tracer, |
| 276 | 294 | ); |
| 277 | - } else { |
|
| 278 | - try { |
|
| 295 | + } |
|
| 296 | + else |
|
| 297 | + { |
|
| 298 | + try |
|
| 299 | + { |
|
| 279 | 300 | //Binding is pointing to something else |
| 280 | 301 | $instance = $this->factory->make($binding->alias, $arguments, $context); |
| 281 | - } catch (TracedContainerException $e) { |
|
| 302 | + } |
|
| 303 | + catch (TracedContainerException $e) |
|
| 304 | + { |
|
| 282 | 305 | throw $e::createWithTrace( |
| 283 | 306 | $alias === $tracer->getRootAlias() |
| 284 | 307 | ? "Can't resolve `{$alias}`." |
@@ -297,7 +320,8 @@ discard block |
||
| 297 | 320 | |
| 298 | 321 | private function resolveProxy(Config\Proxy $binding, string $alias, \Stringable|string|null $context): mixed |
| 299 | 322 | { |
| 300 | - if ($context instanceof RetryContext) { |
|
| 323 | + if ($context instanceof RetryContext) |
|
| 324 | + { |
|
| 301 | 325 | return $binding->fallbackFactory === null |
| 302 | 326 | ? throw new RecursiveProxyException( |
| 303 | 327 | $alias, |
@@ -308,7 +332,8 @@ discard block |
||
| 308 | 332 | |
| 309 | 333 | $result = Proxy::create(new \ReflectionClass($binding->getReturnClass()), $context, new Attribute\Proxy()); |
| 310 | 334 | |
| 311 | - if ($binding->singleton) { |
|
| 335 | + if ($binding->singleton) |
|
| 336 | + { |
|
| 312 | 337 | $this->state->singletons[$alias] = $result; |
| 313 | 338 | } |
| 314 | 339 | |
@@ -322,7 +347,8 @@ discard block |
||
| 322 | 347 | array $arguments, |
| 323 | 348 | Tracer $tracer, |
| 324 | 349 | ): object { |
| 325 | - if ($arguments !== []) { |
|
| 350 | + if ($arguments !== []) |
|
| 351 | + { |
|
| 326 | 352 | // Avoid singleton cache |
| 327 | 353 | return $this->createInstance( |
| 328 | 354 | new Ctx(alias: $alias, class: $binding->value::class, context: $context, singleton: false), |
@@ -332,7 +358,8 @@ discard block |
||
| 332 | 358 | ); |
| 333 | 359 | } |
| 334 | 360 | |
| 335 | - if ($binding->singleton) { |
|
| 361 | + if ($binding->singleton) |
|
| 362 | + { |
|
| 336 | 363 | $this->state->singletons[$alias] = $binding->value; |
| 337 | 364 | } |
| 338 | 365 | |
@@ -349,9 +376,12 @@ discard block |
||
| 349 | 376 | $target = $binding->autowire->alias; |
| 350 | 377 | $ctx = new Ctx(alias: $alias, class: $target, context: $context, singleton: $binding->singleton && $arguments === [] ?: null); |
| 351 | 378 | |
| 352 | - if ($alias === $target) { |
|
| 379 | + if ($alias === $target) |
|
| 380 | + { |
|
| 353 | 381 | $instance = $this->autowire($ctx, \array_merge($binding->autowire->parameters, $arguments), $this, $tracer); |
| 354 | - } else { |
|
| 382 | + } |
|
| 383 | + else |
|
| 384 | + { |
|
| 355 | 385 | $instance = $binding->autowire->resolve($this->factory, $arguments); |
| 356 | 386 | $this->validateConstraint($instance, $ctx); |
| 357 | 387 | } |
@@ -367,25 +397,34 @@ discard block |
||
| 367 | 397 | Tracer $tracer, |
| 368 | 398 | ): mixed { |
| 369 | 399 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton && $arguments === [] ?: null); |
| 370 | - try { |
|
| 400 | + try |
|
| 401 | + { |
|
| 371 | 402 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
| 372 | 403 | ? ($binding->factory)() |
| 373 | 404 | : $this->invoker->invoke($binding->factory, $arguments); |
| 374 | - } catch (NotCallableException $e) { |
|
| 405 | + } |
|
| 406 | + catch (NotCallableException $e) |
|
| 407 | + { |
|
| 375 | 408 | throw TracedContainerException::createWithTrace( |
| 376 | 409 | \sprintf('Invalid callable binding for `%s`.', $ctx->alias), |
| 377 | 410 | $tracer->getTraces(), |
| 378 | 411 | $e, |
| 379 | 412 | ); |
| 380 | - } catch (TracedContainerException $e) { |
|
| 413 | + } |
|
| 414 | + catch (TracedContainerException $e) |
|
| 415 | + { |
|
| 381 | 416 | throw $e::createWithTrace( |
| 382 | 417 | \sprintf("Can't resolve `%s`: factory invocation failed.", $tracer->getRootAlias()), |
| 383 | 418 | $tracer->getTraces(), |
| 384 | 419 | $e, |
| 385 | 420 | ); |
| 386 | - } catch (ContainerExceptionInterface $e) { |
|
| 421 | + } |
|
| 422 | + catch (ContainerExceptionInterface $e) |
|
| 423 | + { |
|
| 387 | 424 | throw $e; |
| 388 | - } catch (\Throwable $e) { |
|
| 425 | + } |
|
| 426 | + catch (\Throwable $e) |
|
| 427 | + { |
|
| 389 | 428 | throw NotFoundException::createWithTrace( |
| 390 | 429 | \sprintf("Can't resolve `%s` due to factory invocation error: %s", $tracer->getRootAlias(), $e->getMessage()), |
| 391 | 430 | $tracer->getTraces(), |
@@ -393,7 +432,8 @@ discard block |
||
| 393 | 432 | ); |
| 394 | 433 | } |
| 395 | 434 | |
| 396 | - if (\is_object($instance)) { |
|
| 435 | + if (\is_object($instance)) |
|
| 436 | + { |
|
| 397 | 437 | $this->validateConstraint($instance, $ctx); |
| 398 | 438 | return $this->registerInstance($ctx, $instance); |
| 399 | 439 | } |
@@ -410,8 +450,10 @@ discard block |
||
| 410 | 450 | ): ?object { |
| 411 | 451 | $avoidCache = $arguments !== []; |
| 412 | 452 | |
| 413 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
| 414 | - try { |
|
| 453 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
| 454 | + { |
|
| 455 | + try |
|
| 456 | + { |
|
| 415 | 457 | $tracer->push(false, alias: $alias, source: \WeakReference::class, context: $context); |
| 416 | 458 | |
| 417 | 459 | $object = $this->createInstance( |
@@ -420,17 +462,22 @@ discard block |
||
| 420 | 462 | $this, |
| 421 | 463 | $tracer, |
| 422 | 464 | ); |
| 423 | - if ($avoidCache) { |
|
| 465 | + if ($avoidCache) |
|
| 466 | + { |
|
| 424 | 467 | return $object; |
| 425 | 468 | } |
| 426 | 469 | $binding->reference = \WeakReference::create($object); |
| 427 | - } catch (\Throwable) { |
|
| 470 | + } |
|
| 471 | + catch (\Throwable) |
|
| 472 | + { |
|
| 428 | 473 | throw TracedContainerException::createWithTrace(\sprintf( |
| 429 | 474 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
| 430 | 475 | $tracer->getRootAlias(), |
| 431 | 476 | $alias, |
| 432 | 477 | ), $tracer->getTraces()); |
| 433 | - } finally { |
|
| 478 | + } |
|
| 479 | + finally |
|
| 480 | + { |
|
| 434 | 481 | $tracer->pop(); |
| 435 | 482 | } |
| 436 | 483 | } |
@@ -446,13 +493,16 @@ discard block |
||
| 446 | 493 | object $instance, |
| 447 | 494 | Ctx $ctx, |
| 448 | 495 | ): void { |
| 449 | - if ($this->options->checkScope) { |
|
| 496 | + if ($this->options->checkScope) |
|
| 497 | + { |
|
| 450 | 498 | // Check scope name |
| 451 | 499 | $ctx->reflection ??= new \ReflectionClass($instance); |
| 452 | 500 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
| 453 | - if ($scopeName !== null) { |
|
| 501 | + if ($scopeName !== null) |
|
| 502 | + { |
|
| 454 | 503 | $scope = $this->scope; |
| 455 | - while ($scope->getScopeName() !== $scopeName) { |
|
| 504 | + while ($scope->getScopeName() !== $scopeName) |
|
| 505 | + { |
|
| 456 | 506 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
| 457 | 507 | } |
| 458 | 508 | } |
@@ -479,26 +529,34 @@ discard block |
||
| 479 | 529 | Tracer $tracer, |
| 480 | 530 | ): object { |
| 481 | 531 | $class = $ctx->class; |
| 482 | - try { |
|
| 532 | + try |
|
| 533 | + { |
|
| 483 | 534 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 484 | - } catch (\ReflectionException $e) { |
|
| 535 | + } |
|
| 536 | + catch (\ReflectionException $e) |
|
| 537 | + { |
|
| 485 | 538 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 486 | 539 | } |
| 487 | 540 | |
| 488 | 541 | // Check Scope attribute |
| 489 | 542 | $actor = $fallbackActor ?? $this; |
| 490 | - if ($this->options->checkScope) { # todo |
|
| 543 | + if ($this->options->checkScope) |
|
| 544 | + { |
|
| 545 | +# todo |
|
| 491 | 546 | $ar = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null); |
| 492 | - if ($ar !== null) { |
|
| 547 | + if ($ar !== null) |
|
| 548 | + { |
|
| 493 | 549 | /** @var Attribute\Scope $attr */ |
| 494 | 550 | $attr = $ar->newInstance(); |
| 495 | 551 | $scope = $this->scope; |
| 496 | 552 | $actor = $this; |
| 497 | 553 | // Go through all parent scopes |
| 498 | 554 | $needed = $actor; |
| 499 | - while ($attr->name !== $scope->getScopeName()) { |
|
| 555 | + while ($attr->name !== $scope->getScopeName()) |
|
| 556 | + { |
|
| 500 | 557 | $needed = $scope->getParentActor(); |
| 501 | - if ($needed === null) { |
|
| 558 | + if ($needed === null) |
|
| 559 | + { |
|
| 502 | 560 | throw new BadScopeException($attr->name, $class); |
| 503 | 561 | } |
| 504 | 562 | |
@@ -511,11 +569,13 @@ discard block |
||
| 511 | 569 | } # todo |
| 512 | 570 | |
| 513 | 571 | // We have to construct class using external injector when we know the exact context |
| 514 | - if ($arguments === [] && $actor->binder->hasInjector($class)) { |
|
| 572 | + if ($arguments === [] && $actor->binder->hasInjector($class)) |
|
| 573 | + { |
|
| 515 | 574 | return $actor->resolveInjector($actor->state->bindings[$ctx->class], $ctx, $arguments, $tracer); |
| 516 | 575 | } |
| 517 | 576 | |
| 518 | - if (!$reflection->isInstantiable()) { |
|
| 577 | + if (!$reflection->isInstantiable()) |
|
| 578 | + { |
|
| 519 | 579 | $itIs = match (true) { |
| 520 | 580 | $reflection->isEnum() => 'Enum', |
| 521 | 581 | $reflection->isAbstract() => 'Abstract class', |
@@ -529,8 +589,10 @@ discard block |
||
| 529 | 589 | |
| 530 | 590 | $constructor = $reflection->getConstructor(); |
| 531 | 591 | |
| 532 | - if ($constructor !== null) { |
|
| 533 | - try { |
|
| 592 | + if ($constructor !== null) |
|
| 593 | + { |
|
| 594 | + try |
|
| 595 | + { |
|
| 534 | 596 | $newScope = $this !== $actor; |
| 535 | 597 | $debug = [ |
| 536 | 598 | 'action' => 'resolve arguments', |
@@ -544,35 +606,48 @@ discard block |
||
| 544 | 606 | $tracer->push($newScope, ...$debug); |
| 545 | 607 | $tracer->push(true); |
| 546 | 608 | $args = $actor->resolver->resolveArguments($constructor, $arguments, $actor->options->validateArguments); |
| 547 | - } catch (\Throwable $e) { |
|
| 609 | + } |
|
| 610 | + catch (\Throwable $e) |
|
| 611 | + { |
|
| 548 | 612 | throw TracedContainerException::createWithTrace( |
| 549 | 613 | \sprintf( |
| 550 | 614 | "Can't resolve `%s`.", |
| 551 | 615 | $tracer->getRootAlias(), |
| 552 | 616 | ), $tracer->getTraces(), $e |
| 553 | 617 | ); |
| 554 | - } finally { |
|
| 618 | + } |
|
| 619 | + finally |
|
| 620 | + { |
|
| 555 | 621 | $tracer->pop($newScope); |
| 556 | 622 | $tracer->pop(false); |
| 557 | 623 | } |
| 558 | - try { |
|
| 624 | + try |
|
| 625 | + { |
|
| 559 | 626 | // Using constructor with resolved arguments |
| 560 | 627 | $tracer->push(false, call: "$class::__construct", arguments: $args); |
| 561 | 628 | $tracer->push(true); |
| 562 | 629 | $instance = new $class(...$args); |
| 563 | - } catch (\TypeError $e) { |
|
| 630 | + } |
|
| 631 | + catch (\TypeError $e) |
|
| 632 | + { |
|
| 564 | 633 | throw new WrongTypeException($constructor, $e); |
| 565 | - } catch (TracedContainerException $e) { |
|
| 634 | + } |
|
| 635 | + catch (TracedContainerException $e) |
|
| 636 | + { |
|
| 566 | 637 | throw $e::createWithTrace(\sprintf( |
| 567 | 638 | 'Can\'t resolve `%s`: failed constructing `%s`.', |
| 568 | 639 | $tracer->getRootAlias(), |
| 569 | 640 | $class, |
| 570 | 641 | ), $tracer->getTraces(), $e); |
| 571 | - } finally { |
|
| 642 | + } |
|
| 643 | + finally |
|
| 644 | + { |
|
| 572 | 645 | $tracer->pop(true); |
| 573 | 646 | $tracer->pop(false); |
| 574 | 647 | } |
| 575 | - } else { |
|
| 648 | + } |
|
| 649 | + else |
|
| 650 | + { |
|
| 576 | 651 | // No constructor specified |
| 577 | 652 | $instance = $reflection->newInstance(); |
| 578 | 653 | } |
@@ -604,12 +679,14 @@ discard block |
||
| 604 | 679 | */ |
| 605 | 680 | private function isSingleton(Ctx $ctx): bool |
| 606 | 681 | { |
| 607 | - if (is_bool($ctx->singleton)) { |
|
| 682 | + if (is_bool($ctx->singleton)) |
|
| 683 | + { |
|
| 608 | 684 | return $ctx->singleton; |
| 609 | 685 | } |
| 610 | 686 | |
| 611 | 687 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
| 612 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
| 688 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
| 689 | + { |
|
| 613 | 690 | return true; |
| 614 | 691 | } |
| 615 | 692 | |
@@ -623,7 +700,8 @@ discard block |
||
| 623 | 700 | * @var Attribute\Finalize|null $attribute |
| 624 | 701 | */ |
| 625 | 702 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
| 626 | - if ($attribute === null) { |
|
| 703 | + if ($attribute === null) |
|
| 704 | + { |
|
| 627 | 705 | return null; |
| 628 | 706 | } |
| 629 | 707 | |
@@ -637,10 +715,14 @@ discard block |
||
| 637 | 715 | { |
| 638 | 716 | $scope = $this->scope; |
| 639 | 717 | |
| 640 | - while ($scope !== null) { |
|
| 641 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
| 642 | - if ($instance instanceof $class) { |
|
| 643 | - foreach ($inflectors as $inflector) { |
|
| 718 | + while ($scope !== null) |
|
| 719 | + { |
|
| 720 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
| 721 | + { |
|
| 722 | + if ($instance instanceof $class) |
|
| 723 | + { |
|
| 724 | + foreach ($inflectors as $inflector) |
|
| 725 | + { |
|
| 644 | 726 | $instance = $inflector->getParametersCount() > 1 |
| 645 | 727 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
| 646 | 728 | : ($inflector->inflector)($instance); |
@@ -656,9 +738,12 @@ discard block |
||
| 656 | 738 | |
| 657 | 739 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
| 658 | 740 | { |
| 659 | - try { |
|
| 741 | + try |
|
| 742 | + { |
|
| 660 | 743 | $this->resolver->validateArguments($reflection, $arguments); |
| 661 | - } catch (\Throwable) { |
|
| 744 | + } |
|
| 745 | + catch (\Throwable) |
|
| 746 | + { |
|
| 662 | 747 | return false; |
| 663 | 748 | } |
| 664 | 749 | |
@@ -30,9 +30,9 @@ discard block |
||
| 30 | 30 | ): static { |
| 31 | 31 | $class = static::class; |
| 32 | 32 | // Merge traces |
| 33 | - if ($previous instanceof self) { |
|
| 33 | + if ($previous instanceof self){ |
|
| 34 | 34 | $merge = $previous->containerTrace; |
| 35 | - if ($trace !== [] && $merge !== []) { |
|
| 35 | + if ($trace !== [] && $merge !== []){ |
|
| 36 | 36 | // merge lat element of $traces with first element of $merge |
| 37 | 37 | \array_push($trace[\count($trace) - 1], ...$merge[0]); |
| 38 | 38 | unset($merge[0]); |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | $result = $class::createStatic( |
| 47 | - $message . ($trace === [] ? '' : "\nResolving trace:\n" . Tracer::renderTraceList($trace)), |
|
| 47 | + $message.($trace === [] ? '' : "\nResolving trace:\n".Tracer::renderTraceList($trace)), |
|
| 48 | 48 | $previous, |
| 49 | 49 | ); |
| 50 | 50 | $result->originalMessage = $message; |
@@ -54,6 +54,6 @@ discard block |
||
| 54 | 54 | |
| 55 | 55 | protected static function createStatic(string $message, ?\Throwable $previous): static |
| 56 | 56 | { |
| 57 | - return new static($message, previous: $previous); |
|
| 57 | + return new static($message, previous : $previous); |
|
| 58 | 58 | } |
| 59 | 59 | } |
@@ -30,9 +30,11 @@ |
||
| 30 | 30 | ): static { |
| 31 | 31 | $class = static::class; |
| 32 | 32 | // Merge traces |
| 33 | - if ($previous instanceof self) { |
|
| 33 | + if ($previous instanceof self) |
|
| 34 | + { |
|
| 34 | 35 | $merge = $previous->containerTrace; |
| 35 | - if ($trace !== [] && $merge !== []) { |
|
| 36 | + if ($trace !== [] && $merge !== []) |
|
| 37 | + { |
|
| 36 | 38 | // merge lat element of $traces with first element of $merge |
| 37 | 39 | \array_push($trace[\count($trace) - 1], ...$merge[0]); |
| 38 | 40 | unset($merge[0]); |
@@ -6,4 +6,4 @@ |
||
| 6 | 6 | |
| 7 | 7 | use Spiral\Core\Exception\Container\TracedContainerException; |
| 8 | 8 | |
| 9 | -class ResolvingException extends TracedContainerException {} |
|
| 9 | +class ResolvingException extends TracedContainerException{} |
|
@@ -6,4 +6,6 @@ |
||
| 6 | 6 | |
| 7 | 7 | use Spiral\Core\Exception\Container\TracedContainerException; |
| 8 | 8 | |
| 9 | -class ResolvingException extends TracedContainerException {} |
|
| 9 | +class ResolvingException extends TracedContainerException |
|
| 10 | +{ |
|
| 11 | +} |
|
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | protected readonly \ReflectionFunctionAbstract $reflection, |
| 15 | 15 | protected readonly string $parameter, |
| 16 | 16 | ?string $message = null, |
| 17 | - ) { |
|
| 17 | + ){ |
|
| 18 | 18 | $message ??= $this->getValidationMessage($reflection, $parameter); |
| 19 | 19 | parent::__construct($this->renderFunctionAndParameter($reflection, $message)); |
| 20 | 20 | } |
@@ -16,6 +16,6 @@ |
||
| 16 | 16 | |
| 17 | 17 | public function getParameter(): string |
| 18 | 18 | { |
| 19 | - return '#' . $this->parameter; |
|
| 19 | + return '#'.$this->parameter; |
|
| 20 | 20 | } |
| 21 | 21 | } |
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | private readonly \ReflectionFunctionAbstract $reflection, |
| 15 | 15 | private readonly string $parameter, |
| 16 | 16 | ?string $message = null, |
| 17 | - ) { |
|
| 17 | + ){ |
|
| 18 | 18 | $message ??= "Unable to resolve required argument `{$parameter}` when resolving `%s` %s."; |
| 19 | 19 | parent::__construct($this->renderFunctionAndParameter($reflection, $message)); |
| 20 | 20 | } |