diff --git a/src/Generator/ParameterGenerator.php b/src/Generator/ParameterGenerator.php index d1259e8b..48434ab4 100644 --- a/src/Generator/ParameterGenerator.php +++ b/src/Generator/ParameterGenerator.php @@ -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; } diff --git a/test/Generator/ParameterGeneratorTest.php b/test/Generator/ParameterGeneratorTest.php index 46d1029c..9d2eb755 100644 --- a/test/Generator/ParameterGeneratorTest.php +++ b/test/Generator/ParameterGeneratorTest.php @@ -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(); @@ -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() {