Skip to content

Commit

Permalink
Fix param variadic and ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin V (vvval) committed Nov 8, 2019
1 parent a4eec47 commit d740e1f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
9 changes: 9 additions & 0 deletions src/Promise/Declaration/Extractor/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function __construct(
/**
* @param \ReflectionClass $reflection
* @return array
* @throws \ReflectionException
*/
public function getMethods(\ReflectionClass $reflection): array
{
Expand Down Expand Up @@ -198,6 +199,14 @@ private function packParams(\ReflectionMethod $method): array
$param->setDefault($parameter->getDefaultValue());
}

if ($parameter->isVariadic()) {
$param->makeVariadic();
}

if ($parameter->isPassedByReference()) {
$param->makeByRef();
}

$type = $this->defineParamReturnType($parameter);
if ($type !== null) {
$param->setType($type);
Expand Down
8 changes: 8 additions & 0 deletions tests/Promise/ProxyPrinter/Methods/Fixtures/ArgsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

class ArgsFixture
{
public function referencedSetter(string $a, &$b, int $c): void
{
}

public function typedSetter(string $a, $b, int $c): void
{
}
Expand All @@ -15,4 +19,8 @@ public function typedSetter(string $a, $b, int $c): void
public function defaultsSetter(string $a, $b = [], int $c = 3, bool $d): void
{
}

public function variadicSetter($a, string ...$b): void
{
}
}
54 changes: 34 additions & 20 deletions tests/Promise/ProxyPrinter/Methods/MethodArgsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,50 @@ class MethodArgsTest extends BaseProxyPrinterTest
*/
public function testHasArgType(): void
{
$classname = Fixtures\ArgsFixture::class;
$as = self::NS . __CLASS__ . __LINE__;
$reflection = new \ReflectionClass($classname);
$output = $this->makeOutput(Fixtures\ArgsFixture::class, self::NS . __CLASS__ . __LINE__);

$parent = Declarations::createParentFromReflection($reflection);
$class = Declarations::createClassFromName($as, $parent);
$this->assertStringContainsString('typedSetter(string $a, $b, int $c)', $output);
}

$output = $this->make($reflection, $class, $parent);
$output = ltrim($output, '<?php');
/**
* @throws \ReflectionException
*/
public function testArgDefaults(): void
{
$output = $this->makeOutput(Fixtures\ArgsFixture::class, self::NS . __CLASS__ . __LINE__);

$this->assertFalse(class_exists($class->getFullName()));
//Long syntax by default
$this->assertStringContainsString('defaultsSetter(string $a, $b = array(), int $c = 3, bool $d)', $output);
}

eval($output);
/**
* @throws \ReflectionException
*/
public function testVariadicArg(): void
{
$output = $this->makeOutput(Fixtures\ArgsFixture::class, self::NS . __CLASS__ . __LINE__);

$this->assertStringContainsString('typedSetter(string $a, $b, int $c)', $output);
$this->assertStringContainsString('public function variadicSetter($a, string ...$b)', $output);
}

/**
* @throws \ReflectionException
*/
public function testArgDefaults(): void
public function testReferencedArg(): void
{
$output = $this->makeOutput(Fixtures\ArgsFixture::class, self::NS . __CLASS__ . __LINE__);

$this->assertStringContainsString('public function referencedSetter(string $a, &$b, int $c)', $output);
}

/**
* @param string $classname
* @param string $as
* @return string
* @throws \ReflectionException
*/
private function makeOutput(string $classname, string $as): string
{
$classname = Fixtures\ArgsFixture::class;
$as = self::NS . __CLASS__ . __LINE__;
$reflection = new \ReflectionClass($classname);

$parent = Declarations::createParentFromReflection($reflection);
Expand All @@ -50,12 +70,6 @@ public function testArgDefaults(): void

eval($output);

//Long syntax by default
$this->assertStringContainsString('defaultsSetter(string $a, $b = array(), int $c = 3, bool $d)', $output);
}

public function testArgVariadic(): void
{
$this->assertTrue(true);
return $output;
}
}

0 comments on commit d740e1f

Please sign in to comment.