From 2ecfcd2319f75a7d5083da4fc988ade3f686b588 Mon Sep 17 00:00:00 2001 From: Farrokh Ghamsary Date: Mon, 12 Dec 2022 17:41:43 +0100 Subject: [PATCH 1/2] Resolve problem of zero as default value for a property --- src/Property.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Property.php b/src/Property.php index 035bc98..2fca957 100644 --- a/src/Property.php +++ b/src/Property.php @@ -56,7 +56,7 @@ public static function new( public function generate(): string { $docBlock = $this->docBlock ? "$this->docBlock\n" : ''; - $value = $this->value ? " = $this->value" : ''; + $value = $this->value !== '' ? " = $this->value" : ''; $isStatic = $this->isStatic ? 'static ' : ''; $typeHint = ''; From d4bd42028239865088e117ecf517e499e89539d5 Mon Sep 17 00:00:00 2001 From: Farrokh Ghamsary Date: Tue, 13 Dec 2022 07:36:20 +0100 Subject: [PATCH 2/2] Added tests for default property value check --- tests/PropertyTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/PropertyTest.php b/tests/PropertyTest.php index 9884d26..07aef51 100644 --- a/tests/PropertyTest.php +++ b/tests/PropertyTest.php @@ -22,6 +22,23 @@ public function createProperty(): void $this->assertEquals($anotherName, $property->getName()); $this->assertEquals(Modifier::PUBLIC, $property->getModifier()); + $property->setTypeHint('int'); + $this->assertEquals("public int $$anotherName", $property->generate()); + + $property->setDefaultValue(0); + $this->assertEquals("public int $$anotherName = 0", $property->generate()); + + $property->setDefaultValue(Property::NO_PARAM); + $this->assertEquals("public int $$anotherName", $property->generate()); + + $property->setTypeHint(''); + $property->setDefaultValue(''); + $this->assertEquals("public $$anotherName = ''", $property->generate()); + + $property->setDefaultValue(null); + $this->assertEquals("public $$anotherName = null", $property->generate()); + + $property->setDefaultValue(Property::NO_PARAM); $property->setStatic(); $this->assertTrue($property->isStatic); $this->assertEquals("public static $$anotherName", $property->generate());