Skip to content

Commit 09fed30

Browse files
JJK96felixfbecker
authored andcommitted
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.
1 parent 8c2d25c commit 09fed30

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
vendor/
44
composer.lock
55
node_modules/
6+
.idea/

lib/Dispatcher.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,15 @@ public function dispatch($msg)
104104
if (is_array($msg->params)) {
105105
$args = $msg->params;
106106
} else if (is_object($msg->params)) {
107-
foreach (get_object_vars($msg->params) as $key => $value) {
108-
$position = -1;
109-
foreach ($parameters as $pos => $parameter) {
107+
foreach ($parameters as $pos => $parameter) {
108+
$value = null;
109+
foreach(get_object_vars($msg->params) as $key => $val) {
110110
if ($parameter->name === $key) {
111-
$position = $pos;
111+
$value = $val;
112112
break;
113113
}
114114
}
115-
if ($position === -1) {
116-
// Unknown parameter, ignore
117-
continue;
118-
}
119-
$args[$position] = $value;
115+
$args[$pos] = $value;
120116
}
121117
} else {
122118
throw new Error('Params must be structured or omitted', ErrorCode::INVALID_REQUEST);

tests/DispatcherTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public function testCallMethodWithArrayParamTag()
5050
$this->assertEquals($this->calls, [new MethodCall('someMethodWithArrayParamTag', [[new Argument('whatever')]])]);
5151
}
5252

53+
public function testCallMethodWithMissingArgument()
54+
{
55+
$result = $this->dispatcher->dispatch((string)new Request(1, 'someMethodWithDifferentlyTypedArgs', ['arg2' => 0]));
56+
$this->assertEquals('Hello World', $result);
57+
$this->assertEquals($this->calls, [new MethodCall('someMethodWithDifferentlyTypedArgs', [0 => null, 1 => 0])]);
58+
}
59+
5360
public function testCallMethodWithUnionTypeParamTag()
5461
{
5562
$result = $this->dispatcher->dispatch((string)new Request(1, 'someMethodWithUnionTypeParamTag', ['arg' => [new Argument('whatever')]]));
@@ -64,4 +71,6 @@ public function testCallMethodWithTypeHintWithNamedArgsOnNestedTarget()
6471
$this->assertEquals($this->calls, []);
6572
$this->assertEquals($this->callsOfNestedTarget, [new MethodCall('someMethodWithTypeHint', [new Argument('whatever')])]);
6673
}
74+
75+
6776
}

tests/Target.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@ public function someMethodWithUnionTypeParamTag($arg)
4343
$this->calls[] = new MethodCall('someMethodWithUnionTypeParamTag', func_get_args());
4444
return 'Hello World';
4545
}
46+
47+
public function someMethodWithDifferentlyTypedArgs(string $arg1 = null, int $arg2 = null)
48+
{
49+
$this->calls[] = new MethodCall('someMethodWithDifferentlyTypedArgs', func_get_args());
50+
return 'Hello World';
51+
}
4652
}

0 commit comments

Comments
 (0)