@@ -49,7 +49,8 @@ |
||
| 49 | 49 | */ |
| 50 | 50 | public function get(string|Autowire $id, \Stringable|string|null $context = null): mixed |
| 51 | 51 | { |
| 52 | - if ($id instanceof Autowire) { |
|
| 52 | + if ($id instanceof Autowire) |
|
| 53 | + { |
|
| 53 | 54 | return $id->resolve($this->factory); |
| 54 | 55 | } |
| 55 | 56 | |
@@ -52,7 +52,8 @@ discard block |
||
| 52 | 52 | ): array { |
| 53 | 53 | $state = new ResolvingState($reflection, $parameters); |
| 54 | 54 | |
| 55 | - foreach ($reflection->getParameters() as $parameter) { |
|
| 55 | + foreach ($reflection->getParameters() as $parameter) |
|
| 56 | + { |
|
| 56 | 57 | $this->resolveParameter($parameter, $state, $validate) |
| 57 | 58 | or |
| 58 | 59 | throw new ArgumentResolvingException($reflection, $parameter->getName()); |
@@ -66,47 +67,59 @@ discard block |
||
| 66 | 67 | $positional = true; |
| 67 | 68 | $variadic = false; |
| 68 | 69 | $parameters = $reflection->getParameters(); |
| 69 | - if (\count($parameters) === 0) { |
|
| 70 | + if (\count($parameters) === 0) |
|
| 71 | + { |
|
| 70 | 72 | return; |
| 71 | 73 | } |
| 72 | 74 | |
| 73 | 75 | $parameter = null; |
| 74 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
| 76 | + while (\count($parameters) > 0 || \count($arguments) > 0) |
|
| 77 | + { |
|
| 75 | 78 | // get related argument value |
| 76 | 79 | $key = \key($arguments); |
| 77 | 80 | |
| 78 | 81 | // For a variadic parameter it's no sense - named or positional argument will be sent |
| 79 | 82 | // But you can't send positional argument after named in any case |
| 80 | - if (\is_int($key) && !$positional) { |
|
| 83 | + if (\is_int($key) && !$positional) |
|
| 84 | + { |
|
| 81 | 85 | throw new PositionalArgumentException($reflection, $key); |
| 82 | 86 | } |
| 83 | 87 | |
| 84 | 88 | $positional = $positional && \is_int($key); |
| 85 | 89 | |
| 86 | - if (!$variadic) { |
|
| 90 | + if (!$variadic) |
|
| 91 | + { |
|
| 87 | 92 | $parameter = \array_shift($parameters); |
| 88 | 93 | $variadic = $parameter?->isVariadic() ?? false; |
| 89 | 94 | } |
| 90 | 95 | |
| 91 | - if ($parameter === null) { |
|
| 96 | + if ($parameter === null) |
|
| 97 | + { |
|
| 92 | 98 | throw new UnknownParameterException($reflection, $key); |
| 93 | 99 | } |
| 94 | 100 | $name = $parameter->getName(); |
| 95 | 101 | |
| 96 | - if (($positional || $variadic) && $key !== null) { |
|
| 102 | + if (($positional || $variadic) && $key !== null) |
|
| 103 | + { |
|
| 97 | 104 | /** @psalm-suppress ReferenceReusedFromConfusingScope */ |
| 98 | 105 | $value = \array_shift($arguments); |
| 99 | - } elseif ($key === null || !\array_key_exists($name, $arguments)) { |
|
| 100 | - if ($parameter->isOptional()) { |
|
| 106 | + } |
|
| 107 | + elseif ($key === null || !\array_key_exists($name, $arguments)) |
|
| 108 | + { |
|
| 109 | + if ($parameter->isOptional()) |
|
| 110 | + { |
|
| 101 | 111 | continue; |
| 102 | 112 | } |
| 103 | 113 | throw new MissingRequiredArgumentException($reflection, $name); |
| 104 | - } else { |
|
| 114 | + } |
|
| 115 | + else |
|
| 116 | + { |
|
| 105 | 117 | $value = &$arguments[$name]; |
| 106 | 118 | unset($arguments[$name]); |
| 107 | 119 | } |
| 108 | 120 | |
| 109 | - if (!$this->validateValueToParameter($parameter, $value)) { |
|
| 121 | + if (!$this->validateValueToParameter($parameter, $value)) |
|
| 122 | + { |
|
| 110 | 123 | throw new InvalidArgumentException($reflection, $name); |
| 111 | 124 | } |
| 112 | 125 | } |
@@ -114,7 +127,8 @@ discard block |
||
| 114 | 127 | |
| 115 | 128 | private function validateValueToParameter(ReflectionParameter $parameter, mixed $value): bool |
| 116 | 129 | { |
| 117 | - if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) { |
|
| 130 | + if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) |
|
| 131 | + { |
|
| 118 | 132 | return true; |
| 119 | 133 | } |
| 120 | 134 | $type = $parameter->getType(); |
@@ -125,17 +139,21 @@ discard block |
||
| 125 | 139 | $type instanceof ReflectionIntersectionType => [false, $type->getTypes()], |
| 126 | 140 | }; |
| 127 | 141 | |
| 128 | - foreach ($types as $t) { |
|
| 142 | + foreach ($types as $t) |
|
| 143 | + { |
|
| 129 | 144 | \assert($t instanceof ReflectionNamedType); |
| 130 | - if (!$this->validateValueNamedType($t, $value)) { |
|
| 145 | + if (!$this->validateValueNamedType($t, $value)) |
|
| 146 | + { |
|
| 131 | 147 | // If it is TypeIntersection |
| 132 | - if ($or) { |
|
| 148 | + if ($or) |
|
| 149 | + { |
|
| 133 | 150 | continue; |
| 134 | 151 | } |
| 135 | 152 | return false; |
| 136 | 153 | } |
| 137 | 154 | // If it is not type intersection then we can skip that value after first successful check |
| 138 | - if ($or) { |
|
| 155 | + if ($or) |
|
| 156 | + { |
|
| 139 | 157 | return true; |
| 140 | 158 | } |
| 141 | 159 | } |
@@ -150,7 +168,8 @@ discard block |
||
| 150 | 168 | { |
| 151 | 169 | $name = $type->getName(); |
| 152 | 170 | |
| 153 | - if ($type->isBuiltin()) { |
|
| 171 | + if ($type->isBuiltin()) |
|
| 172 | + { |
|
| 154 | 173 | return match ($name) { |
| 155 | 174 | 'mixed' => true, |
| 156 | 175 | 'string' => \is_string($value), |
@@ -183,13 +202,18 @@ discard block |
||
| 183 | 202 | |
| 184 | 203 | // Try to resolve parameter by name |
| 185 | 204 | $res = $state->resolveParameterByNameOrPosition($parameter, $isVariadic); |
| 186 | - if ($res !== [] || $isVariadic) { |
|
| 205 | + if ($res !== [] || $isVariadic) |
|
| 206 | + { |
|
| 187 | 207 | // validate |
| 188 | - if ($isVariadic) { |
|
| 189 | - foreach ($res as $k => &$v) { |
|
| 208 | + if ($isVariadic) |
|
| 209 | + { |
|
| 210 | + foreach ($res as $k => &$v) |
|
| 211 | + { |
|
| 190 | 212 | $this->processArgument($state, $v, validateWith: $validate ? $parameter : null, key: $k); |
| 191 | 213 | } |
| 192 | - } else { |
|
| 214 | + } |
|
| 215 | + else |
|
| 216 | + { |
|
| 193 | 217 | $this->processArgument($state, $res[0], validateWith: $validate ? $parameter : null); |
| 194 | 218 | } |
| 195 | 219 | |
@@ -197,39 +221,49 @@ discard block |
||
| 197 | 221 | } |
| 198 | 222 | |
| 199 | 223 | $error = null; |
| 200 | - if ($hasType) { |
|
| 224 | + if ($hasType) |
|
| 225 | + { |
|
| 201 | 226 | /** @var ReflectionIntersectionType|ReflectionUnionType|ReflectionNamedType $reflectionType */ |
| 202 | 227 | $reflectionType = $parameter->getType(); |
| 203 | 228 | |
| 204 | - if ($reflectionType instanceof ReflectionIntersectionType) { |
|
| 229 | + if ($reflectionType instanceof ReflectionIntersectionType) |
|
| 230 | + { |
|
| 205 | 231 | throw new UnsupportedTypeException($parameter->getDeclaringFunction(), $parameter->getName()); |
| 206 | 232 | } |
| 207 | 233 | |
| 208 | 234 | $types = $reflectionType instanceof ReflectionNamedType ? [$reflectionType] : $reflectionType->getTypes(); |
| 209 | - foreach ($types as $namedType) { |
|
| 210 | - try { |
|
| 211 | - if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $parameter, $validate)) { |
|
| 235 | + foreach ($types as $namedType) |
|
| 236 | + { |
|
| 237 | + try |
|
| 238 | + { |
|
| 239 | + if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $parameter, $validate)) |
|
| 240 | + { |
|
| 212 | 241 | return true; |
| 213 | 242 | } |
| 214 | - } catch (Throwable $e) { |
|
| 243 | + } |
|
| 244 | + catch (Throwable $e) |
|
| 245 | + { |
|
| 215 | 246 | $error = $e; |
| 216 | 247 | } |
| 217 | 248 | } |
| 218 | 249 | } |
| 219 | 250 | |
| 220 | - if ($parameter->isDefaultValueAvailable()) { |
|
| 251 | + if ($parameter->isDefaultValueAvailable()) |
|
| 252 | + { |
|
| 221 | 253 | $argument = $parameter->getDefaultValue(); |
| 222 | 254 | $this->processArgument($state, $argument); |
| 223 | 255 | return true; |
| 224 | 256 | } |
| 225 | 257 | |
| 226 | - if ($hasType && $parameter->allowsNull()) { |
|
| 258 | + if ($hasType && $parameter->allowsNull()) |
|
| 259 | + { |
|
| 227 | 260 | $argument = null; |
| 228 | 261 | $this->processArgument($state, $argument); |
| 229 | 262 | return true; |
| 230 | 263 | } |
| 231 | 264 | |
| 232 | - if ($error === null) { |
|
| 265 | + if ($error === null) |
|
| 266 | + { |
|
| 233 | 267 | return false; |
| 234 | 268 | } |
| 235 | 269 | |
@@ -270,12 +304,14 @@ discard block |
||
| 270 | 304 | int|string $key = null |
| 271 | 305 | ): void { |
| 272 | 306 | // Resolve Autowire objects |
| 273 | - if ($value instanceof Autowire) { |
|
| 307 | + if ($value instanceof Autowire) |
|
| 308 | + { |
|
| 274 | 309 | $value = $value->resolve($this->factory); |
| 275 | 310 | } |
| 276 | 311 | |
| 277 | 312 | // Validation |
| 278 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
| 313 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) |
|
| 314 | + { |
|
| 279 | 315 | throw new InvalidArgumentException( |
| 280 | 316 | $validateWith->getDeclaringFunction(), |
| 281 | 317 | $validateWith->getName() |
@@ -68,17 +68,20 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed |
| 70 | 70 | { |
| 71 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
| 71 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) |
|
| 72 | + { |
|
| 72 | 73 | return $this->state->singletons[$alias]; |
| 73 | 74 | } |
| 74 | 75 | |
| 75 | 76 | $binding = $this->state->bindings[$alias] ?? null; |
| 76 | 77 | |
| 77 | - if ($binding === null) { |
|
| 78 | + if ($binding === null) |
|
| 79 | + { |
|
| 78 | 80 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
| 79 | 81 | } |
| 80 | 82 | |
| 81 | - try { |
|
| 83 | + try |
|
| 84 | + { |
|
| 82 | 85 | $this->tracer->push( |
| 83 | 86 | false, |
| 84 | 87 | action: 'resolve from binding', |
@@ -106,7 +109,9 @@ discard block |
||
| 106 | 109 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
| 107 | 110 | default => $binding, |
| 108 | 111 | }; |
| 109 | - } finally { |
|
| 112 | + } |
|
| 113 | + finally |
|
| 114 | + { |
|
| 110 | 115 | $this->state->bindings[$alias] ??= $binding; |
| 111 | 116 | $this->tracer->pop(true); |
| 112 | 117 | $this->tracer->pop(false); |
@@ -120,18 +125,23 @@ discard block |
||
| 120 | 125 | private function resolveInjector(Injectable $binding, Ctx $ctx, array $arguments) |
| 121 | 126 | { |
| 122 | 127 | $context = $ctx->context; |
| 123 | - try { |
|
| 128 | + try |
|
| 129 | + { |
|
| 124 | 130 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
| 125 | - } catch (\ReflectionException $e) { |
|
| 131 | + } |
|
| 132 | + catch (\ReflectionException $e) |
|
| 133 | + { |
|
| 126 | 134 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 127 | 135 | } |
| 128 | 136 | |
| 129 | 137 | $injector = $binding->injector; |
| 130 | 138 | |
| 131 | - try { |
|
| 139 | + try |
|
| 140 | + { |
|
| 132 | 141 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
| 133 | 142 | |
| 134 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 143 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 144 | + { |
|
| 135 | 145 | throw new InjectionException( |
| 136 | 146 | \sprintf( |
| 137 | 147 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -158,7 +168,8 @@ discard block |
||
| 158 | 168 | default => (string)$context, |
| 159 | 169 | }); |
| 160 | 170 | |
| 161 | - if (!$reflection->isInstance($instance)) { |
|
| 171 | + if (!$reflection->isInstance($instance)) |
|
| 172 | + { |
|
| 162 | 173 | throw new InjectionException( |
| 163 | 174 | \sprintf( |
| 164 | 175 | "Invalid injection response for '%s'.", |
@@ -168,7 +179,9 @@ discard block |
||
| 168 | 179 | } |
| 169 | 180 | |
| 170 | 181 | return $instance; |
| 171 | - } finally { |
|
| 182 | + } |
|
| 183 | + finally |
|
| 184 | + { |
|
| 172 | 185 | $this->state->bindings[$ctx->class] ??= $binding; |
| 173 | 186 | } |
| 174 | 187 | } |
@@ -187,7 +200,8 @@ discard block |
||
| 187 | 200 | //Binding is pointing to something else |
| 188 | 201 | : $this->make($binding->alias, $arguments, $context); |
| 189 | 202 | |
| 190 | - if ($binding->singleton && $arguments === []) { |
|
| 203 | + if ($binding->singleton && $arguments === []) |
|
| 204 | + { |
|
| 191 | 205 | $this->state->singletons[$alias] = $result; |
| 192 | 206 | } |
| 193 | 207 | |
@@ -228,11 +242,14 @@ discard block |
||
| 228 | 242 | array $arguments, |
| 229 | 243 | ): mixed { |
| 230 | 244 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
| 231 | - try { |
|
| 245 | + try |
|
| 246 | + { |
|
| 232 | 247 | $instance = $binding::class === \Spiral\Core\Config\Factory::class && $binding->getParametersCount() === 0 |
| 233 | 248 | ? ($binding->factory)() |
| 234 | 249 | : $this->invoker->invoke($binding->factory, $arguments); |
| 235 | - } catch (NotCallableException $e) { |
|
| 250 | + } |
|
| 251 | + catch (NotCallableException $e) |
|
| 252 | + { |
|
| 236 | 253 | throw new ContainerException( |
| 237 | 254 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
| 238 | 255 | $e->getCode(), |
@@ -251,19 +268,24 @@ discard block |
||
| 251 | 268 | ): ?object { |
| 252 | 269 | $avoidCache = $arguments !== []; |
| 253 | 270 | |
| 254 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
| 255 | - try { |
|
| 271 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
| 272 | + { |
|
| 273 | + try |
|
| 274 | + { |
|
| 256 | 275 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 257 | 276 | |
| 258 | 277 | $object = $this->createInstance( |
| 259 | 278 | new Ctx(alias: $alias, class: $alias, context: $context), |
| 260 | 279 | $arguments, |
| 261 | 280 | ); |
| 262 | - if ($avoidCache) { |
|
| 281 | + if ($avoidCache) |
|
| 282 | + { |
|
| 263 | 283 | return $object; |
| 264 | 284 | } |
| 265 | 285 | $binding->reference = WeakReference::create($object); |
| 266 | - } catch (\Throwable) { |
|
| 286 | + } |
|
| 287 | + catch (\Throwable) |
|
| 288 | + { |
|
| 267 | 289 | throw new ContainerException( |
| 268 | 290 | $this->tracer->combineTraceMessage( |
| 269 | 291 | \sprintf( |
@@ -273,7 +295,9 @@ discard block |
||
| 273 | 295 | ) |
| 274 | 296 | ) |
| 275 | 297 | ); |
| 276 | - } finally { |
|
| 298 | + } |
|
| 299 | + finally |
|
| 300 | + { |
|
| 277 | 301 | $this->tracer->pop(); |
| 278 | 302 | } |
| 279 | 303 | } |
@@ -288,18 +312,25 @@ discard block |
||
| 288 | 312 | ): mixed { |
| 289 | 313 | $parent = $this->scope->getParent(); |
| 290 | 314 | |
| 291 | - if ($parent !== null) { |
|
| 292 | - try { |
|
| 315 | + if ($parent !== null) |
|
| 316 | + { |
|
| 317 | + try |
|
| 318 | + { |
|
| 293 | 319 | $this->tracer->push(false, ...[ |
| 294 | 320 | 'current scope' => $this->scope->getScopeName(), |
| 295 | 321 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
| 296 | 322 | ]); |
| 297 | 323 | return $parent->make($alias, $parameters, $context); |
| 298 | - } catch (BadScopeException $e) { |
|
| 299 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
| 324 | + } |
|
| 325 | + catch (BadScopeException $e) |
|
| 326 | + { |
|
| 327 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
| 328 | + { |
|
| 300 | 329 | throw $e; |
| 301 | 330 | } |
| 302 | - } catch (ContainerExceptionInterface $e) { |
|
| 331 | + } |
|
| 332 | + catch (ContainerExceptionInterface $e) |
|
| 333 | + { |
|
| 303 | 334 | $className = match (true) { |
| 304 | 335 | $e instanceof NotFoundException => NotFoundException::class, |
| 305 | 336 | default => ContainerException::class, |
@@ -308,19 +339,24 @@ discard block |
||
| 308 | 339 | 'Can\'t resolve `%s`.', |
| 309 | 340 | $alias, |
| 310 | 341 | )), previous: $e); |
| 311 | - } finally { |
|
| 342 | + } |
|
| 343 | + finally |
|
| 344 | + { |
|
| 312 | 345 | $this->tracer->pop(false); |
| 313 | 346 | } |
| 314 | 347 | } |
| 315 | 348 | |
| 316 | 349 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
| 317 | - try { |
|
| 350 | + try |
|
| 351 | + { |
|
| 318 | 352 | //No direct instructions how to construct class, make is automatically |
| 319 | 353 | return $this->autowire( |
| 320 | 354 | new Ctx(alias: $alias, class: $alias, context: $context), |
| 321 | 355 | $parameters, |
| 322 | 356 | ); |
| 323 | - } finally { |
|
| 357 | + } |
|
| 358 | + finally |
|
| 359 | + { |
|
| 324 | 360 | $this->tracer->pop(false); |
| 325 | 361 | } |
| 326 | 362 | } |
@@ -370,7 +406,8 @@ discard block |
||
| 370 | 406 | // Check scope name |
| 371 | 407 | $ctx->reflection = new \ReflectionClass($instance); |
| 372 | 408 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
| 373 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
| 409 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) |
|
| 410 | + { |
|
| 374 | 411 | throw new BadScopeException($scopeName, $instance::class); |
| 375 | 412 | } |
| 376 | 413 | |
@@ -397,24 +434,30 @@ discard block |
||
| 397 | 434 | array $arguments, |
| 398 | 435 | ): object { |
| 399 | 436 | $class = $ctx->class; |
| 400 | - try { |
|
| 437 | + try |
|
| 438 | + { |
|
| 401 | 439 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 402 | - } catch (\ReflectionException $e) { |
|
| 440 | + } |
|
| 441 | + catch (\ReflectionException $e) |
|
| 442 | + { |
|
| 403 | 443 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 404 | 444 | } |
| 405 | 445 | |
| 406 | 446 | // Check scope name |
| 407 | 447 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
| 408 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
| 448 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
| 449 | + { |
|
| 409 | 450 | throw new BadScopeException($scope, $class); |
| 410 | 451 | } |
| 411 | 452 | |
| 412 | 453 | // We have to construct class using external injector when we know the exact context |
| 413 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
| 454 | + if ($arguments === [] && $this->binder->hasInjector($class)) |
|
| 455 | + { |
|
| 414 | 456 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
| 415 | 457 | } |
| 416 | 458 | |
| 417 | - if (!$reflection->isInstantiable()) { |
|
| 459 | + if (!$reflection->isInstantiable()) |
|
| 460 | + { |
|
| 418 | 461 | $itIs = match (true) { |
| 419 | 462 | $reflection->isEnum() => 'Enum', |
| 420 | 463 | $reflection->isAbstract() => 'Abstract class', |
@@ -427,12 +470,16 @@ discard block |
||
| 427 | 470 | |
| 428 | 471 | $constructor = $reflection->getConstructor(); |
| 429 | 472 | |
| 430 | - if ($constructor !== null) { |
|
| 431 | - try { |
|
| 473 | + if ($constructor !== null) |
|
| 474 | + { |
|
| 475 | + try |
|
| 476 | + { |
|
| 432 | 477 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 433 | 478 | $this->tracer->push(true); |
| 434 | 479 | $args = $this->resolver->resolveArguments($constructor, $arguments); |
| 435 | - } catch (ValidationException $e) { |
|
| 480 | + } |
|
| 481 | + catch (ValidationException $e) |
|
| 482 | + { |
|
| 436 | 483 | throw new ContainerException( |
| 437 | 484 | $this->tracer->combineTraceMessage( |
| 438 | 485 | \sprintf( |
@@ -442,22 +489,31 @@ discard block |
||
| 442 | 489 | ) |
| 443 | 490 | ), |
| 444 | 491 | ); |
| 445 | - } finally { |
|
| 492 | + } |
|
| 493 | + finally |
|
| 494 | + { |
|
| 446 | 495 | $this->tracer->pop(true); |
| 447 | 496 | $this->tracer->pop(false); |
| 448 | 497 | } |
| 449 | - try { |
|
| 498 | + try |
|
| 499 | + { |
|
| 450 | 500 | // Using constructor with resolved arguments |
| 451 | 501 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
| 452 | 502 | $this->tracer->push(true); |
| 453 | 503 | $instance = new $class(...$args); |
| 454 | - } catch (\TypeError $e) { |
|
| 504 | + } |
|
| 505 | + catch (\TypeError $e) |
|
| 506 | + { |
|
| 455 | 507 | throw new WrongTypeException($constructor, $e); |
| 456 | - } finally { |
|
| 508 | + } |
|
| 509 | + finally |
|
| 510 | + { |
|
| 457 | 511 | $this->tracer->pop(true); |
| 458 | 512 | $this->tracer->pop(false); |
| 459 | 513 | } |
| 460 | - } else { |
|
| 514 | + } |
|
| 515 | + else |
|
| 516 | + { |
|
| 461 | 517 | // No constructor specified |
| 462 | 518 | $instance = $reflection->newInstance(); |
| 463 | 519 | } |
@@ -475,13 +531,15 @@ discard block |
||
| 475 | 531 | $instance = $this->runInflector($instance); |
| 476 | 532 | |
| 477 | 533 | //Declarative singletons |
| 478 | - if ($this->isSingleton($ctx)) { |
|
| 534 | + if ($this->isSingleton($ctx)) |
|
| 535 | + { |
|
| 479 | 536 | $this->state->singletons[$ctx->alias] = $instance; |
| 480 | 537 | } |
| 481 | 538 | |
| 482 | 539 | // Register finalizer |
| 483 | 540 | $finalizer = $this->getFinalizer($ctx, $instance); |
| 484 | - if ($finalizer !== null) { |
|
| 541 | + if ($finalizer !== null) |
|
| 542 | + { |
|
| 485 | 543 | $this->state->finalizers[] = $finalizer; |
| 486 | 544 | } |
| 487 | 545 | |
@@ -493,12 +551,14 @@ discard block |
||
| 493 | 551 | */ |
| 494 | 552 | private function isSingleton(Ctx $ctx): bool |
| 495 | 553 | { |
| 496 | - if ($ctx->singleton === true) { |
|
| 554 | + if ($ctx->singleton === true) |
|
| 555 | + { |
|
| 497 | 556 | return true; |
| 498 | 557 | } |
| 499 | 558 | |
| 500 | 559 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
| 501 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
| 560 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
| 561 | + { |
|
| 502 | 562 | return true; |
| 503 | 563 | } |
| 504 | 564 | |
@@ -512,7 +572,8 @@ discard block |
||
| 512 | 572 | * @var Finalize|null $attribute |
| 513 | 573 | */ |
| 514 | 574 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
| 515 | - if ($attribute === null) { |
|
| 575 | + if ($attribute === null) |
|
| 576 | + { |
|
| 516 | 577 | return null; |
| 517 | 578 | } |
| 518 | 579 | |
@@ -526,10 +587,14 @@ discard block |
||
| 526 | 587 | { |
| 527 | 588 | $scope = $this->scope; |
| 528 | 589 | |
| 529 | - while ($scope !== null) { |
|
| 530 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
| 531 | - if ($instance instanceof $class) { |
|
| 532 | - foreach ($inflectors as $inflector) { |
|
| 590 | + while ($scope !== null) |
|
| 591 | + { |
|
| 592 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
| 593 | + { |
|
| 594 | + if ($instance instanceof $class) |
|
| 595 | + { |
|
| 596 | + foreach ($inflectors as $inflector) |
|
| 597 | + { |
|
| 533 | 598 | $instance = $inflector->getParametersCount() > 1 |
| 534 | 599 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
| 535 | 600 | : ($inflector->inflector)($instance); |
@@ -545,9 +610,12 @@ discard block |
||
| 545 | 610 | |
| 546 | 611 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
| 547 | 612 | { |
| 548 | - try { |
|
| 613 | + try |
|
| 614 | + { |
|
| 549 | 615 | $this->resolver->validateArguments($reflection, $arguments); |
| 550 | - } catch (\Throwable) { |
|
| 616 | + } |
|
| 617 | + catch (\Throwable) |
|
| 618 | + { |
|
| 551 | 619 | return false; |
| 552 | 620 | } |
| 553 | 621 | |