Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure variadic parameter does not have default value #72

8 changes: 8 additions & 0 deletions src/Generator/ParameterGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ public function getName()
*/
public function setDefaultValue($defaultValue)
{
if ($this->variadic) {
throw new Exception\InvalidArgumentException('Variadic parameter cannot have a default value');
}

if (! $defaultValue instanceof ValueGenerator) {
$defaultValue = new ValueGenerator($defaultValue);
}
Expand Down Expand Up @@ -265,6 +269,10 @@ public function setPassedByReference($passedByReference)
*/
public function setVariadic($variadic)
{
if (isset($this->defaultValue)) {
throw new Exception\InvalidArgumentException('Variadic parameter cannot have a default value');
}

$this->variadic = (bool) $variadic;

return $this;
Expand Down
34 changes: 33 additions & 1 deletion test/Generator/ParameterGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace LaminasTest\Code\Generator;

use Closure;
use Laminas\Code\Generator\Exception\InvalidArgumentException;
use Laminas\Code\Generator\ParameterGenerator;
use Laminas\Code\Generator\ValueGenerator;
use Laminas\Code\Reflection\ClassReflection;
Expand Down Expand Up @@ -516,7 +517,7 @@ public function testVariadicArgumentFromReflection(
$methodName,
$parameterName,
$expectedGeneratedSignature
) {
): void {
$parameter = ParameterGenerator::fromReflection(new ParameterReflection(
[$className, $methodName],
$parameterName
Expand Down Expand Up @@ -576,6 +577,37 @@ public function testSetGetVariadic()
self::assertSame('$foo', $parameter->generate());
}

public function testAssigningDefaultValueToVariadicParameterThrowsInvalidArgumentException(): void
{
$parameter = new ParameterGenerator();

$parameter->setName('parameter');
$parameter->setType('int');
$parameter->setPosition(1);
$parameter->setVariadic(true);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Variadic parameter cannot have a default value');

$parameter->setDefaultValue([]);
}

public function testMakingParameterVariadicWithExistingDefaultValueThrowsInvalidArgumentException(): void
{
$parameter = new ParameterGenerator();

$parameter->setName('parameter');
$parameter->setType('int');
$parameter->setPosition(1);

$parameter->setDefaultValue([]);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Variadic parameter cannot have a default value');
mchekin marked this conversation as resolved.
Show resolved Hide resolved

$parameter->setVariadic(true);
}

/**
* @group zendframework/zend-code#29
*/
Expand Down