From 7d042a7c08bafec3c1655277f0a57fbb2ce82701 Mon Sep 17 00:00:00 2001 From: Rudolph Gottesheim Date: Mon, 4 Mar 2019 11:54:12 +0100 Subject: [PATCH] Omit default property value if it is null --- src/Generator/PropertyGenerator.php | 6 +++++- test/Generator/PropertyGeneratorTest.php | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Generator/PropertyGenerator.php b/src/Generator/PropertyGenerator.php index 6259efc0..b5838e60 100644 --- a/src/Generator/PropertyGenerator.php +++ b/src/Generator/PropertyGenerator.php @@ -46,7 +46,11 @@ public static function fromReflection(PropertyReflection $reflectionProperty) $allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties(); - $property->setDefaultValue($allDefaultProperties[$reflectionProperty->getName()]); + $defaultValue = $allDefaultProperties[$reflectionProperty->getName()]; + $property->setDefaultValue($defaultValue); + if ($defaultValue === null) { + $property->omitDefaultValue = true; + } if ($reflectionProperty->getDocComment() != '') { $property->setDocBlock(DocBlockGenerator::fromReflection($reflectionProperty->getDocBlock())); diff --git a/test/Generator/PropertyGeneratorTest.php b/test/Generator/PropertyGeneratorTest.php index 685c72e8..bf76903e 100644 --- a/test/Generator/PropertyGeneratorTest.php +++ b/test/Generator/PropertyGeneratorTest.php @@ -291,4 +291,15 @@ public function testOmitType() self::assertEquals(' public $foo;', $property->generate()); } + + public function testFromReflectionOmitsDefaultValueIfItIsNull() : void + { + $reflectionClass = new ClassReflection(TestAsset\TestClassWithManyProperties::class); + $propertyReflection = $reflectionClass->getProperty('fooStaticProperty'); + + $generator = PropertyGenerator::fromReflection($propertyReflection); + $code = $generator->generate(); + + $this->assertEquals(' public static $fooStaticProperty;', $code); + } }