@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | */ |
| 25 | 25 | protected function iterableToArray(iterable $iterable): array |
| 26 | 26 | { |
| 27 | - if ($iterable instanceof \Traversable) { |
|
| 27 | + if ($iterable instanceof \Traversable){ |
|
| 28 | 28 | return \iterator_to_array($iterable, false); |
| 29 | 29 | } |
| 30 | 30 | |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | { |
| 50 | 50 | $instance = new $class(); |
| 51 | 51 | |
| 52 | - foreach ($fields as $field => $value) { |
|
| 52 | + foreach ($fields as $field => $value){ |
|
| 53 | 53 | $instance->$field = $value; |
| 54 | 54 | } |
| 55 | 55 | |
@@ -65,12 +65,12 @@ discard block |
||
| 65 | 65 | * @param string $message |
| 66 | 66 | * Expected message. |
| 67 | 67 | */ |
| 68 | - public function expectExceptionMessageEquals(string $message) { |
|
| 68 | + public function expectExceptionMessageEquals(string $message){ |
|
| 69 | 69 | // The "contains" check produces a more readable message. |
| 70 | 70 | $this->expectExceptionMessage($message); |
| 71 | 71 | // The regex check is the only way to check exact equality. |
| 72 | 72 | $this->expectExceptionMessageMatches( |
| 73 | - '@^' . preg_quote($message, '@') . '$@' |
|
| 73 | + '@^'.preg_quote($message, '@').'$@' |
|
| 74 | 74 | ); |
| 75 | 75 | } |
| 76 | 76 | |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | * @throws \ReflectionException |
| 88 | 88 | * The class does not exist. |
| 89 | 89 | */ |
| 90 | - public static function assertExceptionSource(\Throwable $e, string $class, string $search) { |
|
| 90 | + public static function assertExceptionSource(\Throwable $e, string $class, string $search){ |
|
| 91 | 91 | $rc = new \ReflectionClass($class); |
| 92 | 92 | $file = $rc->getFileName(); |
| 93 | 93 | self::assertSame($file, $e->getFile(), 'Exception file'); |
@@ -24,7 +24,8 @@ discard block |
||
| 24 | 24 | */ |
| 25 | 25 | protected function iterableToArray(iterable $iterable): array |
| 26 | 26 | { |
| 27 | - if ($iterable instanceof \Traversable) { |
|
| 27 | + if ($iterable instanceof \Traversable) |
|
| 28 | + { |
|
| 28 | 29 | return \iterator_to_array($iterable, false); |
| 29 | 30 | } |
| 30 | 31 | |
@@ -49,7 +50,8 @@ discard block |
||
| 49 | 50 | { |
| 50 | 51 | $instance = new $class(); |
| 51 | 52 | |
| 52 | - foreach ($fields as $field => $value) { |
|
| 53 | + foreach ($fields as $field => $value) |
|
| 54 | + { |
|
| 53 | 55 | $instance->$field = $value; |
| 54 | 56 | } |
| 55 | 57 | |
@@ -65,7 +67,8 @@ discard block |
||
| 65 | 67 | * @param string $message |
| 66 | 68 | * Expected message. |
| 67 | 69 | */ |
| 68 | - public function expectExceptionMessageEquals(string $message) { |
|
| 70 | + public function expectExceptionMessageEquals(string $message) |
|
| 71 | + { |
|
| 69 | 72 | // The "contains" check produces a more readable message. |
| 70 | 73 | $this->expectExceptionMessage($message); |
| 71 | 74 | // The regex check is the only way to check exact equality. |
@@ -87,7 +90,8 @@ discard block |
||
| 87 | 90 | * @throws \ReflectionException |
| 88 | 91 | * The class does not exist. |
| 89 | 92 | */ |
| 90 | - public static function assertExceptionSource(\Throwable $e, string $class, string $search) { |
|
| 93 | + public static function assertExceptionSource(\Throwable $e, string $class, string $search) |
|
| 94 | + { |
|
| 91 | 95 | $rc = new \ReflectionClass($class); |
| 92 | 96 | $file = $rc->getFileName(); |
| 93 | 97 | self::assertSame($file, $e->getFile(), 'Exception file'); |
@@ -73,23 +73,23 @@ discard block |
||
| 73 | 73 | |
| 74 | 74 | public function testUnknownSequentialAfterNamed() |
| 75 | 75 | { |
| 76 | - if (PHP_VERSION_ID < 80000) { |
|
| 76 | + if (PHP_VERSION_ID < 80000){ |
|
| 77 | 77 | $this->expectException(\BadMethodCallException::class); |
| 78 | - } else { |
|
| 78 | + }else{ |
|
| 79 | 79 | $this->expectException(\Error::class); |
| 80 | 80 | } |
| 81 | 81 | /* @see NamedArgumentsInstantiator::ERROR_POSITIONAL_AFTER_NAMED */ |
| 82 | 82 | $this->expectExceptionMessageEquals('Cannot use positional argument after named argument'); |
| 83 | 83 | |
| 84 | - try { |
|
| 84 | + try{ |
|
| 85 | 85 | $this->new($class = NamedArgumentConstructorFixture::class, [ |
| 86 | 86 | 'a' => 'A', |
| 87 | 87 | 5 => 'five', |
| 88 | 88 | ]); |
| 89 | - } catch (\Throwable $e) { |
|
| 90 | - if (PHP_VERSION_ID < 80000) { |
|
| 89 | + }catch (\Throwable $e){ |
|
| 90 | + if (PHP_VERSION_ID < 80000){ |
|
| 91 | 91 | self::assertExceptionSource($e, $class, 'function __construct'); |
| 92 | - } else { |
|
| 92 | + }else{ |
|
| 93 | 93 | self::assertExceptionSource($e, $class, 'class '); |
| 94 | 94 | } |
| 95 | 95 | throw $e; |
@@ -98,9 +98,9 @@ discard block |
||
| 98 | 98 | |
| 99 | 99 | public function testKnownSequentialAfterNamed() |
| 100 | 100 | { |
| 101 | - if (PHP_VERSION_ID < 80000) { |
|
| 101 | + if (PHP_VERSION_ID < 80000){ |
|
| 102 | 102 | $this->expectException(\BadMethodCallException::class); |
| 103 | - } else { |
|
| 103 | + }else{ |
|
| 104 | 104 | $this->expectException(\Error::class); |
| 105 | 105 | } |
| 106 | 106 | /* @see NamedArgumentsInstantiator::ERROR_POSITIONAL_AFTER_NAMED */ |
@@ -123,15 +123,15 @@ discard block |
||
| 123 | 123 | ) |
| 124 | 124 | ); |
| 125 | 125 | |
| 126 | - try { |
|
| 126 | + try{ |
|
| 127 | 127 | $this->new($class = NamedRequiredArgumentConstructorFixture::class, [ |
| 128 | 128 | 'a', |
| 129 | 129 | 'c' => 'C', |
| 130 | 130 | ]); |
| 131 | - } catch (\Throwable $e) { |
|
| 132 | - if (PHP_VERSION_ID < 80000) { |
|
| 131 | + }catch (\Throwable $e){ |
|
| 132 | + if (PHP_VERSION_ID < 80000){ |
|
| 133 | 133 | self::assertExceptionSource($e, $class, 'function __construct'); |
| 134 | - } else { |
|
| 134 | + }else{ |
|
| 135 | 135 | self::assertExceptionSource($e, $class, 'class '); |
| 136 | 136 | } |
| 137 | 137 | throw $e; |
@@ -140,22 +140,22 @@ discard block |
||
| 140 | 140 | |
| 141 | 141 | public function testUnknownArg() |
| 142 | 142 | { |
| 143 | - if (PHP_VERSION_ID < 80000) { |
|
| 143 | + if (PHP_VERSION_ID < 80000){ |
|
| 144 | 144 | $this->expectException(\BadMethodCallException::class); |
| 145 | - } else { |
|
| 145 | + }else{ |
|
| 146 | 146 | $this->expectException(\Error::class); |
| 147 | 147 | } |
| 148 | 148 | /* @see NamedArgumentsInstantiator::ERROR_UNKNOWN_ARGUMENT */ |
| 149 | 149 | $this->expectExceptionMessageEquals('Unknown named parameter $d'); |
| 150 | 150 | |
| 151 | - try { |
|
| 151 | + try{ |
|
| 152 | 152 | $this->new($class = NamedArgumentConstructorFixture::class, [ |
| 153 | 153 | 'd' => 'D', |
| 154 | 154 | ]); |
| 155 | - } catch (\Throwable $e) { |
|
| 156 | - if (PHP_VERSION_ID < 80000) { |
|
| 155 | + }catch (\Throwable $e){ |
|
| 156 | + if (PHP_VERSION_ID < 80000){ |
|
| 157 | 157 | self::assertExceptionSource($e, $class, 'function __construct'); |
| 158 | - } else { |
|
| 158 | + }else{ |
|
| 159 | 159 | self::assertExceptionSource($e, $class, 'class '); |
| 160 | 160 | } |
| 161 | 161 | throw $e; |
@@ -164,9 +164,9 @@ discard block |
||
| 164 | 164 | |
| 165 | 165 | public function testOverwriteArg() |
| 166 | 166 | { |
| 167 | - if (PHP_VERSION_ID < 80000) { |
|
| 167 | + if (PHP_VERSION_ID < 80000){ |
|
| 168 | 168 | $this->expectException(\BadMethodCallException::class); |
| 169 | - } else { |
|
| 169 | + }else{ |
|
| 170 | 170 | $this->expectException(\Error::class); |
| 171 | 171 | } |
| 172 | 172 | /* @see NamedArgumentsInstantiator::ERROR_OVERWRITE_ARGUMENT */ |
@@ -195,9 +195,9 @@ discard block |
||
| 195 | 195 | |
| 196 | 196 | public function testVariadicPositionalAfterNamed() |
| 197 | 197 | { |
| 198 | - if (PHP_VERSION_ID < 80000) { |
|
| 198 | + if (PHP_VERSION_ID < 80000){ |
|
| 199 | 199 | $this->expectException(\BadMethodCallException::class); |
| 200 | - } else { |
|
| 200 | + }else{ |
|
| 201 | 201 | $this->expectException(\Error::class); |
| 202 | 202 | } |
| 203 | 203 | /* @see NamedArgumentsInstantiator::ERROR_POSITIONAL_AFTER_NAMED */ |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | |
| 214 | 214 | public function testVariadicMixed() |
| 215 | 215 | { |
| 216 | - if (PHP_VERSION_ID < 80000) { |
|
| 216 | + if (PHP_VERSION_ID < 80000){ |
|
| 217 | 217 | $this->expectException(\BadMethodCallException::class); |
| 218 | 218 | /* @see NamedArgumentsInstantiator::ERROR_NAMED_ARG_TO_VARIADIC */ |
| 219 | 219 | $this->expectExceptionMessageEquals('Cannot pass named argument $x to variadic parameter ...$args in PHP < 8'); |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | |
| 235 | 235 | public function testVariadicNamed() |
| 236 | 236 | { |
| 237 | - if (PHP_VERSION_ID < 80000) { |
|
| 237 | + if (PHP_VERSION_ID < 80000){ |
|
| 238 | 238 | $this->expectException(\BadMethodCallException::class); |
| 239 | 239 | /* @see NamedArgumentsInstantiator::ERROR_NAMED_ARG_TO_VARIADIC */ |
| 240 | 240 | $this->expectExceptionMessageEquals('Cannot pass named argument $x to variadic parameter ...$args in PHP < 8'); |
@@ -73,23 +73,32 @@ discard block |
||
| 73 | 73 | |
| 74 | 74 | public function testUnknownSequentialAfterNamed() |
| 75 | 75 | { |
| 76 | - if (PHP_VERSION_ID < 80000) { |
|
| 76 | + if (PHP_VERSION_ID < 80000) |
|
| 77 | + { |
|
| 77 | 78 | $this->expectException(\BadMethodCallException::class); |
| 78 | - } else { |
|
| 79 | + } |
|
| 80 | + else |
|
| 81 | + { |
|
| 79 | 82 | $this->expectException(\Error::class); |
| 80 | 83 | } |
| 81 | 84 | /* @see NamedArgumentsInstantiator::ERROR_POSITIONAL_AFTER_NAMED */ |
| 82 | 85 | $this->expectExceptionMessageEquals('Cannot use positional argument after named argument'); |
| 83 | 86 | |
| 84 | - try { |
|
| 87 | + try |
|
| 88 | + { |
|
| 85 | 89 | $this->new($class = NamedArgumentConstructorFixture::class, [ |
| 86 | 90 | 'a' => 'A', |
| 87 | 91 | 5 => 'five', |
| 88 | 92 | ]); |
| 89 | - } catch (\Throwable $e) { |
|
| 90 | - if (PHP_VERSION_ID < 80000) { |
|
| 93 | + } |
|
| 94 | + catch (\Throwable $e) |
|
| 95 | + { |
|
| 96 | + if (PHP_VERSION_ID < 80000) |
|
| 97 | + { |
|
| 91 | 98 | self::assertExceptionSource($e, $class, 'function __construct'); |
| 92 | - } else { |
|
| 99 | + } |
|
| 100 | + else |
|
| 101 | + { |
|
| 93 | 102 | self::assertExceptionSource($e, $class, 'class '); |
| 94 | 103 | } |
| 95 | 104 | throw $e; |
@@ -98,9 +107,12 @@ discard block |
||
| 98 | 107 | |
| 99 | 108 | public function testKnownSequentialAfterNamed() |
| 100 | 109 | { |
| 101 | - if (PHP_VERSION_ID < 80000) { |
|
| 110 | + if (PHP_VERSION_ID < 80000) |
|
| 111 | + { |
|
| 102 | 112 | $this->expectException(\BadMethodCallException::class); |
| 103 | - } else { |
|
| 113 | + } |
|
| 114 | + else |
|
| 115 | + { |
|
| 104 | 116 | $this->expectException(\Error::class); |
| 105 | 117 | } |
| 106 | 118 | /* @see NamedArgumentsInstantiator::ERROR_POSITIONAL_AFTER_NAMED */ |
@@ -123,15 +135,21 @@ discard block |
||
| 123 | 135 | ) |
| 124 | 136 | ); |
| 125 | 137 | |
| 126 | - try { |
|
| 138 | + try |
|
| 139 | + { |
|
| 127 | 140 | $this->new($class = NamedRequiredArgumentConstructorFixture::class, [ |
| 128 | 141 | 'a', |
| 129 | 142 | 'c' => 'C', |
| 130 | 143 | ]); |
| 131 | - } catch (\Throwable $e) { |
|
| 132 | - if (PHP_VERSION_ID < 80000) { |
|
| 144 | + } |
|
| 145 | + catch (\Throwable $e) |
|
| 146 | + { |
|
| 147 | + if (PHP_VERSION_ID < 80000) |
|
| 148 | + { |
|
| 133 | 149 | self::assertExceptionSource($e, $class, 'function __construct'); |
| 134 | - } else { |
|
| 150 | + } |
|
| 151 | + else |
|
| 152 | + { |
|
| 135 | 153 | self::assertExceptionSource($e, $class, 'class '); |
| 136 | 154 | } |
| 137 | 155 | throw $e; |
@@ -140,22 +158,31 @@ discard block |
||
| 140 | 158 | |
| 141 | 159 | public function testUnknownArg() |
| 142 | 160 | { |
| 143 | - if (PHP_VERSION_ID < 80000) { |
|
| 161 | + if (PHP_VERSION_ID < 80000) |
|
| 162 | + { |
|
| 144 | 163 | $this->expectException(\BadMethodCallException::class); |
| 145 | - } else { |
|
| 164 | + } |
|
| 165 | + else |
|
| 166 | + { |
|
| 146 | 167 | $this->expectException(\Error::class); |
| 147 | 168 | } |
| 148 | 169 | /* @see NamedArgumentsInstantiator::ERROR_UNKNOWN_ARGUMENT */ |
| 149 | 170 | $this->expectExceptionMessageEquals('Unknown named parameter $d'); |
| 150 | 171 | |
| 151 | - try { |
|
| 172 | + try |
|
| 173 | + { |
|
| 152 | 174 | $this->new($class = NamedArgumentConstructorFixture::class, [ |
| 153 | 175 | 'd' => 'D', |
| 154 | 176 | ]); |
| 155 | - } catch (\Throwable $e) { |
|
| 156 | - if (PHP_VERSION_ID < 80000) { |
|
| 177 | + } |
|
| 178 | + catch (\Throwable $e) |
|
| 179 | + { |
|
| 180 | + if (PHP_VERSION_ID < 80000) |
|
| 181 | + { |
|
| 157 | 182 | self::assertExceptionSource($e, $class, 'function __construct'); |
| 158 | - } else { |
|
| 183 | + } |
|
| 184 | + else |
|
| 185 | + { |
|
| 159 | 186 | self::assertExceptionSource($e, $class, 'class '); |
| 160 | 187 | } |
| 161 | 188 | throw $e; |
@@ -164,9 +191,12 @@ discard block |
||
| 164 | 191 | |
| 165 | 192 | public function testOverwriteArg() |
| 166 | 193 | { |
| 167 | - if (PHP_VERSION_ID < 80000) { |
|
| 194 | + if (PHP_VERSION_ID < 80000) |
|
| 195 | + { |
|
| 168 | 196 | $this->expectException(\BadMethodCallException::class); |
| 169 | - } else { |
|
| 197 | + } |
|
| 198 | + else |
|
| 199 | + { |
|
| 170 | 200 | $this->expectException(\Error::class); |
| 171 | 201 | } |
| 172 | 202 | /* @see NamedArgumentsInstantiator::ERROR_OVERWRITE_ARGUMENT */ |
@@ -195,9 +225,12 @@ discard block |
||
| 195 | 225 | |
| 196 | 226 | public function testVariadicPositionalAfterNamed() |
| 197 | 227 | { |
| 198 | - if (PHP_VERSION_ID < 80000) { |
|
| 228 | + if (PHP_VERSION_ID < 80000) |
|
| 229 | + { |
|
| 199 | 230 | $this->expectException(\BadMethodCallException::class); |
| 200 | - } else { |
|
| 231 | + } |
|
| 232 | + else |
|
| 233 | + { |
|
| 201 | 234 | $this->expectException(\Error::class); |
| 202 | 235 | } |
| 203 | 236 | /* @see NamedArgumentsInstantiator::ERROR_POSITIONAL_AFTER_NAMED */ |
@@ -213,7 +246,8 @@ discard block |
||
| 213 | 246 | |
| 214 | 247 | public function testVariadicMixed() |
| 215 | 248 | { |
| 216 | - if (PHP_VERSION_ID < 80000) { |
|
| 249 | + if (PHP_VERSION_ID < 80000) |
|
| 250 | + { |
|
| 217 | 251 | $this->expectException(\BadMethodCallException::class); |
| 218 | 252 | /* @see NamedArgumentsInstantiator::ERROR_NAMED_ARG_TO_VARIADIC */ |
| 219 | 253 | $this->expectExceptionMessageEquals('Cannot pass named argument $x to variadic parameter ...$args in PHP < 8'); |
@@ -234,7 +268,8 @@ discard block |
||
| 234 | 268 | |
| 235 | 269 | public function testVariadicNamed() |
| 236 | 270 | { |
| 237 | - if (PHP_VERSION_ID < 80000) { |
|
| 271 | + if (PHP_VERSION_ID < 80000) |
|
| 272 | + { |
|
| 238 | 273 | $this->expectException(\BadMethodCallException::class); |
| 239 | 274 | /* @see NamedArgumentsInstantiator::ERROR_NAMED_ARG_TO_VARIADIC */ |
| 240 | 275 | $this->expectExceptionMessageEquals('Cannot pass named argument $x to variadic parameter ...$args in PHP < 8'); |
@@ -49,17 +49,17 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | public function instantiate(\ReflectionClass $attr, array $arguments, \Reflector $context = null): object |
| 51 | 51 | { |
| 52 | - if ($this->isNamedArgumentsSupported()) { |
|
| 53 | - try { |
|
| 52 | + if ($this->isNamedArgumentsSupported()){ |
|
| 53 | + try{ |
|
| 54 | 54 | return $attr->newInstanceArgs($arguments); |
| 55 | - } catch (\Throwable $e) { |
|
| 55 | + }catch (\Throwable $e){ |
|
| 56 | 56 | throw Exception::withLocation($e, $attr->getFileName(), $attr->getStartLine()); |
| 57 | 57 | } |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | $constructor = $this->getConstructor($attr); |
| 61 | 61 | |
| 62 | - if ($constructor === null) { |
|
| 62 | + if ($constructor === null){ |
|
| 63 | 63 | return $attr->newInstanceWithoutConstructor(); |
| 64 | 64 | } |
| 65 | 65 | |
@@ -78,9 +78,9 @@ discard block |
||
| 78 | 78 | */ |
| 79 | 79 | private function resolveParameters(\ReflectionClass $ctx, \ReflectionMethod $constructor, array $arguments): array |
| 80 | 80 | { |
| 81 | - try { |
|
| 81 | + try{ |
|
| 82 | 82 | return $this->doResolveParameters($ctx, $constructor, $arguments); |
| 83 | - } catch (\Throwable $e) { |
|
| 83 | + }catch (\Throwable $e){ |
|
| 84 | 84 | throw Exception::withLocation($e, $constructor->getFileName(), $constructor->getStartLine()); |
| 85 | 85 | } |
| 86 | 86 | } |
@@ -93,16 +93,16 @@ discard block |
||
| 93 | 93 | { |
| 94 | 94 | $namedArgsBegin = $this->analyzeKeys($arguments); |
| 95 | 95 | |
| 96 | - if ($namedArgsBegin === null) { |
|
| 96 | + if ($namedArgsBegin === null){ |
|
| 97 | 97 | // Only numeric / positional keys exist. |
| 98 | 98 | return $arguments; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - if ($namedArgsBegin === 0) { |
|
| 101 | + if ($namedArgsBegin === 0){ |
|
| 102 | 102 | // Only named keys exist. |
| 103 | 103 | $passed = []; |
| 104 | 104 | $named = $arguments; |
| 105 | - } else { |
|
| 105 | + }else{ |
|
| 106 | 106 | // Numeric/positional keys followed by named keys. |
| 107 | 107 | // No need to preserve numeric keys. |
| 108 | 108 | $passed = array_slice($arguments, 0, $namedArgsBegin); |
@@ -138,11 +138,11 @@ discard block |
||
| 138 | 138 | $arguments = array_merge($arguments); |
| 139 | 139 | |
| 140 | 140 | $i = 0; |
| 141 | - foreach ($arguments as $k => $_) { |
|
| 142 | - if ($k !== $i) { |
|
| 141 | + foreach ($arguments as $k => $_){ |
|
| 142 | + if ($k !== $i){ |
|
| 143 | 143 | // This must be a string key. |
| 144 | 144 | // Any further numeric keys are illegal. |
| 145 | - if (\array_key_exists($i, $arguments)) { |
|
| 145 | + if (\array_key_exists($i, $arguments)){ |
|
| 146 | 146 | throw new \BadMethodCallException(self::ERROR_POSITIONAL_AFTER_NAMED); |
| 147 | 147 | } |
| 148 | 148 | return $i; |
@@ -179,25 +179,25 @@ discard block |
||
| 179 | 179 | { |
| 180 | 180 | // Analyze parameters. |
| 181 | 181 | $n = count($parameters); |
| 182 | - if ($n > 0 && end($parameters)->isVariadic()) { |
|
| 182 | + if ($n > 0 && end($parameters)->isVariadic()){ |
|
| 183 | 183 | $variadicParameter = end($parameters); |
| 184 | 184 | // Don't include the variadic parameter in the mapping process. |
| 185 | 185 | --$n; |
| 186 | - } else { |
|
| 186 | + }else{ |
|
| 187 | 187 | $variadicParameter = null; |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | // Process parameters that are not already filled with positional args. |
| 191 | 191 | // This loop will do nothing if $namedArgsBegin >= $n. That's ok. |
| 192 | - for ($i = $namedArgsBegin; $i < $n; ++$i) { |
|
| 192 | + for ($i = $namedArgsBegin; $i < $n; ++$i){ |
|
| 193 | 193 | $parameter = $parameters[$i]; |
| 194 | 194 | $k = $parameter->getName(); |
| 195 | - if (array_key_exists($k, $named)) { |
|
| 195 | + if (array_key_exists($k, $named)){ |
|
| 196 | 196 | $passed[] = $named[$k]; |
| 197 | 197 | unset($named[$k]); |
| 198 | - } elseif ($parameter->isDefaultValueAvailable()) { |
|
| 198 | + } elseif ($parameter->isDefaultValueAvailable()){ |
|
| 199 | 199 | $passed[] = $parameter->getDefaultValue(); |
| 200 | - } else { |
|
| 200 | + }else{ |
|
| 201 | 201 | $message = \vsprintf(self::ERROR_ARGUMENT_NOT_PASSED, [ |
| 202 | 202 | $ctx->getName(), |
| 203 | 203 | $parameter->getPosition() + 1, |
@@ -208,7 +208,7 @@ discard block |
||
| 208 | 208 | } |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | - if ($named === []) { |
|
| 211 | + if ($named === []){ |
|
| 212 | 212 | // No unknown argument names exist. |
| 213 | 213 | return $passed; |
| 214 | 214 | } |
@@ -218,11 +218,11 @@ discard block |
||
| 218 | 218 | $badArgName = key($named); |
| 219 | 219 | |
| 220 | 220 | // Check collision with positional arguments. |
| 221 | - foreach ($parameters as $i => $parameter) { |
|
| 222 | - if ($i >= $namedArgsBegin) { |
|
| 221 | + foreach ($parameters as $i => $parameter){ |
|
| 222 | + if ($i >= $namedArgsBegin){ |
|
| 223 | 223 | break; |
| 224 | 224 | } |
| 225 | - if ($parameter->getName() === $badArgName) { |
|
| 225 | + if ($parameter->getName() === $badArgName){ |
|
| 226 | 226 | // The named argument overwrites a positional argument. |
| 227 | 227 | $message = \sprintf(self::ERROR_OVERWRITE_ARGUMENT, $badArgName); |
| 228 | 228 | throw new \BadMethodCallException($message); |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | // Special handling if a variadic parameter is present. |
| 233 | - if ($variadicParameter !== null) { |
|
| 233 | + if ($variadicParameter !== null){ |
|
| 234 | 234 | // The last parameter is variadic. |
| 235 | 235 | // Since PHP 8+, variadic parameters can consume named arguments. |
| 236 | 236 | // However, this code only runs if PHP < 8. |
@@ -49,17 +49,22 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | public function instantiate(\ReflectionClass $attr, array $arguments, \Reflector $context = null): object |
| 51 | 51 | { |
| 52 | - if ($this->isNamedArgumentsSupported()) { |
|
| 53 | - try { |
|
| 52 | + if ($this->isNamedArgumentsSupported()) |
|
| 53 | + { |
|
| 54 | + try |
|
| 55 | + { |
|
| 54 | 56 | return $attr->newInstanceArgs($arguments); |
| 55 | - } catch (\Throwable $e) { |
|
| 57 | + } |
|
| 58 | + catch (\Throwable $e) |
|
| 59 | + { |
|
| 56 | 60 | throw Exception::withLocation($e, $attr->getFileName(), $attr->getStartLine()); |
| 57 | 61 | } |
| 58 | 62 | } |
| 59 | 63 | |
| 60 | 64 | $constructor = $this->getConstructor($attr); |
| 61 | 65 | |
| 62 | - if ($constructor === null) { |
|
| 66 | + if ($constructor === null) |
|
| 67 | + { |
|
| 63 | 68 | return $attr->newInstanceWithoutConstructor(); |
| 64 | 69 | } |
| 65 | 70 | |
@@ -78,9 +83,12 @@ discard block |
||
| 78 | 83 | */ |
| 79 | 84 | private function resolveParameters(\ReflectionClass $ctx, \ReflectionMethod $constructor, array $arguments): array |
| 80 | 85 | { |
| 81 | - try { |
|
| 86 | + try |
|
| 87 | + { |
|
| 82 | 88 | return $this->doResolveParameters($ctx, $constructor, $arguments); |
| 83 | - } catch (\Throwable $e) { |
|
| 89 | + } |
|
| 90 | + catch (\Throwable $e) |
|
| 91 | + { |
|
| 84 | 92 | throw Exception::withLocation($e, $constructor->getFileName(), $constructor->getStartLine()); |
| 85 | 93 | } |
| 86 | 94 | } |
@@ -93,16 +101,20 @@ discard block |
||
| 93 | 101 | { |
| 94 | 102 | $namedArgsBegin = $this->analyzeKeys($arguments); |
| 95 | 103 | |
| 96 | - if ($namedArgsBegin === null) { |
|
| 104 | + if ($namedArgsBegin === null) |
|
| 105 | + { |
|
| 97 | 106 | // Only numeric / positional keys exist. |
| 98 | 107 | return $arguments; |
| 99 | 108 | } |
| 100 | 109 | |
| 101 | - if ($namedArgsBegin === 0) { |
|
| 110 | + if ($namedArgsBegin === 0) |
|
| 111 | + { |
|
| 102 | 112 | // Only named keys exist. |
| 103 | 113 | $passed = []; |
| 104 | 114 | $named = $arguments; |
| 105 | - } else { |
|
| 115 | + } |
|
| 116 | + else |
|
| 117 | + { |
|
| 106 | 118 | // Numeric/positional keys followed by named keys. |
| 107 | 119 | // No need to preserve numeric keys. |
| 108 | 120 | $passed = array_slice($arguments, 0, $namedArgsBegin); |
@@ -138,11 +150,14 @@ discard block |
||
| 138 | 150 | $arguments = array_merge($arguments); |
| 139 | 151 | |
| 140 | 152 | $i = 0; |
| 141 | - foreach ($arguments as $k => $_) { |
|
| 142 | - if ($k !== $i) { |
|
| 153 | + foreach ($arguments as $k => $_) |
|
| 154 | + { |
|
| 155 | + if ($k !== $i) |
|
| 156 | + { |
|
| 143 | 157 | // This must be a string key. |
| 144 | 158 | // Any further numeric keys are illegal. |
| 145 | - if (\array_key_exists($i, $arguments)) { |
|
| 159 | + if (\array_key_exists($i, $arguments)) |
|
| 160 | + { |
|
| 146 | 161 | throw new \BadMethodCallException(self::ERROR_POSITIONAL_AFTER_NAMED); |
| 147 | 162 | } |
| 148 | 163 | return $i; |
@@ -179,25 +194,34 @@ discard block |
||
| 179 | 194 | { |
| 180 | 195 | // Analyze parameters. |
| 181 | 196 | $n = count($parameters); |
| 182 | - if ($n > 0 && end($parameters)->isVariadic()) { |
|
| 197 | + if ($n > 0 && end($parameters)->isVariadic()) |
|
| 198 | + { |
|
| 183 | 199 | $variadicParameter = end($parameters); |
| 184 | 200 | // Don't include the variadic parameter in the mapping process. |
| 185 | 201 | --$n; |
| 186 | - } else { |
|
| 202 | + } |
|
| 203 | + else |
|
| 204 | + { |
|
| 187 | 205 | $variadicParameter = null; |
| 188 | 206 | } |
| 189 | 207 | |
| 190 | 208 | // Process parameters that are not already filled with positional args. |
| 191 | 209 | // This loop will do nothing if $namedArgsBegin >= $n. That's ok. |
| 192 | - for ($i = $namedArgsBegin; $i < $n; ++$i) { |
|
| 210 | + for ($i = $namedArgsBegin; $i < $n; ++$i) |
|
| 211 | + { |
|
| 193 | 212 | $parameter = $parameters[$i]; |
| 194 | 213 | $k = $parameter->getName(); |
| 195 | - if (array_key_exists($k, $named)) { |
|
| 214 | + if (array_key_exists($k, $named)) |
|
| 215 | + { |
|
| 196 | 216 | $passed[] = $named[$k]; |
| 197 | 217 | unset($named[$k]); |
| 198 | - } elseif ($parameter->isDefaultValueAvailable()) { |
|
| 218 | + } |
|
| 219 | + elseif ($parameter->isDefaultValueAvailable()) |
|
| 220 | + { |
|
| 199 | 221 | $passed[] = $parameter->getDefaultValue(); |
| 200 | - } else { |
|
| 222 | + } |
|
| 223 | + else |
|
| 224 | + { |
|
| 201 | 225 | $message = \vsprintf(self::ERROR_ARGUMENT_NOT_PASSED, [ |
| 202 | 226 | $ctx->getName(), |
| 203 | 227 | $parameter->getPosition() + 1, |
@@ -208,7 +232,8 @@ discard block |
||
| 208 | 232 | } |
| 209 | 233 | } |
| 210 | 234 | |
| 211 | - if ($named === []) { |
|
| 235 | + if ($named === []) |
|
| 236 | + { |
|
| 212 | 237 | // No unknown argument names exist. |
| 213 | 238 | return $passed; |
| 214 | 239 | } |
@@ -218,11 +243,14 @@ discard block |
||
| 218 | 243 | $badArgName = key($named); |
| 219 | 244 | |
| 220 | 245 | // Check collision with positional arguments. |
| 221 | - foreach ($parameters as $i => $parameter) { |
|
| 222 | - if ($i >= $namedArgsBegin) { |
|
| 246 | + foreach ($parameters as $i => $parameter) |
|
| 247 | + { |
|
| 248 | + if ($i >= $namedArgsBegin) |
|
| 249 | + { |
|
| 223 | 250 | break; |
| 224 | 251 | } |
| 225 | - if ($parameter->getName() === $badArgName) { |
|
| 252 | + if ($parameter->getName() === $badArgName) |
|
| 253 | + { |
|
| 226 | 254 | // The named argument overwrites a positional argument. |
| 227 | 255 | $message = \sprintf(self::ERROR_OVERWRITE_ARGUMENT, $badArgName); |
| 228 | 256 | throw new \BadMethodCallException($message); |
@@ -230,7 +258,8 @@ discard block |
||
| 230 | 258 | } |
| 231 | 259 | |
| 232 | 260 | // Special handling if a variadic parameter is present. |
| 233 | - if ($variadicParameter !== null) { |
|
| 261 | + if ($variadicParameter !== null) |
|
| 262 | + { |
|
| 234 | 263 | // The last parameter is variadic. |
| 235 | 264 | // Since PHP 8+, variadic parameters can consume named arguments. |
| 236 | 265 | // However, this code only runs if PHP < 8. |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | ReaderInterface $reader, |
| 31 | 31 | ?RenderErrors $renderErrors = null, |
| 32 | 32 | int $strategy = self::STRATEGY_JSON_RESPONSE |
| 33 | - ) { |
|
| 33 | + ){ |
|
| 34 | 34 | parent::__construct($container, $strategy); |
| 35 | 35 | |
| 36 | 36 | $this->reader = $reader; |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | { |
| 50 | 50 | parent::buildCache($parameter, $class, $key); |
| 51 | 51 | |
| 52 | - if (null !== ($renderWith = $this->reader->firstClassMetadata($class, RenderWith::class))) { |
|
| 52 | + if (null !== ($renderWith = $this->reader->firstClassMetadata($class, RenderWith::class))){ |
|
| 53 | 53 | $this->renderersCache[$class->getName()] = $this->container->get($renderWith->getRenderer()); |
| 54 | 54 | } |
| 55 | 55 | } |
@@ -49,7 +49,8 @@ |
||
| 49 | 49 | { |
| 50 | 50 | parent::buildCache($parameter, $class, $key); |
| 51 | 51 | |
| 52 | - if (null !== ($renderWith = $this->reader->firstClassMetadata($class, RenderWith::class))) { |
|
| 52 | + if (null !== ($renderWith = $this->reader->firstClassMetadata($class, RenderWith::class))) |
|
| 53 | + { |
|
| 53 | 54 | $this->renderersCache[$class->getName()] = $this->container->get($renderWith->getRenderer()); |
| 54 | 55 | } |
| 55 | 56 | } |
@@ -25,7 +25,7 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | public function render(FilterInterface $filter) |
| 27 | 27 | { |
| 28 | - switch ($this->strategy) { |
|
| 28 | + switch ($this->strategy){ |
|
| 29 | 29 | case FilterInterceptor::STRATEGY_JSON_RESPONSE: |
| 30 | 30 | return [ |
| 31 | 31 | 'status' => 400, |
@@ -25,7 +25,8 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | public function render(FilterInterface $filter) |
| 27 | 27 | { |
| 28 | - switch ($this->strategy) { |
|
| 28 | + switch ($this->strategy) |
|
| 29 | + { |
|
| 29 | 30 | case FilterInterceptor::STRATEGY_JSON_RESPONSE: |
| 30 | 31 | return [ |
| 31 | 32 | 'status' => 400, |
@@ -37,20 +37,20 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | public function process(string $controller, string $action, array $parameters, CoreInterface $core) |
| 39 | 39 | { |
| 40 | - foreach ($this->getDeclaredFilters($controller, $action) as $parameter => $filterClass) { |
|
| 41 | - if (isset($parameters[$parameter])) { |
|
| 40 | + foreach ($this->getDeclaredFilters($controller, $action) as $parameter => $filterClass){ |
|
| 41 | + if (isset($parameters[$parameter])){ |
|
| 42 | 42 | continue; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | /** @var FilterInterface $filter */ |
| 46 | 46 | $filter = $this->container->get($filterClass); |
| 47 | 47 | |
| 48 | - if (isset($parameters['@context'])) { |
|
| 48 | + if (isset($parameters['@context'])){ |
|
| 49 | 49 | // other interceptors can define the validation contex |
| 50 | 50 | $filter->setContext($parameters['@context']); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - if (!$filter->isValid()) { |
|
| 53 | + if (!$filter->isValid()){ |
|
| 54 | 54 | return $this->renderInvalid($filter); |
| 55 | 55 | } |
| 56 | 56 | |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | protected function renderInvalid(FilterInterface $filter) |
| 70 | 70 | { |
| 71 | - switch ($this->strategy) { |
|
| 71 | + switch ($this->strategy){ |
|
| 72 | 72 | case self::STRATEGY_JSON_RESPONSE: |
| 73 | 73 | return [ |
| 74 | 74 | 'status' => 400, |
@@ -87,25 +87,25 @@ discard block |
||
| 87 | 87 | private function getDeclaredFilters(string $controller, string $action): array |
| 88 | 88 | { |
| 89 | 89 | $key = sprintf('%s:%s', $controller, $action); |
| 90 | - if (array_key_exists($key, $this->cache)) { |
|
| 90 | + if (array_key_exists($key, $this->cache)){ |
|
| 91 | 91 | return $this->cache[$key]; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | $this->cache[$key] = []; |
| 95 | - try { |
|
| 95 | + try{ |
|
| 96 | 96 | $method = new \ReflectionMethod($controller, $action); |
| 97 | - } catch (\ReflectionException $e) { |
|
| 97 | + }catch (\ReflectionException $e){ |
|
| 98 | 98 | return []; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - foreach ($method->getParameters() as $parameter) { |
|
| 101 | + foreach ($method->getParameters() as $parameter){ |
|
| 102 | 102 | $class = $this->getParameterClass($parameter); |
| 103 | 103 | |
| 104 | - if ($class === null) { |
|
| 104 | + if ($class === null){ |
|
| 105 | 105 | continue; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - if ($class->implementsInterface(FilterInterface::class)) { |
|
| 108 | + if ($class->implementsInterface(FilterInterface::class)){ |
|
| 109 | 109 | $this->buildCache($parameter, $class, $key); |
| 110 | 110 | } |
| 111 | 111 | } |
@@ -123,11 +123,11 @@ discard block |
||
| 123 | 123 | { |
| 124 | 124 | $type = $parameter->getType(); |
| 125 | 125 | |
| 126 | - if (!$type instanceof \ReflectionNamedType) { |
|
| 126 | + if (!$type instanceof \ReflectionNamedType){ |
|
| 127 | 127 | return null; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | - if ($type->isBuiltin()) { |
|
| 130 | + if ($type->isBuiltin()){ |
|
| 131 | 131 | return null; |
| 132 | 132 | } |
| 133 | 133 | |
@@ -37,20 +37,24 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | public function process(string $controller, string $action, array $parameters, CoreInterface $core) |
| 39 | 39 | { |
| 40 | - foreach ($this->getDeclaredFilters($controller, $action) as $parameter => $filterClass) { |
|
| 41 | - if (isset($parameters[$parameter])) { |
|
| 40 | + foreach ($this->getDeclaredFilters($controller, $action) as $parameter => $filterClass) |
|
| 41 | + { |
|
| 42 | + if (isset($parameters[$parameter])) |
|
| 43 | + { |
|
| 42 | 44 | continue; |
| 43 | 45 | } |
| 44 | 46 | |
| 45 | 47 | /** @var FilterInterface $filter */ |
| 46 | 48 | $filter = $this->container->get($filterClass); |
| 47 | 49 | |
| 48 | - if (isset($parameters['@context'])) { |
|
| 50 | + if (isset($parameters['@context'])) |
|
| 51 | + { |
|
| 49 | 52 | // other interceptors can define the validation contex |
| 50 | 53 | $filter->setContext($parameters['@context']); |
| 51 | 54 | } |
| 52 | 55 | |
| 53 | - if (!$filter->isValid()) { |
|
| 56 | + if (!$filter->isValid()) |
|
| 57 | + { |
|
| 54 | 58 | return $this->renderInvalid($filter); |
| 55 | 59 | } |
| 56 | 60 | |
@@ -68,7 +72,8 @@ discard block |
||
| 68 | 72 | */ |
| 69 | 73 | protected function renderInvalid(FilterInterface $filter) |
| 70 | 74 | { |
| 71 | - switch ($this->strategy) { |
|
| 75 | + switch ($this->strategy) |
|
| 76 | + { |
|
| 72 | 77 | case self::STRATEGY_JSON_RESPONSE: |
| 73 | 78 | return [ |
| 74 | 79 | 'status' => 400, |
@@ -87,25 +92,32 @@ discard block |
||
| 87 | 92 | private function getDeclaredFilters(string $controller, string $action): array |
| 88 | 93 | { |
| 89 | 94 | $key = sprintf('%s:%s', $controller, $action); |
| 90 | - if (array_key_exists($key, $this->cache)) { |
|
| 95 | + if (array_key_exists($key, $this->cache)) |
|
| 96 | + { |
|
| 91 | 97 | return $this->cache[$key]; |
| 92 | 98 | } |
| 93 | 99 | |
| 94 | 100 | $this->cache[$key] = []; |
| 95 | - try { |
|
| 101 | + try |
|
| 102 | + { |
|
| 96 | 103 | $method = new \ReflectionMethod($controller, $action); |
| 97 | - } catch (\ReflectionException $e) { |
|
| 104 | + } |
|
| 105 | + catch (\ReflectionException $e) |
|
| 106 | + { |
|
| 98 | 107 | return []; |
| 99 | 108 | } |
| 100 | 109 | |
| 101 | - foreach ($method->getParameters() as $parameter) { |
|
| 110 | + foreach ($method->getParameters() as $parameter) |
|
| 111 | + { |
|
| 102 | 112 | $class = $this->getParameterClass($parameter); |
| 103 | 113 | |
| 104 | - if ($class === null) { |
|
| 114 | + if ($class === null) |
|
| 115 | + { |
|
| 105 | 116 | continue; |
| 106 | 117 | } |
| 107 | 118 | |
| 108 | - if ($class->implementsInterface(FilterInterface::class)) { |
|
| 119 | + if ($class->implementsInterface(FilterInterface::class)) |
|
| 120 | + { |
|
| 109 | 121 | $this->buildCache($parameter, $class, $key); |
| 110 | 122 | } |
| 111 | 123 | } |
@@ -123,11 +135,13 @@ discard block |
||
| 123 | 135 | { |
| 124 | 136 | $type = $parameter->getType(); |
| 125 | 137 | |
| 126 | - if (!$type instanceof \ReflectionNamedType) { |
|
| 138 | + if (!$type instanceof \ReflectionNamedType) |
|
| 139 | + { |
|
| 127 | 140 | return null; |
| 128 | 141 | } |
| 129 | 142 | |
| 130 | - if ($type->isBuiltin()) { |
|
| 143 | + if ($type->isBuiltin()) |
|
| 144 | + { |
|
| 131 | 145 | return null; |
| 132 | 146 | } |
| 133 | 147 | |