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

Add omitdefaultvalue to fromArray options #136

Merged
merged 2 commits into from
Oct 20, 2017
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
2 changes: 2 additions & 0 deletions doc/book/generator/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ class Zend\Code\Generator\ParameterGenerator extends Zend\Code\Generator\Abstrac
public function getPassedByReference()
public function setPassedByReference($passedByReference)
public function generate()
public function omitDefaultValue()
}
```

Expand Down Expand Up @@ -399,5 +400,6 @@ class Zend\Code\Generator\PropertyGenerator
public function setDefaultValue($defaultValue)
public function getDefaultValue()
public function generate()
public function omitDefaultValue()
}
```
25 changes: 15 additions & 10 deletions src/Generator/ParameterGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@ public static function fromReflection(ParameterReflection $reflectionParameter)
/**
* Generate from array
*
* @configkey name string [required] Class Name
* @configkey type string
* @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
* @configkey passedbyreference bool
* @configkey position int
* @configkey sourcedirty bool
* @configkey indentation string
* @configkey sourcecontent string
* @configkey name string [required] Class Name
* @configkey type string
* @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
* @configkey passedbyreference bool
* @configkey position int
* @configkey sourcedirty bool
* @configkey indentation string
* @configkey sourcecontent string
* @configkey omitdefaultvalue bool
*
* @throws Exception\InvalidArgumentException
* @param array $array
Expand Down Expand Up @@ -136,6 +137,9 @@ public static function fromArray(array $array)
case 'sourcecontent':
$param->setSourceContent($value);
break;
case 'omitdefaultvalue':
$param->omitDefaultValue($value);
break;
}
}

Expand Down Expand Up @@ -413,11 +417,12 @@ private function generateTypeHint()
}

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

return $this;
}
Expand Down
25 changes: 15 additions & 10 deletions src/Generator/PropertyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ public static function fromReflection(PropertyReflection $reflectionProperty)
/**
* Generate from array
*
* @configkey name string [required] Class Name
* @configkey const bool
* @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
* @configkey flags int
* @configkey abstract bool
* @configkey final bool
* @configkey static bool
* @configkey visibility string
* @configkey name string [required] Class Name
* @configkey const bool
* @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
* @configkey flags int
* @configkey abstract bool
* @configkey final bool
* @configkey static bool
* @configkey visibility string
* @configkey omitdefaultvalue bool
*
* @throws Exception\InvalidArgumentException
* @param array $array
Expand Down Expand Up @@ -122,6 +123,9 @@ public static function fromArray(array $array)
case 'visibility':
$property->setVisibility($value);
break;
case 'omitdefaultvalue':
$property->omitDefaultValue($value);
break;
}
}

Expand Down Expand Up @@ -239,11 +243,12 @@ public function generate()
}

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

return $this;
}
Expand Down
2 changes: 2 additions & 0 deletions test/Generator/ParameterGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public function testCreateFromArray()
'sourcedirty' => false,
'sourcecontent' => 'foo',
'indentation' => '-',
'omitdefaultvalue' => true,
]);

self::assertEquals('SampleParameter', $parameterGenerator->getName());
Expand All @@ -227,6 +228,7 @@ public function testCreateFromArray()
self::assertFalse($parameterGenerator->isSourceDirty());
self::assertEquals('foo', $parameterGenerator->getSourceContent());
self::assertEquals('-', $parameterGenerator->getIndentation());
self::assertAttributeEquals(true, 'omitDefaultValue', $parameterGenerator);
}

/**
Expand Down
18 changes: 10 additions & 8 deletions test/Generator/PropertyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,17 @@ public function testOtherTypesThrowExceptionOnGenerate() : void
public function testCreateFromArray() : void
{
$propertyGenerator = PropertyGenerator::fromArray([
'name' => 'SampleProperty',
'const' => true,
'defaultvalue' => 'foo',
'docblock' => [
'name' => 'SampleProperty',
'const' => true,
'defaultvalue' => 'foo',
'docblock' => [
'shortdescription' => 'foo',
],
'abstract' => true,
'final' => true,
'static' => true,
'visibility' => PropertyGenerator::VISIBILITY_PROTECTED,
'abstract' => true,
'final' => true,
'static' => true,
'visibility' => PropertyGenerator::VISIBILITY_PROTECTED,
'omitdefaultvalue' => true,
]);

self::assertEquals('SampleProperty', $propertyGenerator->getName());
Expand All @@ -243,6 +244,7 @@ public function testCreateFromArray() : void
self::assertTrue($propertyGenerator->isFinal());
self::assertTrue($propertyGenerator->isStatic());
self::assertEquals(PropertyGenerator::VISIBILITY_PROTECTED, $propertyGenerator->getVisibility());
self::assertAttributeEquals(true, 'omitDefaultValue', $propertyGenerator);
}

/**
Expand Down