Skip to content

Commit

Permalink
Merge pull request #434 from Ocramius/fix/variadic-arguments-remote-s…
Browse files Browse the repository at this point in the history
…ervice-proxy-2.2.x

Fix passing of variadic arguments to RPC adapters in remote object proxies (Backport of #443)
  • Loading branch information
Ocramius authored Sep 27, 2018
2 parents 306da83 + a33084d commit 14b137b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,16 @@
use ProxyManager\Generator\MethodGenerator;
use ProxyManager\Generator\Util\ProxiedMethodReturnExpression;
use ReflectionClass;
use Zend\Code\Generator\ParameterGenerator;
use Zend\Code\Generator\PropertyGenerator;
use Zend\Code\Reflection\MethodReflection;
use function var_export;

/**
* Method decorator for remote objects
*
* @author Vincent Blanchon <[email protected]>
* @license MIT
*/
class RemoteObjectMethod extends MethodGenerator
{
/**
* @param \Zend\Code\Reflection\MethodReflection $originalMethod
* @param \Zend\Code\Generator\PropertyGenerator $adapterProperty
* @param \ReflectionClass $originalClass
*
* @return self|static
*/
Expand All @@ -31,19 +25,13 @@ public static function generateMethod(
PropertyGenerator $adapterProperty,
ReflectionClass $originalClass
) : self {
/* @var $method self */
$method = static::fromReflectionWithoutBodyAndDocBlock($originalMethod);
$list = array_values(array_map(
function (ParameterGenerator $parameter) : string {
return '$' . $parameter->getName();
},
$method->getParameters()
));
/** @var self $method */
$method = static::fromReflectionWithoutBodyAndDocBlock($originalMethod);

$method->setBody(
'$return = $this->' . $adapterProperty->getName()
. '->call(' . var_export($originalClass->getName(), true)
. ', ' . var_export($originalMethod->getName(), true) . ', array('. implode(', ', $list) .'));' . "\n\n"
. ', ' . var_export($originalMethod->getName(), true) . ', \func_get_args());' . "\n\n"
. ProxiedMethodReturnExpression::generate('$return', $originalMethod)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ProxyManagerTestAsset\OtherObjectAccessClass;
use ProxyManagerTestAsset\RemoteProxy\Foo;
use ProxyManagerTestAsset\RemoteProxy\FooServiceInterface;
use ProxyManagerTestAsset\RemoteProxy\VariadicArgumentsServiceInterface;
use ProxyManagerTestAsset\VoidCounter;
use ReflectionClass;
use Zend\Server\Client;
Expand Down Expand Up @@ -197,6 +198,12 @@ public function getProxyMethods() : array
[$selfHintParam],
$selfHintParam
],
[
VariadicArgumentsServiceInterface::class,
'method',
['aaa', 1, 2, 3, 4, 5],
true,
],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testBodyStructureWithParameters() : void
self::assertCount(2, $method->getParameters());
self::assertSame(
'$return = $this->adapter->call(\'Zend\\\Code\\\Generator\\\PropertyGenerator\', '
. '\'publicByReferenceParameterMethod\', array($param, $byRefParam));'
. '\'publicByReferenceParameterMethod\', \func_get_args());'
. "\n\nreturn \$return;",
$method->getBody()
);
Expand All @@ -72,7 +72,7 @@ public function testBodyStructureWithArrayParameter() : void
self::assertCount(1, $method->getParameters());
self::assertSame(
'$return = $this->adapter->call(\'Zend\\\Code\\\Generator\\\PropertyGenerator\', '
. '\'publicArrayHintedMethod\', array($param));'
. '\'publicArrayHintedMethod\', \func_get_args());'
. "\n\nreturn \$return;",
$method->getBody()
);
Expand All @@ -99,7 +99,7 @@ public function testBodyStructureWithoutParameters() : void
self::assertCount(0, $method->getParameters());
self::assertSame(
'$return = $this->adapter->call(\'Zend\\\Code\\\Generator\\\PropertyGenerator\', '
. '\'publicMethod\', array());'
. '\'publicMethod\', \func_get_args());'
. "\n\nreturn \$return;",
$method->getBody()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace ProxyManagerTestAsset\RemoteProxy;

/**
* Simple interface for a remote API that has a variadic number of arguments
*
* @author Marco Pivetta <[email protected]>
* @license MIT
*/
interface VariadicArgumentsServiceInterface
{
public function method(string $param1, int ...$param2) : bool;
}

0 comments on commit 14b137b

Please sign in to comment.