From 09fed304fc802b82be939a64691c09443c97defa Mon Sep 17 00:00:00 2001 From: JJK96 Date: Sun, 9 Sep 2018 19:23:26 +0200 Subject: [PATCH] fix(dispatcher): pass null for missing arguments (#27) Currently if the received params object does not contain a certain parameter of the function this parameter is simply omitted in the array, resulting in a displacement of the following arguments. This commit fills a missing parameter in the array by the value `null` so that at least all parameters are represented in the args array. --- .gitignore | 1 + lib/Dispatcher.php | 14 +++++--------- tests/DispatcherTest.php | 9 +++++++++ tests/Target.php | 6 ++++++ 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 05e4e98..57fd5a6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ vendor/ composer.lock node_modules/ +.idea/ diff --git a/lib/Dispatcher.php b/lib/Dispatcher.php index d31e53e..3814005 100644 --- a/lib/Dispatcher.php +++ b/lib/Dispatcher.php @@ -104,19 +104,15 @@ public function dispatch($msg) if (is_array($msg->params)) { $args = $msg->params; } else if (is_object($msg->params)) { - foreach (get_object_vars($msg->params) as $key => $value) { - $position = -1; - foreach ($parameters as $pos => $parameter) { + foreach ($parameters as $pos => $parameter) { + $value = null; + foreach(get_object_vars($msg->params) as $key => $val) { if ($parameter->name === $key) { - $position = $pos; + $value = $val; break; } } - if ($position === -1) { - // Unknown parameter, ignore - continue; - } - $args[$position] = $value; + $args[$pos] = $value; } } else { throw new Error('Params must be structured or omitted', ErrorCode::INVALID_REQUEST); diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index c3bfe37..ee9ac77 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -50,6 +50,13 @@ public function testCallMethodWithArrayParamTag() $this->assertEquals($this->calls, [new MethodCall('someMethodWithArrayParamTag', [[new Argument('whatever')]])]); } + public function testCallMethodWithMissingArgument() + { + $result = $this->dispatcher->dispatch((string)new Request(1, 'someMethodWithDifferentlyTypedArgs', ['arg2' => 0])); + $this->assertEquals('Hello World', $result); + $this->assertEquals($this->calls, [new MethodCall('someMethodWithDifferentlyTypedArgs', [0 => null, 1 => 0])]); + } + public function testCallMethodWithUnionTypeParamTag() { $result = $this->dispatcher->dispatch((string)new Request(1, 'someMethodWithUnionTypeParamTag', ['arg' => [new Argument('whatever')]])); @@ -64,4 +71,6 @@ public function testCallMethodWithTypeHintWithNamedArgsOnNestedTarget() $this->assertEquals($this->calls, []); $this->assertEquals($this->callsOfNestedTarget, [new MethodCall('someMethodWithTypeHint', [new Argument('whatever')])]); } + + } diff --git a/tests/Target.php b/tests/Target.php index 0773106..84ece52 100644 --- a/tests/Target.php +++ b/tests/Target.php @@ -43,4 +43,10 @@ public function someMethodWithUnionTypeParamTag($arg) $this->calls[] = new MethodCall('someMethodWithUnionTypeParamTag', func_get_args()); return 'Hello World'; } + + public function someMethodWithDifferentlyTypedArgs(string $arg1 = null, int $arg2 = null) + { + $this->calls[] = new MethodCall('someMethodWithDifferentlyTypedArgs', func_get_args()); + return 'Hello World'; + } }