Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit eca8365

Browse files
committed
Merge pull request #166 from MidnightDesign/omit-default-null
Omit default property value if it is null
2 parents 0f9ccce + 7d042a7 commit eca8365

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/Generator/PropertyGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public static function fromReflection(PropertyReflection $reflectionProperty)
4646

4747
$allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties();
4848

49-
$property->setDefaultValue($allDefaultProperties[$reflectionProperty->getName()]);
49+
$defaultValue = $allDefaultProperties[$reflectionProperty->getName()];
50+
$property->setDefaultValue($defaultValue);
51+
if ($defaultValue === null) {
52+
$property->omitDefaultValue = true;
53+
}
5054

5155
if ($reflectionProperty->getDocComment() != '') {
5256
$property->setDocBlock(DocBlockGenerator::fromReflection($reflectionProperty->getDocBlock()));

test/Generator/PropertyGeneratorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,15 @@ public function testOmitType()
308308

309309
self::assertEquals(' public $foo;', $property->generate());
310310
}
311+
312+
public function testFromReflectionOmitsDefaultValueIfItIsNull() : void
313+
{
314+
$reflectionClass = new ClassReflection(TestAsset\TestClassWithManyProperties::class);
315+
$propertyReflection = $reflectionClass->getProperty('fooStaticProperty');
316+
317+
$generator = PropertyGenerator::fromReflection($propertyReflection);
318+
$code = $generator->generate();
319+
320+
$this->assertEquals(' public static $fooStaticProperty;', $code);
321+
}
311322
}

0 commit comments

Comments
 (0)