Skip to content

Commit

Permalink
Preventing variadic parameter with default value in a setters instead…
Browse files Browse the repository at this point in the history
… of during the parameter generation.

Signed-off-by: Michael Chekin <mchekin@gmail.com>
  • Loading branch information
mchekin committed Mar 30, 2021
1 parent 91151f8 commit d031130
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
12 changes: 8 additions & 4 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 Expand Up @@ -300,10 +308,6 @@ public function generate()
}

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

$output .= ' = ';
$this->defaultValue->setOutputMode(ValueGenerator::OUTPUT_SINGLE_LINE);
$output .= $this->defaultValue;
Expand Down
18 changes: 16 additions & 2 deletions test/Generator/ParameterGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ public function testSetGetVariadic()
self::assertSame('$foo', $parameter->generate());
}

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

Expand All @@ -586,12 +586,26 @@ public function testGeneratingVariadicParameterWithDefaultValueThrowsInvalidArgu
$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');

$parameter->generate();
$parameter->setVariadic(true);
}

/**
Expand Down

0 comments on commit d031130

Please sign in to comment.