Skip to content

Commit 8674ce8

Browse files
committed
added parameter $overwrite to addMember(), addMethod(), addProperty(), addConstant(), addCase() [Closes #152]
1 parent c02c065 commit 8674ce8

File tree

9 files changed

+34
-16
lines changed

9 files changed

+34
-16
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public function removeImplement(string $name): static
177177
}
178178

179179

180-
public function addMember(Method|Property|Constant|TraitUse $member): static
180+
public function addMember(Method|Property|Constant|TraitUse $member, bool $overwrite = false): static
181181
{
182182
$name = $member->getName();
183183
[$type, $n] = match (true) {
@@ -186,7 +186,7 @@ public function addMember(Method|Property|Constant|TraitUse $member): static
186186
$member instanceof Property => ['properties', $name],
187187
$member instanceof TraitUse => ['traits', $name],
188188
};
189-
if (isset($this->$type[$n])) {
189+
if (!$overwrite && isset($this->$type[$n])) {
190190
throw new Nette\InvalidStateException("Cannot add member '$name', because it already exists.");
191191
}
192192
$this->$type[$n] = $member;

src/PhpGenerator/EnumType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ public function getCases(): array
9999

100100

101101
/** Adds case to enum */
102-
public function addCase(string $name, string|int|Literal|null $value = null): EnumCase
102+
public function addCase(string $name, string|int|Literal|null $value = null, bool $overwrite = false): EnumCase
103103
{
104-
if (isset($this->cases[$name])) {
104+
if (!$overwrite && isset($this->cases[$name])) {
105105
throw new Nette\InvalidStateException("Cannot add cases '$name', because it already exists.");
106106
}
107107
return $this->cases[$name] = (new EnumCase($name))
@@ -116,7 +116,7 @@ public function removeCase(string $name): static
116116
}
117117

118118

119-
public function addMember(Method|Constant|EnumCase|TraitUse $member): static
119+
public function addMember(Method|Constant|EnumCase|TraitUse $member, bool $overwrite = false): static
120120
{
121121
$name = $member->getName();
122122
[$type, $n] = match (true) {
@@ -125,7 +125,7 @@ public function addMember(Method|Constant|EnumCase|TraitUse $member): static
125125
$member instanceof TraitUse => ['traits', $name],
126126
$member instanceof EnumCase => ['cases', $name],
127127
};
128-
if (isset($this->$type[$n])) {
128+
if (!$overwrite && isset($this->$type[$n])) {
129129
throw new Nette\InvalidStateException("Cannot add member '$name', because it already exists.");
130130
}
131131
$this->$type[$n] = $member;

src/PhpGenerator/InterfaceType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ public function addExtend(string $name): static
5151
}
5252

5353

54-
public function addMember(Method|Constant $member): static
54+
public function addMember(Method|Constant $member, bool $overwrite = false): static
5555
{
5656
$name = $member->getName();
5757
[$type, $n] = match (true) {
5858
$member instanceof Constant => ['consts', $name],
5959
$member instanceof Method => ['methods', strtolower($name)],
6060
};
61-
if (isset($this->$type[$n])) {
61+
if (!$overwrite && isset($this->$type[$n])) {
6262
throw new Nette\InvalidStateException("Cannot add member '$name', because it already exists.");
6363
}
6464
$this->$type[$n] = $member;

src/PhpGenerator/TraitType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class TraitType extends ClassLike
2222
use Traits\PropertiesAware;
2323
use Traits\TraitsAware;
2424

25-
public function addMember(Method|Property|Constant|TraitUse $member): static
25+
public function addMember(Method|Property|Constant|TraitUse $member, bool $overwrite = false): static
2626
{
2727
$name = $member->getName();
2828
[$type, $n] = match (true) {
@@ -31,7 +31,7 @@ public function addMember(Method|Property|Constant|TraitUse $member): static
3131
$member instanceof Property => ['properties', $name],
3232
$member instanceof TraitUse => ['traits', $name],
3333
};
34-
if (isset($this->$type[$n])) {
34+
if (!$overwrite && isset($this->$type[$n])) {
3535
throw new Nette\InvalidStateException("Cannot add member '$name', because it already exists.");
3636
}
3737
$this->$type[$n] = $member;

src/PhpGenerator/Traits/ConstantsAware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public function getConstant(string $name): Constant
4848
}
4949

5050

51-
public function addConstant(string $name, mixed $value): Constant
51+
public function addConstant(string $name, mixed $value, bool $overwrite = false): Constant
5252
{
53-
if (isset($this->consts[$name])) {
53+
if (!$overwrite && isset($this->consts[$name])) {
5454
throw new Nette\InvalidStateException("Cannot add constant '$name', because it already exists.");
5555
}
5656
return $this->consts[$name] = (new Constant($name))

src/PhpGenerator/Traits/MethodsAware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public function getMethod(string $name): Method
5353
}
5454

5555

56-
public function addMethod(string $name): Method
56+
public function addMethod(string $name, bool $overwrite = false): Method
5757
{
5858
$lower = strtolower($name);
59-
if (isset($this->methods[$lower])) {
59+
if (!$overwrite && isset($this->methods[$lower])) {
6060
throw new Nette\InvalidStateException("Cannot add method '$name', because it already exists.");
6161
}
6262
$method = new Method($name);

src/PhpGenerator/Traits/PropertiesAware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public function getProperty(string $name): Property
4949

5050

5151
/** @param string $name without $ */
52-
public function addProperty(string $name, mixed $value = null): Property
52+
public function addProperty(string $name, mixed $value = null, bool $overwrite = false): Property
5353
{
54-
if (isset($this->properties[$name])) {
54+
if (!$overwrite && isset($this->properties[$name])) {
5555
throw new Nette\InvalidStateException("Cannot add property '$name', because it already exists.");
5656
}
5757
return $this->properties[$name] = func_num_args() > 1

tests/PhpGenerator/ClassType.addMember.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ Assert::exception(
2929
Nette\InvalidStateException::class,
3030
"Cannot add member 'FOO', because it already exists.",
3131
);
32+
33+
$class->addMember($new = new Nette\PhpGenerator\Method('FOO'), overwrite: true);
34+
Assert::same($new, $class->getMethod('FOO'));

tests/PhpGenerator/ClassType.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,21 @@ Assert::exception(
217217
);
218218

219219

220+
// overwrite
221+
$class = new ClassType('Example');
222+
$class->addConstant('a', 1);
223+
$new = $class->addConstant('a', 1, overwrite: true);
224+
Assert::same($new, $class->getConstant('a'));
225+
226+
$class->addProperty('a');
227+
$new = $class->addProperty('a', overwrite: true);
228+
Assert::same($new, $class->getProperty('a'));
229+
230+
$class->addMethod('a');
231+
$new = $class->addMethod('a', overwrite: true);
232+
Assert::same($new, $class->getMethod('a'));
233+
234+
220235
// remove members
221236
$class = new ClassType('Example');
222237
$class->addConstant('a', 1);

0 commit comments

Comments
 (0)