From 222a1bf27365281f1beb971fb7f1c14256d8ef15 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 25 Aug 2023 11:00:43 +0200 Subject: [PATCH 1/2] Avoid instantation of class with type mixed during mapping --- lib/Dispatcher.php | 4 +++- tests/DispatcherTest.php | 8 ++++++++ tests/Target.php | 6 ++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/Dispatcher.php b/lib/Dispatcher.php index 5f045df..6354059 100644 --- a/lib/Dispatcher.php +++ b/lib/Dispatcher.php @@ -133,7 +133,9 @@ public function dispatch($msg) // Fallback for php 7.0, which is still supported (and doesn't have nullable). $class = (string)$paramType; } - $value = $this->mapper->map($value, new $class()); + if ($class !== 'mixed') { + $value = $this->mapper->map($value, new $class()); + } } } else if (is_array($value) && isset($docBlock)) { // Get the array type from the DocBlock diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 8b54d06..bfbb81a 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -57,6 +57,14 @@ public function testCallMethodWithMissingArgument() $this->assertEquals($this->calls, [new MethodCall('someMethodWithDifferentlyTypedArgs', [0 => null, 1 => 0])]); } + public function testCallMethodWithMixedParam() + { + $result = $this->dispatcher->dispatch((string)new Request(1, 'someMethodWithMixedTypeParam', ['arg' => new Argument('whatever')])); + $this->assertEquals('Hello World', $result); + $this->assertIsObject($this->calls[0]->args[0]); + $this->assertEquals($this->calls, [new MethodCall('someMethodWithMixedTypeParam', [0 => $this->calls[0]->args[0]])]); + } + public function testCallMethodWithUnionTypeParamTag() { $result = $this->dispatcher->dispatch((string)new Request(1, 'someMethodWithUnionTypeParamTag', ['arg' => [new Argument('whatever')]])); diff --git a/tests/Target.php b/tests/Target.php index 84ece52..d4d6c08 100644 --- a/tests/Target.php +++ b/tests/Target.php @@ -44,6 +44,12 @@ public function someMethodWithUnionTypeParamTag($arg) return 'Hello World'; } + public function someMethodWithMixedTypeParam(mixed $arg) + { + $this->calls[] = new MethodCall('someMethodWithMixedTypeParam', func_get_args()); + return 'Hello World'; + } + public function someMethodWithDifferentlyTypedArgs(string $arg1 = null, int $arg2 = null) { $this->calls[] = new MethodCall('someMethodWithDifferentlyTypedArgs', func_get_args()); From 0b65db453bc7393c7aedb4997bf4f61197c1dc43 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 25 Aug 2023 11:03:27 +0200 Subject: [PATCH 2/2] Comment out php8-only tests for now --- tests/DispatcherTest.php | 4 ++-- tests/Target.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index bfbb81a..8b223a9 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -57,13 +57,13 @@ public function testCallMethodWithMissingArgument() $this->assertEquals($this->calls, [new MethodCall('someMethodWithDifferentlyTypedArgs', [0 => null, 1 => 0])]); } - public function testCallMethodWithMixedParam() + /*public function testCallMethodWithMixedParam() { $result = $this->dispatcher->dispatch((string)new Request(1, 'someMethodWithMixedTypeParam', ['arg' => new Argument('whatever')])); $this->assertEquals('Hello World', $result); $this->assertIsObject($this->calls[0]->args[0]); $this->assertEquals($this->calls, [new MethodCall('someMethodWithMixedTypeParam', [0 => $this->calls[0]->args[0]])]); - } + }*/ public function testCallMethodWithUnionTypeParamTag() { diff --git a/tests/Target.php b/tests/Target.php index d4d6c08..5ea4b6b 100644 --- a/tests/Target.php +++ b/tests/Target.php @@ -44,11 +44,11 @@ public function someMethodWithUnionTypeParamTag($arg) return 'Hello World'; } - public function someMethodWithMixedTypeParam(mixed $arg) + /*public function someMethodWithMixedTypeParam(mixed $arg) { $this->calls[] = new MethodCall('someMethodWithMixedTypeParam', func_get_args()); return 'Hello World'; - } + }*/ public function someMethodWithDifferentlyTypedArgs(string $arg1 = null, int $arg2 = null) {