Skip to content

Commit

Permalink
Fix call parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuffre committed Jun 28, 2017
1 parent 6d289ae commit 3dbefd0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
22 changes: 21 additions & 1 deletion src/Moka/Generator/ProxyClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
class ProxyClassGenerator
{
const UNSAFE_METHODS = ['__call', '__construct', '__destruct', '__clone'];
// const CALL_PARAMETERS = ['name', 'arguments'];

private static $template = '
return new class extends %s implements %s
{
use %s;
%s
public function __call(%s $name, %s $arguments)
{
return $this->doCall($name, $arguments);
}
};
';

Expand All @@ -22,18 +28,32 @@ public static function generateCode(string $classWillBeEtended): string
$reflection = new \ReflectionClass($classWillBeEtended);
$methods = $reflection->getMethods();
$methodsArray = [];

foreach ($methods as $method) {
if (!in_array($method->getName(), self::UNSAFE_METHODS, true)) {
$methodsArray[] = ProxyMethodGenerator::generateMethodString($method);
}

if ($method->getName() === '__call') {
$p = [];
$callParameters = $method->getParameters();
foreach ($callParameters as $callParameter) {
$p[$callParameter->getPosition()] = (string)$callParameter->getType();
}
}
}

$callName = $p[0] ?? '';
$callArguments = $p[1] ?? '';

return sprintf(
self::$template,
$classWillBeEtended,
ProxyInterface::class,
ProxyTrait::class,
implode(PHP_EOL, $methodsArray)
implode(PHP_EOL, $methodsArray),
$callName,
$callArguments
);
}
}
14 changes: 7 additions & 7 deletions src/Moka/Generator/ProxyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ public function __construct()
{
}

public function __call($name, array $arguments)
{
if ($this->mockingStrategy instanceof MockingStrategyInterface) {
return $this->mockingStrategy->call($this->mock, $name, $arguments);
}
}

/**
* @param mixed $object
*/
Expand Down Expand Up @@ -70,4 +63,11 @@ public function serve()
{
return $this->mock;
}

protected function doCall(string $name, array $arguments)
{
if ($this->mockingStrategy instanceof MockingStrategyInterface) {
return $this->mockingStrategy->call($this->mock, $name, $arguments);
}
}
}

0 comments on commit 3dbefd0

Please sign in to comment.