@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | $this->container->bindSingleton(Bucket::class, $bucket = new Bucket('foo')); |
96 | 96 | |
97 | 97 | $result = $this->container->invoke( |
98 | - static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') { |
|
98 | + static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz'){ |
|
99 | 99 | return \compact('bucket', 'class', 'name', 'path'); |
100 | 100 | }, |
101 | 101 | ['name' => 'bar'] |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | $this->expectErrorMessage('Unable to resolve required argument `name` when resolving'); |
114 | 114 | |
115 | 115 | $this->container->invoke( |
116 | - static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') { |
|
116 | + static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz'){ |
|
117 | 117 | return \compact('bucket', 'class', 'name', 'path'); |
118 | 118 | }, |
119 | 119 | ['name' => 'bar'] |
@@ -95,7 +95,8 @@ discard block |
||
95 | 95 | $this->container->bindSingleton(Bucket::class, $bucket = new Bucket('foo')); |
96 | 96 | |
97 | 97 | $result = $this->container->invoke( |
98 | - static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') { |
|
98 | + static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') |
|
99 | + { |
|
99 | 100 | return \compact('bucket', 'class', 'name', 'path'); |
100 | 101 | }, |
101 | 102 | ['name' => 'bar'] |
@@ -113,7 +114,8 @@ discard block |
||
113 | 114 | $this->expectErrorMessage('Unable to resolve required argument `name` when resolving'); |
114 | 115 | |
115 | 116 | $this->container->invoke( |
116 | - static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') { |
|
117 | + static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') |
|
118 | + { |
|
117 | 119 | return \compact('bucket', 'class', 'name', 'path'); |
118 | 120 | }, |
119 | 121 | ['name' => 'bar'] |
@@ -38,9 +38,9 @@ discard block |
||
38 | 38 | * |
39 | 39 | * @psalm-param TResolver|object $resolver |
40 | 40 | */ |
41 | - public function bind(string $alias, string|array|callable|object $resolver): void |
|
41 | + public function bind(string $alias, string | array | callable | object $resolver): void |
|
42 | 42 | { |
43 | - if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) { |
|
43 | + if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire){ |
|
44 | 44 | // array means = execute me, false = not singleton |
45 | 45 | $this->state->bindings[$alias] = [$resolver, false]; |
46 | 46 | |
@@ -56,9 +56,9 @@ discard block |
||
56 | 56 | * |
57 | 57 | * @psalm-param TResolver|object $resolver |
58 | 58 | */ |
59 | - public function bindSingleton(string $alias, string|array|callable|object $resolver): void |
|
59 | + public function bindSingleton(string $alias, string | array | callable | object $resolver): void |
|
60 | 60 | { |
61 | - if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) { |
|
61 | + if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire){ |
|
62 | 62 | // direct binding to an instance |
63 | 63 | $this->state->bindings[$alias] = $resolver; |
64 | 64 | |
@@ -73,12 +73,12 @@ discard block |
||
73 | 73 | */ |
74 | 74 | public function hasInstance(string $alias): bool |
75 | 75 | { |
76 | - if (!$this->container->has($alias)) { |
|
76 | + if (!$this->container->has($alias)){ |
|
77 | 77 | return false; |
78 | 78 | } |
79 | 79 | $bindings = &$this->state->bindings; |
80 | 80 | |
81 | - while (isset($bindings[$alias]) && \is_string($bindings[$alias])) { |
|
81 | + while (isset($bindings[$alias]) && \is_string($bindings[$alias])){ |
|
82 | 82 | //Checking alias tree |
83 | 83 | $alias = $bindings[$alias]; |
84 | 84 | } |
@@ -111,31 +111,31 @@ discard block |
||
111 | 111 | |
112 | 112 | public function hasInjector(string $class): bool |
113 | 113 | { |
114 | - try { |
|
114 | + try{ |
|
115 | 115 | $reflection = new \ReflectionClass($class); |
116 | - } catch (\ReflectionException $e) { |
|
116 | + }catch (\ReflectionException $e){ |
|
117 | 117 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
118 | 118 | } |
119 | 119 | |
120 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
120 | + if (\array_key_exists($class, $this->state->injectors)){ |
|
121 | 121 | return $this->state->injectors[$class] !== null; |
122 | 122 | } |
123 | 123 | |
124 | 124 | if ( |
125 | 125 | $reflection->implementsInterface(InjectableInterface::class) |
126 | 126 | && $reflection->hasConstant('INJECTOR') |
127 | - ) { |
|
127 | + ){ |
|
128 | 128 | $this->state->injectors[$class] = $reflection->getConstant('INJECTOR'); |
129 | 129 | |
130 | 130 | return true; |
131 | 131 | } |
132 | 132 | |
133 | 133 | // check interfaces |
134 | - foreach ($this->state->injectors as $target => $injector) { |
|
134 | + foreach ($this->state->injectors as $target => $injector){ |
|
135 | 135 | if ( |
136 | 136 | \class_exists($target, true) |
137 | 137 | && $reflection->isSubclassOf($target) |
138 | - ) { |
|
138 | + ){ |
|
139 | 139 | $this->state->injectors[$class] = $injector; |
140 | 140 | |
141 | 141 | return true; |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | if ( |
145 | 145 | \interface_exists($target, true) |
146 | 146 | && $reflection->implementsInterface($target) |
147 | - ) { |
|
147 | + ){ |
|
148 | 148 | $this->state->injectors[$class] = $injector; |
149 | 149 | |
150 | 150 | return true; |
@@ -40,7 +40,8 @@ discard block |
||
40 | 40 | */ |
41 | 41 | public function bind(string $alias, string|array|callable|object $resolver): void |
42 | 42 | { |
43 | - if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) { |
|
43 | + if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) |
|
44 | + { |
|
44 | 45 | // array means = execute me, false = not singleton |
45 | 46 | $this->state->bindings[$alias] = [$resolver, false]; |
46 | 47 | |
@@ -58,7 +59,8 @@ discard block |
||
58 | 59 | */ |
59 | 60 | public function bindSingleton(string $alias, string|array|callable|object $resolver): void |
60 | 61 | { |
61 | - if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) { |
|
62 | + if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) |
|
63 | + { |
|
62 | 64 | // direct binding to an instance |
63 | 65 | $this->state->bindings[$alias] = $resolver; |
64 | 66 | |
@@ -73,12 +75,14 @@ discard block |
||
73 | 75 | */ |
74 | 76 | public function hasInstance(string $alias): bool |
75 | 77 | { |
76 | - if (!$this->container->has($alias)) { |
|
78 | + if (!$this->container->has($alias)) |
|
79 | + { |
|
77 | 80 | return false; |
78 | 81 | } |
79 | 82 | $bindings = &$this->state->bindings; |
80 | 83 | |
81 | - while (isset($bindings[$alias]) && \is_string($bindings[$alias])) { |
|
84 | + while (isset($bindings[$alias]) && \is_string($bindings[$alias])) |
|
85 | + { |
|
82 | 86 | //Checking alias tree |
83 | 87 | $alias = $bindings[$alias]; |
84 | 88 | } |
@@ -111,13 +115,17 @@ discard block |
||
111 | 115 | |
112 | 116 | public function hasInjector(string $class): bool |
113 | 117 | { |
114 | - try { |
|
118 | + try |
|
119 | + { |
|
115 | 120 | $reflection = new \ReflectionClass($class); |
116 | - } catch (\ReflectionException $e) { |
|
121 | + } |
|
122 | + catch (\ReflectionException $e) |
|
123 | + { |
|
117 | 124 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
118 | 125 | } |
119 | 126 | |
120 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
127 | + if (\array_key_exists($class, $this->state->injectors)) |
|
128 | + { |
|
121 | 129 | return $this->state->injectors[$class] !== null; |
122 | 130 | } |
123 | 131 | |
@@ -131,7 +139,8 @@ discard block |
||
131 | 139 | } |
132 | 140 | |
133 | 141 | // check interfaces |
134 | - foreach ($this->state->injectors as $target => $injector) { |
|
142 | + foreach ($this->state->injectors as $target => $injector) |
|
143 | + { |
|
135 | 144 | if ( |
136 | 145 | \class_exists($target, true) |
137 | 146 | && $reflection->isSubclassOf($target) |
@@ -12,14 +12,14 @@ |
||
12 | 12 | public function destruct(): void |
13 | 13 | { |
14 | 14 | $class = new \ReflectionClass($this); |
15 | - foreach ($class->getProperties() as $property) { |
|
15 | + foreach ($class->getProperties() as $property){ |
|
16 | 16 | $name = $property->getName(); |
17 | - if (!isset($this->$name)) { |
|
17 | + if (!isset($this->$name)){ |
|
18 | 18 | continue; |
19 | 19 | } |
20 | 20 | $value = $this->$name; |
21 | 21 | unset($this->$name); |
22 | - if (\is_object($value) && \method_exists($value, 'destruct')) { |
|
22 | + if (\is_object($value) && \method_exists($value, 'destruct')){ |
|
23 | 23 | $value->destruct(); |
24 | 24 | } |
25 | 25 | } |
@@ -12,14 +12,17 @@ |
||
12 | 12 | public function destruct(): void |
13 | 13 | { |
14 | 14 | $class = new \ReflectionClass($this); |
15 | - foreach ($class->getProperties() as $property) { |
|
15 | + foreach ($class->getProperties() as $property) |
|
16 | + { |
|
16 | 17 | $name = $property->getName(); |
17 | - if (!isset($this->$name)) { |
|
18 | + if (!isset($this->$name)) |
|
19 | + { |
|
18 | 20 | continue; |
19 | 21 | } |
20 | 22 | $value = $this->$name; |
21 | 23 | unset($this->$name); |
22 | - if (\is_object($value) && \method_exists($value, 'destruct')) { |
|
24 | + if (\is_object($value) && \method_exists($value, 'destruct')) |
|
25 | + { |
|
23 | 26 | $value->destruct(); |
24 | 27 | } |
25 | 28 | } |
@@ -17,7 +17,7 @@ |
||
17 | 17 | public function __construct( |
18 | 18 | private Config $config, |
19 | 19 | private array $objects = [], |
20 | - ) { |
|
20 | + ){ |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | public function set(string $name, object $value): void |
@@ -23,15 +23,15 @@ discard block |
||
23 | 23 | public function __construct( |
24 | 24 | public readonly ReflectionFunctionAbstract $reflection, |
25 | 25 | private array $arguments, |
26 | - ) { |
|
26 | + ){ |
|
27 | 27 | $this->modeNamed = $this->isNamedMode(); |
28 | 28 | } |
29 | 29 | |
30 | 30 | public function addResolvedValue(mixed &$value, string $key = null): void |
31 | 31 | { |
32 | - if ($key === null) { |
|
32 | + if ($key === null){ |
|
33 | 33 | $this->resolvedValues[] = &$value; |
34 | - } else { |
|
34 | + }else{ |
|
35 | 35 | $this->resolvedValues[$key] = &$value; |
36 | 36 | } |
37 | 37 | } |
@@ -42,25 +42,25 @@ discard block |
||
42 | 42 | ? $parameter->getName() |
43 | 43 | : $parameter->getPosition(); |
44 | 44 | |
45 | - if (!\array_key_exists($key, $this->arguments)) { |
|
45 | + if (!\array_key_exists($key, $this->arguments)){ |
|
46 | 46 | return []; |
47 | 47 | } |
48 | 48 | $_val = &$this->arguments[$key]; |
49 | 49 | |
50 | - if ($variadic && \is_array($_val)) { |
|
50 | + if ($variadic && \is_array($_val)){ |
|
51 | 51 | // Save keys is possible |
52 | 52 | $positional = true; |
53 | 53 | $result = []; |
54 | - foreach ($_val as $key => &$item) { |
|
55 | - if (!$positional && \is_int($key)) { |
|
54 | + foreach ($_val as $key => &$item){ |
|
55 | + if (!$positional && \is_int($key)){ |
|
56 | 56 | throw new ResolvingException( |
57 | 57 | 'Cannot use positional argument after named argument during unpacking named variadic argument.' |
58 | 58 | ); |
59 | 59 | } |
60 | 60 | $positional = $positional && \is_int($key); |
61 | - if ($positional) { |
|
61 | + if ($positional){ |
|
62 | 62 | $result[] = &$item; |
63 | - } else { |
|
63 | + }else{ |
|
64 | 64 | $result[$key] = &$item; |
65 | 65 | } |
66 | 66 | } |
@@ -78,10 +78,10 @@ discard block |
||
78 | 78 | { |
79 | 79 | $nums = 0; |
80 | 80 | $strings = 0; |
81 | - foreach ($this->arguments as $key => $_) { |
|
82 | - if (\is_int($key)) { |
|
81 | + foreach ($this->arguments as $key => $_){ |
|
82 | + if (\is_int($key)){ |
|
83 | 83 | ++$nums; |
84 | - } else { |
|
84 | + }else{ |
|
85 | 85 | ++$strings; |
86 | 86 | } |
87 | 87 | } |
@@ -29,9 +29,12 @@ discard block |
||
29 | 29 | |
30 | 30 | public function addResolvedValue(mixed &$value, string $key = null): void |
31 | 31 | { |
32 | - if ($key === null) { |
|
32 | + if ($key === null) |
|
33 | + { |
|
33 | 34 | $this->resolvedValues[] = &$value; |
34 | - } else { |
|
35 | + } |
|
36 | + else |
|
37 | + { |
|
35 | 38 | $this->resolvedValues[$key] = &$value; |
36 | 39 | } |
37 | 40 | } |
@@ -42,25 +45,32 @@ discard block |
||
42 | 45 | ? $parameter->getName() |
43 | 46 | : $parameter->getPosition(); |
44 | 47 | |
45 | - if (!\array_key_exists($key, $this->arguments)) { |
|
48 | + if (!\array_key_exists($key, $this->arguments)) |
|
49 | + { |
|
46 | 50 | return []; |
47 | 51 | } |
48 | 52 | $_val = &$this->arguments[$key]; |
49 | 53 | |
50 | - if ($variadic && \is_array($_val)) { |
|
54 | + if ($variadic && \is_array($_val)) |
|
55 | + { |
|
51 | 56 | // Save keys is possible |
52 | 57 | $positional = true; |
53 | 58 | $result = []; |
54 | - foreach ($_val as $key => &$item) { |
|
55 | - if (!$positional && \is_int($key)) { |
|
59 | + foreach ($_val as $key => &$item) |
|
60 | + { |
|
61 | + if (!$positional && \is_int($key)) |
|
62 | + { |
|
56 | 63 | throw new ResolvingException( |
57 | 64 | 'Cannot use positional argument after named argument during unpacking named variadic argument.' |
58 | 65 | ); |
59 | 66 | } |
60 | 67 | $positional = $positional && \is_int($key); |
61 | - if ($positional) { |
|
68 | + if ($positional) |
|
69 | + { |
|
62 | 70 | $result[] = &$item; |
63 | - } else { |
|
71 | + } |
|
72 | + else |
|
73 | + { |
|
64 | 74 | $result[$key] = &$item; |
65 | 75 | } |
66 | 76 | } |
@@ -78,10 +88,14 @@ discard block |
||
78 | 88 | { |
79 | 89 | $nums = 0; |
80 | 90 | $strings = 0; |
81 | - foreach ($this->arguments as $key => $_) { |
|
82 | - if (\is_int($key)) { |
|
91 | + foreach ($this->arguments as $key => $_) |
|
92 | + { |
|
93 | + if (\is_int($key)) |
|
94 | + { |
|
83 | 95 | ++$nums; |
84 | - } else { |
|
96 | + } |
|
97 | + else |
|
98 | + { |
|
85 | 99 | ++$strings; |
86 | 100 | } |
87 | 101 | } |
@@ -35,18 +35,18 @@ discard block |
||
35 | 35 | */ |
36 | 36 | public function invoke(mixed $target, array $parameters = []): mixed |
37 | 37 | { |
38 | - if (\is_array($target) && isset($target[1])) { |
|
38 | + if (\is_array($target) && isset($target[1])){ |
|
39 | 39 | // In a form of resolver and method |
40 | 40 | [$resolver, $method] = $target; |
41 | 41 | |
42 | 42 | // Resolver instance (i.e. [ClassName::class, 'method']) |
43 | - if (\is_string($resolver)) { |
|
43 | + if (\is_string($resolver)){ |
|
44 | 44 | $resolver = $this->container->get($resolver); |
45 | 45 | } |
46 | 46 | |
47 | - try { |
|
47 | + try{ |
|
48 | 48 | $method = new \ReflectionMethod($resolver, $method); |
49 | - } catch (\ReflectionException $e) { |
|
49 | + }catch (\ReflectionException $e){ |
|
50 | 50 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
51 | 51 | } |
52 | 52 | |
@@ -57,14 +57,14 @@ discard block |
||
57 | 57 | ); |
58 | 58 | } |
59 | 59 | |
60 | - if (\is_string($target) && \is_callable($target)) { |
|
60 | + if (\is_string($target) && \is_callable($target)){ |
|
61 | 61 | $target = $target(...); |
62 | 62 | } |
63 | 63 | |
64 | - if ($target instanceof \Closure) { |
|
65 | - try { |
|
64 | + if ($target instanceof \Closure){ |
|
65 | + try{ |
|
66 | 66 | $reflection = new \ReflectionFunction($target); |
67 | - } catch (\ReflectionException $e) { |
|
67 | + }catch (\ReflectionException $e){ |
|
68 | 68 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
69 | 69 | } |
70 | 70 |
@@ -35,18 +35,23 @@ discard block |
||
35 | 35 | */ |
36 | 36 | public function invoke(mixed $target, array $parameters = []): mixed |
37 | 37 | { |
38 | - if (\is_array($target) && isset($target[1])) { |
|
38 | + if (\is_array($target) && isset($target[1])) |
|
39 | + { |
|
39 | 40 | // In a form of resolver and method |
40 | 41 | [$resolver, $method] = $target; |
41 | 42 | |
42 | 43 | // Resolver instance (i.e. [ClassName::class, 'method']) |
43 | - if (\is_string($resolver)) { |
|
44 | + if (\is_string($resolver)) |
|
45 | + { |
|
44 | 46 | $resolver = $this->container->get($resolver); |
45 | 47 | } |
46 | 48 | |
47 | - try { |
|
49 | + try |
|
50 | + { |
|
48 | 51 | $method = new \ReflectionMethod($resolver, $method); |
49 | - } catch (\ReflectionException $e) { |
|
52 | + } |
|
53 | + catch (\ReflectionException $e) |
|
54 | + { |
|
50 | 55 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
51 | 56 | } |
52 | 57 | |
@@ -57,14 +62,19 @@ discard block |
||
57 | 62 | ); |
58 | 63 | } |
59 | 64 | |
60 | - if (\is_string($target) && \is_callable($target)) { |
|
65 | + if (\is_string($target) && \is_callable($target)) |
|
66 | + { |
|
61 | 67 | $target = $target(...); |
62 | 68 | } |
63 | 69 | |
64 | - if ($target instanceof \Closure) { |
|
65 | - try { |
|
70 | + if ($target instanceof \Closure) |
|
71 | + { |
|
72 | + try |
|
73 | + { |
|
66 | 74 | $reflection = new \ReflectionFunction($target); |
67 | - } catch (\ReflectionException $e) { |
|
75 | + } |
|
76 | + catch (\ReflectionException $e) |
|
77 | + { |
|
68 | 78 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
69 | 79 | } |
70 | 80 |
@@ -50,7 +50,8 @@ |
||
50 | 50 | */ |
51 | 51 | public function get(string|Autowire $id, string $context = null): mixed |
52 | 52 | { |
53 | - if ($id instanceof Autowire) { |
|
53 | + if ($id instanceof Autowire) |
|
54 | + { |
|
54 | 55 | return $id->resolve($this->factory); |
55 | 56 | } |
56 | 57 |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | use DestructorTrait; |
18 | 18 | |
19 | 19 | private State $state; |
20 | - private FactoryInterface|Factory $factory; |
|
20 | + private FactoryInterface | Factory $factory; |
|
21 | 21 | |
22 | 22 | public function __construct(Registry $constructor) |
23 | 23 | { |
@@ -46,9 +46,9 @@ discard block |
||
46 | 46 | * @throws ContainerException |
47 | 47 | * @throws \Throwable |
48 | 48 | */ |
49 | - public function get(string|Autowire $id, string $context = null): mixed |
|
49 | + public function get(string | Autowire $id, string $context = null): mixed |
|
50 | 50 | { |
51 | - if ($id instanceof Autowire) { |
|
51 | + if ($id instanceof Autowire){ |
|
52 | 52 | return $id->resolve($this->factory); |
53 | 53 | } |
54 | 54 |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | ): array { |
50 | 50 | $state = new ResolvingState($reflection, $parameters); |
51 | 51 | |
52 | - foreach ($reflection->getParameters() as $parameter) { |
|
52 | + foreach ($reflection->getParameters() as $parameter){ |
|
53 | 53 | $this->resolveParameter($parameter, $state, $validate) |
54 | 54 | or |
55 | 55 | throw new ArgumentResolvingException($reflection, $parameter->getName()); |
@@ -63,46 +63,46 @@ discard block |
||
63 | 63 | $positional = true; |
64 | 64 | $variadic = false; |
65 | 65 | $parameters = $reflection->getParameters(); |
66 | - if (\count($parameters) === 0) { |
|
66 | + if (\count($parameters) === 0){ |
|
67 | 67 | return; |
68 | 68 | } |
69 | 69 | |
70 | 70 | $parameter = null; |
71 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
71 | + while (\count($parameters) > 0 || \count($arguments) > 0){ |
|
72 | 72 | // get related argument value |
73 | 73 | $key = \key($arguments); |
74 | 74 | |
75 | 75 | // For a variadic parameter it's no sense - named or positional argument will be sent |
76 | 76 | // But you can't send positional argument after named in any case |
77 | - if (\is_int($key) && !$positional) { |
|
77 | + if (\is_int($key) && !$positional){ |
|
78 | 78 | throw new PositionalArgumentException($reflection, $key); |
79 | 79 | } |
80 | 80 | |
81 | 81 | $positional = $positional && \is_int($key); |
82 | 82 | |
83 | - if (!$variadic) { |
|
83 | + if (!$variadic){ |
|
84 | 84 | $parameter = \array_shift($parameters); |
85 | 85 | $variadic = $parameter?->isVariadic() ?? false; |
86 | 86 | } |
87 | 87 | |
88 | - if ($parameter === null) { |
|
88 | + if ($parameter === null){ |
|
89 | 89 | throw new UnknownParameterException($reflection, $key); |
90 | 90 | } |
91 | 91 | $name = $parameter->getName(); |
92 | 92 | |
93 | - if (($positional || $variadic) && $key !== null) { |
|
93 | + if (($positional || $variadic) && $key !== null){ |
|
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 new MissingRequiredArgumentException($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 new InvalidArgumentException($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($parameter, $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 ? $parameter : null, key: $k); |
|
184 | + if ($isVariadic){ |
|
185 | + foreach ($res as $k => &$v){ |
|
186 | + $this->processArgument($state, $v, validateWith: $validate ? $parameter : null, key : $k); |
|
187 | 187 | } |
188 | - } else { |
|
188 | + }else{ |
|
189 | 189 | $this->processArgument($state, $res[0], validateWith: $validate ? $parameter : null); |
190 | 190 | } |
191 | 191 | |
@@ -193,39 +193,39 @@ discard block |
||
193 | 193 | } |
194 | 194 | |
195 | 195 | $error = null; |
196 | - if ($hasType) { |
|
196 | + if ($hasType){ |
|
197 | 197 | /** @var ReflectionIntersectionType|ReflectionUnionType|ReflectionNamedType $reflectionType */ |
198 | 198 | $reflectionType = $parameter->getType(); |
199 | 199 | |
200 | - if ($reflectionType instanceof ReflectionIntersectionType) { |
|
200 | + if ($reflectionType instanceof ReflectionIntersectionType){ |
|
201 | 201 | throw new UnsupportedTypeException($parameter->getDeclaringFunction(), $parameter->getName()); |
202 | 202 | } |
203 | 203 | |
204 | 204 | $types = $reflectionType instanceof ReflectionNamedType ? [$reflectionType] : $reflectionType->getTypes(); |
205 | - foreach ($types as $namedType) { |
|
206 | - try { |
|
207 | - if ($this->resolveNamedType($state, $parameter, $namedType, $validate)) { |
|
205 | + foreach ($types as $namedType){ |
|
206 | + try{ |
|
207 | + if ($this->resolveNamedType($state, $parameter, $namedType, $validate)){ |
|
208 | 208 | return true; |
209 | 209 | } |
210 | - } catch (Throwable $e) { |
|
210 | + }catch (Throwable $e){ |
|
211 | 211 | $error = $e; |
212 | 212 | } |
213 | 213 | } |
214 | 214 | } |
215 | 215 | |
216 | - if ($parameter->isDefaultValueAvailable()) { |
|
216 | + if ($parameter->isDefaultValueAvailable()){ |
|
217 | 217 | $argument = $parameter->getDefaultValue(); |
218 | 218 | $this->processArgument($state, $argument); |
219 | 219 | return true; |
220 | 220 | } |
221 | 221 | |
222 | - if ($hasType && $parameter->allowsNull()) { |
|
222 | + if ($hasType && $parameter->allowsNull()){ |
|
223 | 223 | $argument = null; |
224 | 224 | $this->processArgument($state, $argument); |
225 | 225 | return true; |
226 | 226 | } |
227 | 227 | |
228 | - if ($error === null) { |
|
228 | + if ($error === null){ |
|
229 | 229 | return false; |
230 | 230 | } |
231 | 231 | |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | ReflectionParameter $parameter, |
247 | 247 | ReflectionNamedType $typeRef, |
248 | 248 | bool $validate |
249 | - ) { |
|
249 | + ){ |
|
250 | 250 | return !$typeRef->isBuiltin() && $this->resolveObjectParameter( |
251 | 251 | $state, |
252 | 252 | $typeRef->getName(), |
@@ -289,15 +289,15 @@ discard block |
||
289 | 289 | ResolvingState $state, |
290 | 290 | mixed &$value, |
291 | 291 | ReflectionParameter $validateWith = null, |
292 | - int|string $key = null |
|
292 | + int | string $key = null |
|
293 | 293 | ): void { |
294 | 294 | // Resolve Autowire objects |
295 | - if ($value instanceof Autowire) { |
|
295 | + if ($value instanceof Autowire){ |
|
296 | 296 | $value = $value->resolve($this->factory); |
297 | 297 | } |
298 | 298 | |
299 | 299 | // Validation |
300 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
300 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)){ |
|
301 | 301 | throw new InvalidArgumentException( |
302 | 302 | $validateWith->getDeclaringFunction(), |
303 | 303 | $validateWith->getName() |
@@ -49,7 +49,8 @@ discard block |
||
49 | 49 | ): array { |
50 | 50 | $state = new ResolvingState($reflection, $parameters); |
51 | 51 | |
52 | - foreach ($reflection->getParameters() as $parameter) { |
|
52 | + foreach ($reflection->getParameters() as $parameter) |
|
53 | + { |
|
53 | 54 | $this->resolveParameter($parameter, $state, $validate) |
54 | 55 | or |
55 | 56 | throw new ArgumentResolvingException($reflection, $parameter->getName()); |
@@ -63,46 +64,58 @@ discard block |
||
63 | 64 | $positional = true; |
64 | 65 | $variadic = false; |
65 | 66 | $parameters = $reflection->getParameters(); |
66 | - if (\count($parameters) === 0) { |
|
67 | + if (\count($parameters) === 0) |
|
68 | + { |
|
67 | 69 | return; |
68 | 70 | } |
69 | 71 | |
70 | 72 | $parameter = null; |
71 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
73 | + while (\count($parameters) > 0 || \count($arguments) > 0) |
|
74 | + { |
|
72 | 75 | // get related argument value |
73 | 76 | $key = \key($arguments); |
74 | 77 | |
75 | 78 | // For a variadic parameter it's no sense - named or positional argument will be sent |
76 | 79 | // But you can't send positional argument after named in any case |
77 | - if (\is_int($key) && !$positional) { |
|
80 | + if (\is_int($key) && !$positional) |
|
81 | + { |
|
78 | 82 | throw new PositionalArgumentException($reflection, $key); |
79 | 83 | } |
80 | 84 | |
81 | 85 | $positional = $positional && \is_int($key); |
82 | 86 | |
83 | - if (!$variadic) { |
|
87 | + if (!$variadic) |
|
88 | + { |
|
84 | 89 | $parameter = \array_shift($parameters); |
85 | 90 | $variadic = $parameter?->isVariadic() ?? false; |
86 | 91 | } |
87 | 92 | |
88 | - if ($parameter === null) { |
|
93 | + if ($parameter === null) |
|
94 | + { |
|
89 | 95 | throw new UnknownParameterException($reflection, $key); |
90 | 96 | } |
91 | 97 | $name = $parameter->getName(); |
92 | 98 | |
93 | - if (($positional || $variadic) && $key !== null) { |
|
99 | + if (($positional || $variadic) && $key !== null) |
|
100 | + { |
|
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 new MissingRequiredArgumentException($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 new InvalidArgumentException($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($parameter, $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 ? $parameter : null, key: $k); |
187 | 209 | } |
188 | - } else { |
|
210 | + } |
|
211 | + else |
|
212 | + { |
|
189 | 213 | $this->processArgument($state, $res[0], validateWith: $validate ? $parameter : null); |
190 | 214 | } |
191 | 215 | |
@@ -193,39 +217,49 @@ discard block |
||
193 | 217 | } |
194 | 218 | |
195 | 219 | $error = null; |
196 | - if ($hasType) { |
|
220 | + if ($hasType) |
|
221 | + { |
|
197 | 222 | /** @var ReflectionIntersectionType|ReflectionUnionType|ReflectionNamedType $reflectionType */ |
198 | 223 | $reflectionType = $parameter->getType(); |
199 | 224 | |
200 | - if ($reflectionType instanceof ReflectionIntersectionType) { |
|
225 | + if ($reflectionType instanceof ReflectionIntersectionType) |
|
226 | + { |
|
201 | 227 | throw new UnsupportedTypeException($parameter->getDeclaringFunction(), $parameter->getName()); |
202 | 228 | } |
203 | 229 | |
204 | 230 | $types = $reflectionType instanceof ReflectionNamedType ? [$reflectionType] : $reflectionType->getTypes(); |
205 | - foreach ($types as $namedType) { |
|
206 | - try { |
|
207 | - if ($this->resolveNamedType($state, $parameter, $namedType, $validate)) { |
|
231 | + foreach ($types as $namedType) |
|
232 | + { |
|
233 | + try |
|
234 | + { |
|
235 | + if ($this->resolveNamedType($state, $parameter, $namedType, $validate)) |
|
236 | + { |
|
208 | 237 | return true; |
209 | 238 | } |
210 | - } catch (Throwable $e) { |
|
239 | + } |
|
240 | + catch (Throwable $e) |
|
241 | + { |
|
211 | 242 | $error = $e; |
212 | 243 | } |
213 | 244 | } |
214 | 245 | } |
215 | 246 | |
216 | - if ($parameter->isDefaultValueAvailable()) { |
|
247 | + if ($parameter->isDefaultValueAvailable()) |
|
248 | + { |
|
217 | 249 | $argument = $parameter->getDefaultValue(); |
218 | 250 | $this->processArgument($state, $argument); |
219 | 251 | return true; |
220 | 252 | } |
221 | 253 | |
222 | - if ($hasType && $parameter->allowsNull()) { |
|
254 | + if ($hasType && $parameter->allowsNull()) |
|
255 | + { |
|
223 | 256 | $argument = null; |
224 | 257 | $this->processArgument($state, $argument); |
225 | 258 | return true; |
226 | 259 | } |
227 | 260 | |
228 | - if ($error === null) { |
|
261 | + if ($error === null) |
|
262 | + { |
|
229 | 263 | return false; |
230 | 264 | } |
231 | 265 | |
@@ -292,12 +326,14 @@ discard block |
||
292 | 326 | int|string $key = null |
293 | 327 | ): void { |
294 | 328 | // Resolve Autowire objects |
295 | - if ($value instanceof Autowire) { |
|
329 | + if ($value instanceof Autowire) |
|
330 | + { |
|
296 | 331 | $value = $value->resolve($this->factory); |
297 | 332 | } |
298 | 333 | |
299 | 334 | // Validation |
300 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
335 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) |
|
336 | + { |
|
301 | 337 | throw new InvalidArgumentException( |
302 | 338 | $validateWith->getDeclaringFunction(), |
303 | 339 | $validateWith->getName() |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | public function __construct( |
27 | 27 | private readonly string $alias, |
28 | 28 | private readonly array $parameters = [] |
29 | - ) { |
|
29 | + ){ |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | public static function __set_state(array $anArray): static |
@@ -41,22 +41,22 @@ discard block |
||
41 | 41 | */ |
42 | 42 | public static function wire(mixed $definition): Autowire |
43 | 43 | { |
44 | - if ($definition instanceof self) { |
|
44 | + if ($definition instanceof self){ |
|
45 | 45 | return $definition; |
46 | 46 | } |
47 | 47 | |
48 | - if (\is_string($definition)) { |
|
48 | + if (\is_string($definition)){ |
|
49 | 49 | return new self($definition); |
50 | 50 | } |
51 | 51 | |
52 | - if (\is_array($definition) && isset($definition['class'])) { |
|
52 | + if (\is_array($definition) && isset($definition['class'])){ |
|
53 | 53 | return new self( |
54 | 54 | $definition['class'], |
55 | 55 | $definition['options'] ?? $definition['params'] ?? [] |
56 | 56 | ); |
57 | 57 | } |
58 | 58 | |
59 | - if (\is_object($definition)) { |
|
59 | + if (\is_object($definition)){ |
|
60 | 60 | $autowire = new self($definition::class, []); |
61 | 61 | $autowire->target = $definition; |
62 | 62 | return $autowire; |
@@ -41,22 +41,26 @@ |
||
41 | 41 | */ |
42 | 42 | public static function wire(mixed $definition): Autowire |
43 | 43 | { |
44 | - if ($definition instanceof self) { |
|
44 | + if ($definition instanceof self) |
|
45 | + { |
|
45 | 46 | return $definition; |
46 | 47 | } |
47 | 48 | |
48 | - if (\is_string($definition)) { |
|
49 | + if (\is_string($definition)) |
|
50 | + { |
|
49 | 51 | return new self($definition); |
50 | 52 | } |
51 | 53 | |
52 | - if (\is_array($definition) && isset($definition['class'])) { |
|
54 | + if (\is_array($definition) && isset($definition['class'])) |
|
55 | + { |
|
53 | 56 | return new self( |
54 | 57 | $definition['class'], |
55 | 58 | $definition['options'] ?? $definition['params'] ?? [] |
56 | 59 | ); |
57 | 60 | } |
58 | 61 | |
59 | - if (\is_object($definition)) { |
|
62 | + if (\is_object($definition)) |
|
63 | + { |
|
60 | 64 | $autowire = new self($definition::class, []); |
61 | 65 | $autowire->target = $definition; |
62 | 66 | return $autowire; |