From f3bca9a1ae6b1c4367ab614e88d622e65313aebe Mon Sep 17 00:00:00 2001 From: sakhunzai Date: Fri, 3 Mar 2023 16:58:33 +0500 Subject: [PATCH 1/5] Update ParameterGenerator.php setVariadic should only throw exception when parameter is variadic and has default value. Signed-off-by: sakhunzai --- src/Generator/ParameterGenerator.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Generator/ParameterGenerator.php b/src/Generator/ParameterGenerator.php index d1259e8b..913f8ec3 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; } From 538565f5b68aaf19585778a77cd29da99bad5eec Mon Sep 17 00:00:00 2001 From: sakhunzai Date: Fri, 3 Mar 2023 17:09:33 +0500 Subject: [PATCH 2/5] Update ParameterGeneratorTest.php Non variadic parameter should not throw exception when having default value Signed-off-by: sakhunzai --- test/Generator/ParameterGeneratorTest.php | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/Generator/ParameterGeneratorTest.php b/test/Generator/ParameterGeneratorTest.php index 46d1029c..bb83c773 100644 --- a/test/Generator/ParameterGeneratorTest.php +++ b/test/Generator/ParameterGeneratorTest.php @@ -550,6 +550,18 @@ public function testAssigningDefaultValueToVariadicParameterThrowsInvalidArgumen $parameter->setDefaultValue([]); } + public function testAssigningDefaultValueToNonVariadicParameter(): void + { + $parameter = new ParameterGenerator(); + + $parameter->setName('parameter'); + $parameter->setType('int'); + $parameter->setPosition(1); + $parameter->setVariadic(false); + $this->expectNotToPerformAssertions(); + $parameter->setDefaultValue([]); + } + public function testMakingParameterVariadicWithExistingDefaultValueThrowsInvalidArgumentException(): void { $parameter = new ParameterGenerator(); @@ -565,6 +577,18 @@ public function testMakingParameterVariadicWithExistingDefaultValueThrowsInvalid $parameter->setVariadic(true); } + + public function testMakingParameterNonVariadicWithExistingDefaultValue(): void + { + $parameter = new ParameterGenerator(); + + $parameter->setName('parameter'); + $parameter->setType('int'); + $parameter->setPosition(1); + $parameter->setDefaultValue([]); + $this->expectNotToPerformAssertions(); + $parameter->setVariadic(false); + } #[Group('zendframework/zend-code#29')] public function testGetInternalClassDefaultParameterValue() From 6669044d8c59df0d43e104b977e1be937440956c Mon Sep 17 00:00:00 2001 From: sakhunzai Date: Fri, 3 Mar 2023 20:13:57 +0500 Subject: [PATCH 3/5] Update ParameterGenerator.php remote whitespaces Signed-off-by: sakhunzai --- src/Generator/ParameterGenerator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Generator/ParameterGenerator.php b/src/Generator/ParameterGenerator.php index 913f8ec3..48434ab4 100644 --- a/src/Generator/ParameterGenerator.php +++ b/src/Generator/ParameterGenerator.php @@ -257,11 +257,11 @@ public function setPassedByReference($passedByReference) public function setVariadic($variadic) { $this->variadic = (bool) $variadic; - + if (true === $this->variadic && isset($this->defaultValue)) { throw new Exception\InvalidArgumentException('Variadic parameter cannot have a default value'); } - + return $this; } From 3961d7a9ed6e79970a877723c462bb58fe9a4ce8 Mon Sep 17 00:00:00 2001 From: sakhunzai Date: Fri, 3 Mar 2023 20:15:55 +0500 Subject: [PATCH 4/5] Update ParameterGeneratorTest.php remote whitespaces Signed-off-by: sakhunzai --- test/Generator/ParameterGeneratorTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Generator/ParameterGeneratorTest.php b/test/Generator/ParameterGeneratorTest.php index bb83c773..2502a670 100644 --- a/test/Generator/ParameterGeneratorTest.php +++ b/test/Generator/ParameterGeneratorTest.php @@ -561,7 +561,7 @@ public function testAssigningDefaultValueToNonVariadicParameter(): void $this->expectNotToPerformAssertions(); $parameter->setDefaultValue([]); } - + public function testMakingParameterVariadicWithExistingDefaultValueThrowsInvalidArgumentException(): void { $parameter = new ParameterGenerator(); @@ -577,7 +577,7 @@ public function testMakingParameterVariadicWithExistingDefaultValueThrowsInvalid $parameter->setVariadic(true); } - + public function testMakingParameterNonVariadicWithExistingDefaultValue(): void { $parameter = new ParameterGenerator(); From 0896096f0f1fc1965601ace286ac1a847bfcd2a7 Mon Sep 17 00:00:00 2001 From: sakhunzai Date: Fri, 3 Mar 2023 23:53:36 +0500 Subject: [PATCH 5/5] Update ParameterGeneratorTest.php added generated code expectation Signed-off-by: sakhunzai --- test/Generator/ParameterGeneratorTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/Generator/ParameterGeneratorTest.php b/test/Generator/ParameterGeneratorTest.php index 2502a670..9d2eb755 100644 --- a/test/Generator/ParameterGeneratorTest.php +++ b/test/Generator/ParameterGeneratorTest.php @@ -558,8 +558,9 @@ public function testAssigningDefaultValueToNonVariadicParameter(): void $parameter->setType('int'); $parameter->setPosition(1); $parameter->setVariadic(false); - $this->expectNotToPerformAssertions(); - $parameter->setDefaultValue([]); + self::assertSame('int $parameter', $parameter->generate()); + $parameter->setDefaultValue(7); + self::assertSame('int $parameter = 7', $parameter->generate()); } public function testMakingParameterVariadicWithExistingDefaultValueThrowsInvalidArgumentException(): void @@ -585,9 +586,10 @@ public function testMakingParameterNonVariadicWithExistingDefaultValue(): void $parameter->setName('parameter'); $parameter->setType('int'); $parameter->setPosition(1); - $parameter->setDefaultValue([]); - $this->expectNotToPerformAssertions(); + $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')]