Skip to content

Commit

Permalink
Merge pull request #173 from sakhunzai/4.9.x
Browse files Browse the repository at this point in the history
Non-variadic parameter should not throw exception when calling `ParameterGenerator#setVariadic(false)` with an existing default value
  • Loading branch information
Ocramius authored Mar 4, 2023
2 parents 7d57ac6 + 0896096 commit c1706ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Generator/ParameterGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,12 @@ public function setPassedByReference($passedByReference)
*/
public function setVariadic($variadic)
{
if (isset($this->defaultValue)) {
$this->variadic = (bool) $variadic;

if (true === $this->variadic && isset($this->defaultValue)) {
throw new Exception\InvalidArgumentException('Variadic parameter cannot have a default value');
}

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

return $this;
}

Expand Down
26 changes: 26 additions & 0 deletions test/Generator/ParameterGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,19 @@ public function testAssigningDefaultValueToVariadicParameterThrowsInvalidArgumen
$parameter->setDefaultValue([]);
}

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

$parameter->setName('parameter');
$parameter->setType('int');
$parameter->setPosition(1);
$parameter->setVariadic(false);
self::assertSame('int $parameter', $parameter->generate());
$parameter->setDefaultValue(7);
self::assertSame('int $parameter = 7', $parameter->generate());
}

public function testMakingParameterVariadicWithExistingDefaultValueThrowsInvalidArgumentException(): void
{
$parameter = new ParameterGenerator();
Expand All @@ -566,6 +579,19 @@ public function testMakingParameterVariadicWithExistingDefaultValueThrowsInvalid
$parameter->setVariadic(true);
}

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

$parameter->setName('parameter');
$parameter->setType('int');
$parameter->setPosition(1);
$parameter->setDefaultValue(7);
self::assertSame('int $parameter = 7', $parameter->generate());
$parameter->setVariadic(false);
self::assertSame('int $parameter = 7', $parameter->generate());
}

#[Group('zendframework/zend-code#29')]
public function testGetInternalClassDefaultParameterValue()
{
Expand Down

0 comments on commit c1706ec

Please sign in to comment.