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

Commit f0f6e5f

Browse files
authored
Merge pull request #136 from janvernieuwe/omit-from-array
Add omitdefaultvalue to fromArray options
2 parents e1808d1 + bbe2d9e commit f0f6e5f

File tree

5 files changed

+44
-28
lines changed

5 files changed

+44
-28
lines changed

doc/book/generator/reference.md

+2
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ class Zend\Code\Generator\ParameterGenerator extends Zend\Code\Generator\Abstrac
358358
public function getPassedByReference()
359359
public function setPassedByReference($passedByReference)
360360
public function generate()
361+
public function omitDefaultValue()
361362
}
362363
```
363364

@@ -399,5 +400,6 @@ class Zend\Code\Generator\PropertyGenerator
399400
public function setDefaultValue($defaultValue)
400401
public function getDefaultValue()
401402
public function generate()
403+
public function omitDefaultValue()
402404
}
403405
```

src/Generator/ParameterGenerator.php

+15-10
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,15 @@ public static function fromReflection(ParameterReflection $reflectionParameter)
9090
/**
9191
* Generate from array
9292
*
93-
* @configkey name string [required] Class Name
94-
* @configkey type string
95-
* @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
96-
* @configkey passedbyreference bool
97-
* @configkey position int
98-
* @configkey sourcedirty bool
99-
* @configkey indentation string
100-
* @configkey sourcecontent string
93+
* @configkey name string [required] Class Name
94+
* @configkey type string
95+
* @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
96+
* @configkey passedbyreference bool
97+
* @configkey position int
98+
* @configkey sourcedirty bool
99+
* @configkey indentation string
100+
* @configkey sourcecontent string
101+
* @configkey omitdefaultvalue bool
101102
*
102103
* @throws Exception\InvalidArgumentException
103104
* @param array $array
@@ -136,6 +137,9 @@ public static function fromArray(array $array)
136137
case 'sourcecontent':
137138
$param->setSourceContent($value);
138139
break;
140+
case 'omitdefaultvalue':
141+
$param->omitDefaultValue($value);
142+
break;
139143
}
140144
}
141145

@@ -413,11 +417,12 @@ private function generateTypeHint()
413417
}
414418

415419
/**
420+
* @param bool $omit
416421
* @return ParameterGenerator
417422
*/
418-
public function omitDefaultValue()
423+
public function omitDefaultValue(bool $omit = true)
419424
{
420-
$this->omitDefaultValue = true;
425+
$this->omitDefaultValue = $omit;
421426

422427
return $this;
423428
}

src/Generator/PropertyGenerator.php

+15-10
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@ public static function fromReflection(PropertyReflection $reflectionProperty)
7272
/**
7373
* Generate from array
7474
*
75-
* @configkey name string [required] Class Name
76-
* @configkey const bool
77-
* @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
78-
* @configkey flags int
79-
* @configkey abstract bool
80-
* @configkey final bool
81-
* @configkey static bool
82-
* @configkey visibility string
75+
* @configkey name string [required] Class Name
76+
* @configkey const bool
77+
* @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
78+
* @configkey flags int
79+
* @configkey abstract bool
80+
* @configkey final bool
81+
* @configkey static bool
82+
* @configkey visibility string
83+
* @configkey omitdefaultvalue bool
8384
*
8485
* @throws Exception\InvalidArgumentException
8586
* @param array $array
@@ -122,6 +123,9 @@ public static function fromArray(array $array)
122123
case 'visibility':
123124
$property->setVisibility($value);
124125
break;
126+
case 'omitdefaultvalue':
127+
$property->omitDefaultValue($value);
128+
break;
125129
}
126130
}
127131

@@ -239,11 +243,12 @@ public function generate()
239243
}
240244

241245
/**
246+
* @param bool $omit
242247
* @return PropertyGenerator
243248
*/
244-
public function omitDefaultValue()
249+
public function omitDefaultValue(bool $omit = true)
245250
{
246-
$this->omitDefaultValue = true;
251+
$this->omitDefaultValue = $omit;
247252

248253
return $this;
249254
}

test/Generator/ParameterGeneratorTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ public function testCreateFromArray()
217217
'sourcedirty' => false,
218218
'sourcecontent' => 'foo',
219219
'indentation' => '-',
220+
'omitdefaultvalue' => true,
220221
]);
221222

222223
self::assertEquals('SampleParameter', $parameterGenerator->getName());
@@ -227,6 +228,7 @@ public function testCreateFromArray()
227228
self::assertFalse($parameterGenerator->isSourceDirty());
228229
self::assertEquals('foo', $parameterGenerator->getSourceContent());
229230
self::assertEquals('-', $parameterGenerator->getIndentation());
231+
self::assertAttributeEquals(true, 'omitDefaultValue', $parameterGenerator);
230232
}
231233

232234
/**

test/Generator/PropertyGeneratorTest.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -223,16 +223,17 @@ public function testOtherTypesThrowExceptionOnGenerate() : void
223223
public function testCreateFromArray() : void
224224
{
225225
$propertyGenerator = PropertyGenerator::fromArray([
226-
'name' => 'SampleProperty',
227-
'const' => true,
228-
'defaultvalue' => 'foo',
229-
'docblock' => [
226+
'name' => 'SampleProperty',
227+
'const' => true,
228+
'defaultvalue' => 'foo',
229+
'docblock' => [
230230
'shortdescription' => 'foo',
231231
],
232-
'abstract' => true,
233-
'final' => true,
234-
'static' => true,
235-
'visibility' => PropertyGenerator::VISIBILITY_PROTECTED,
232+
'abstract' => true,
233+
'final' => true,
234+
'static' => true,
235+
'visibility' => PropertyGenerator::VISIBILITY_PROTECTED,
236+
'omitdefaultvalue' => true,
236237
]);
237238

238239
self::assertEquals('SampleProperty', $propertyGenerator->getName());
@@ -243,6 +244,7 @@ public function testCreateFromArray() : void
243244
self::assertTrue($propertyGenerator->isFinal());
244245
self::assertTrue($propertyGenerator->isStatic());
245246
self::assertEquals(PropertyGenerator::VISIBILITY_PROTECTED, $propertyGenerator->getVisibility());
247+
self::assertAttributeEquals(true, 'omitDefaultValue', $propertyGenerator);
246248
}
247249

248250
/**

0 commit comments

Comments
 (0)