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

Commit e1808d1

Browse files
authored
Merge pull request #131 from janvernieuwe/add-omit-propery-value-type
Add omit property value type
2 parents 0294464 + e19867a commit e1808d1

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

src/Generator/ParameterGenerator.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class ParameterGenerator extends AbstractGenerator
4949
*/
5050
private $variadic = false;
5151

52+
/**
53+
* @var bool
54+
*/
55+
private $omitDefaultValue = false;
56+
5257
/**
5358
* @param ParameterReflection $reflectionParameter
5459
* @return ParameterGenerator
@@ -306,6 +311,10 @@ public function generate()
306311

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

314+
if ($this->omitDefaultValue) {
315+
return $output;
316+
}
317+
309318
if ($this->defaultValue !== null) {
310319
$output .= ' = ';
311320
if (is_string($this->defaultValue)) {
@@ -402,4 +411,14 @@ private function generateTypeHint()
402411

403412
return $this->type->generate() . ' ';
404413
}
414+
415+
/**
416+
* @return ParameterGenerator
417+
*/
418+
public function omitDefaultValue()
419+
{
420+
$this->omitDefaultValue = true;
421+
422+
return $this;
423+
}
405424
}

src/Generator/PropertyGenerator.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class PropertyGenerator extends AbstractMemberGenerator
2929
*/
3030
protected $defaultValue;
3131

32+
/**
33+
* @var bool
34+
*/
35+
private $omitDefaultValue = false;
36+
3237
/**
3338
* @param PropertyReflection $reflectionProperty
3439
* @return PropertyGenerator
@@ -220,14 +225,26 @@ public function generate()
220225
}
221226
$output .= $this->indentation . 'const ' . $name . ' = '
222227
. ($defaultValue !== null ? $defaultValue->generate() : 'null;');
223-
} else {
224-
$output .= $this->indentation
225-
. $this->getVisibility()
226-
. ($this->isStatic() ? ' static' : '')
227-
. ' $' . $name . ' = '
228-
. ($defaultValue !== null ? $defaultValue->generate() : 'null;');
228+
229+
return $output;
230+
}
231+
232+
$output .= $this->indentation . $this->getVisibility() . ($this->isStatic() ? ' static' : '') . ' $' . $name;
233+
234+
if ($this->omitDefaultValue) {
235+
return $output . ';';
229236
}
230237

231-
return $output;
238+
return $output . ' = ' . ($defaultValue !== null ? $defaultValue->generate() : 'null;');
239+
}
240+
241+
/**
242+
* @return PropertyGenerator
243+
*/
244+
public function omitDefaultValue()
245+
{
246+
$this->omitDefaultValue = true;
247+
248+
return $this;
232249
}
233250
}

test/Generator/ParameterGeneratorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,4 +582,12 @@ public function testGetInternalClassDefaultParameterValue()
582582

583583
self::assertSame('null', strtolower((string) $parameter->getDefaultValue()));
584584
}
585+
586+
public function testOmitType()
587+
{
588+
$parameter = new ParameterGenerator('foo', 'string', 'bar');
589+
$parameter->omitDefaultValue();
590+
591+
self::assertEquals('string $foo', $parameter->generate());
592+
}
585593
}

test/Generator/PropertyGeneratorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,12 @@ public function testSetDefaultValue(string $type, $value) : void
281281
self::assertEquals($type, $property->getDefaultValue()->getType());
282282
self::assertEquals($value, $property->getDefaultValue()->getValue());
283283
}
284+
285+
public function testOmitType()
286+
{
287+
$property = new PropertyGenerator('foo', null);
288+
$property->omitDefaultValue();
289+
290+
self::assertEquals(' public $foo;', $property->generate());
291+
}
284292
}

0 commit comments

Comments
 (0)