Skip to content

Commit ce787c0

Browse files
committed
ClassType: added removeMethod(), removeProperty(), removeConstant()
1 parent 0df8f10 commit ce787c0

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ It results in:
119119

120120
If the property, constant, method or parameter already exist, it will be overwritten.
121121

122+
Members can be removed using `removeProperty()`, `removeConstant()`, `removeMethod()` or `removeParameter()`.
123+
122124
PHP Generator supports all new PHP 7.2 features:
123125

124126
```php

src/PhpGenerator/ClassType.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,16 @@ public function addConstant(string $name, $value): Constant
369369
}
370370

371371

372+
/**
373+
* @return static
374+
*/
375+
public function removeConstant(string $name): self
376+
{
377+
unset($this->consts[$name]);
378+
return $this;
379+
}
380+
381+
372382
/**
373383
* @param Property[] $props
374384
* @return static
@@ -413,6 +423,17 @@ public function addProperty(string $name, $value = null): Property
413423
}
414424

415425

426+
/**
427+
* @param string $name without $
428+
* @return static
429+
*/
430+
public function removeProperty(string $name): self
431+
{
432+
unset($this->properties[$name]);
433+
return $this;
434+
}
435+
436+
416437
/**
417438
* @param Method[] $methods
418439
* @return static
@@ -460,6 +481,16 @@ public function addMethod(string $name): Method
460481
}
461482

462483

484+
/**
485+
* @return static
486+
*/
487+
public function removeMethod(string $name): self
488+
{
489+
unset($this->methods[$name]);
490+
return $this;
491+
}
492+
493+
463494
private function validate(array $names): void
464495
{
465496
foreach ($names as $name) {

tests/PhpGenerator/ClassType.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,24 @@ Assert::exception(function () {
123123
$class = new ClassType;
124124
$class->addMethod('method')->setVisibility('unknown');
125125
}, Nette\InvalidArgumentException::class, 'Argument must be public|protected|private.');
126+
127+
128+
// remove members
129+
$class = new ClassType('Example');
130+
$class->addConstant('a', 1);
131+
$class->addConstant('b', 1);
132+
$class->removeConstant('b')->removeConstant('c');
133+
134+
Assert::same(['a'], array_keys($class->getConstants()));
135+
136+
$class->addProperty('a');
137+
$class->addProperty('b');
138+
$class->removeProperty('b')->removeProperty('c');
139+
140+
Assert::same(['a'], array_keys($class->getProperties()));
141+
142+
$class->addMethod('a');
143+
$class->addMethod('b');
144+
$class->removeMethod('b')->removeMethod('c');
145+
146+
Assert::same(['a'], array_keys($class->getMethods()));

0 commit comments

Comments
 (0)