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

Add omit property value type #131

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Generator/ParameterGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class ParameterGenerator extends AbstractGenerator
*/
private $variadic = false;

/**
* @var bool
*/
private $omitDefaultValue = false;

/**
* @param ParameterReflection $reflectionParameter
* @return ParameterGenerator
Expand Down Expand Up @@ -306,6 +311,10 @@ public function generate()

$output .= '$' . $this->name;

if ($this->omitDefaultValue) {
return $output;
}

if ($this->defaultValue !== null) {
$output .= ' = ';
if (is_string($this->defaultValue)) {
Expand Down Expand Up @@ -402,4 +411,14 @@ private function generateTypeHint()

return $this->type->generate() . ' ';
}

/**
* @return ParameterGenerator
*/
public function omitDefaultValue()
{
$this->omitDefaultValue = true;

return $this;
}
}
31 changes: 24 additions & 7 deletions src/Generator/PropertyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class PropertyGenerator extends AbstractMemberGenerator
*/
protected $defaultValue;

/**
* @var bool
*/
private $omitDefaultValue = false;

/**
* @param PropertyReflection $reflectionProperty
* @return PropertyGenerator
Expand Down Expand Up @@ -220,14 +225,26 @@ public function generate()
}
$output .= $this->indentation . 'const ' . $name . ' = '
. ($defaultValue !== null ? $defaultValue->generate() : 'null;');
} else {
$output .= $this->indentation
. $this->getVisibility()
. ($this->isStatic() ? ' static' : '')
. ' $' . $name . ' = '
. ($defaultValue !== null ? $defaultValue->generate() : 'null;');

return $output;
}

$output .= $this->indentation . $this->getVisibility() . ($this->isStatic() ? ' static' : '') . ' $' . $name;

if ($this->omitDefaultValue) {
return $output . ';';
}

return $output;
return $output . ' = ' . ($defaultValue !== null ? $defaultValue->generate() : 'null;');
}

/**
* @return PropertyGenerator
*/
public function omitDefaultValue()
{
$this->omitDefaultValue = true;

return $this;
}
}
8 changes: 8 additions & 0 deletions test/Generator/ParameterGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,4 +582,12 @@ public function testGetInternalClassDefaultParameterValue()

self::assertSame('null', strtolower((string) $parameter->getDefaultValue()));
}

public function testOmitType()
{
$parameter = new ParameterGenerator('foo', 'string', 'bar');
$parameter->omitDefaultValue();

self::assertEquals('string $foo', $parameter->generate());
}
}
8 changes: 8 additions & 0 deletions test/Generator/PropertyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,12 @@ public function testSetDefaultValue(string $type, $value) : void
self::assertEquals($type, $property->getDefaultValue()->getType());
self::assertEquals($value, $property->getDefaultValue()->getValue());
}

public function testOmitType()
{
$property = new PropertyGenerator('foo', null);
$property->omitDefaultValue();

self::assertEquals(' public $foo;', $property->generate());
}
}