Skip to content

Commit

Permalink
fix(dispatcher): pass null for missing arguments (#27)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JJK96 authored and felixfbecker committed Sep 9, 2018
1 parent 8c2d25c commit 09fed30
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
vendor/
composer.lock
node_modules/
.idea/
14 changes: 5 additions & 9 deletions lib/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions tests/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]]));
Expand All @@ -64,4 +71,6 @@ public function testCallMethodWithTypeHintWithNamedArgsOnNestedTarget()
$this->assertEquals($this->calls, []);
$this->assertEquals($this->callsOfNestedTarget, [new MethodCall('someMethodWithTypeHint', [new Argument('whatever')])]);
}


}
6 changes: 6 additions & 0 deletions tests/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}

0 comments on commit 09fed30

Please sign in to comment.