Skip to content

Commit f055a0e

Browse files
committed
ClassType: cloning includes attributes and parameters
1 parent 78dcebd commit f055a0e

File tree

9 files changed

+37
-5
lines changed

9 files changed

+37
-5
lines changed

src/PhpGenerator/ClassLike.php

+6
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,10 @@ protected function validateNames(array $names): void
140140
public function validate(): void
141141
{
142142
}
143+
144+
145+
public function __clone(): void
146+
{
147+
$this->attributes = array_map(fn($attr) => clone $attr, $this->attributes);
148+
}
143149
}

src/PhpGenerator/ClassType.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,9 @@ public function validate(): void
260260
}
261261

262262

263-
public function __clone()
263+
public function __clone(): void
264264
{
265+
parent::__clone();
265266
$clone = fn($item) => clone $item;
266267
$this->consts = array_map($clone, $this->consts);
267268
$this->methods = array_map($clone, $this->methods);

src/PhpGenerator/Closure.php

+6
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,10 @@ public function addUse(string $name): Parameter
5757
{
5858
return $this->uses[] = new Parameter($name);
5959
}
60+
61+
62+
public function __clone(): void
63+
{
64+
$this->parameters = array_map(fn($param) => clone $param, $this->parameters);
65+
}
6066
}

src/PhpGenerator/EnumType.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ public function addMember(Method|Constant|EnumCase|TraitUse $member, bool $overw
136136
}
137137

138138

139-
public function __clone()
139+
public function __clone(): void
140140
{
141+
parent::__clone();
141142
$clone = fn($item) => clone $item;
142143
$this->consts = array_map($clone, $this->consts);
143144
$this->methods = array_map($clone, $this->methods);

src/PhpGenerator/GlobalFunction.php

+6
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ public function __toString(): string
3232
{
3333
return (new Printer)->printFunction($this);
3434
}
35+
36+
37+
public function __clone(): void
38+
{
39+
$this->parameters = array_map(fn($param) => clone $param, $this->parameters);
40+
}
3541
}

src/PhpGenerator/InterfaceType.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ public function addMember(Method|Constant $member, bool $overwrite = false): sta
6969
}
7070

7171

72-
public function __clone()
72+
public function __clone(): void
7373
{
74+
parent::__clone();
7475
$clone = fn($item) => clone $item;
7576
$this->consts = array_map($clone, $this->consts);
7677
$this->methods = array_map($clone, $this->methods);

src/PhpGenerator/Method.php

+6
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,10 @@ public function validate(): void
100100
throw new Nette\InvalidStateException("Method $this->name() cannot be abstract and final or private at the same time.");
101101
}
102102
}
103+
104+
105+
public function __clone(): void
106+
{
107+
$this->parameters = array_map(fn($param) => clone $param, $this->parameters);
108+
}
103109
}

src/PhpGenerator/TraitType.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ public function addMember(Method|Property|Constant|TraitUse $member, bool $overw
4242
}
4343

4444

45-
public function __clone()
45+
public function __clone(): void
4646
{
47+
parent::__clone();
4748
$clone = fn($item) => clone $item;
4849
$this->consts = array_map($clone, $this->consts);
4950
$this->methods = array_map($clone, $this->methods);

tests/PhpGenerator/ClassType.clone.phpt

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ require __DIR__ . '/../bootstrap.php';
1010

1111
$class = new ClassType('Example');
1212

13+
$class->addAttribute('Attr');
1314
$class->addConstant('A', 10);
1415
$class->addProperty('a');
15-
$class->addMethod('a');
16+
$class->addMethod('a')
17+
->addParameter('foo');
1618

1719
$dolly = clone $class;
1820

21+
Assert::notSame($dolly->getAttributes(), $class->getAttributes());
1922
Assert::notSame($dolly->getConstants(), $class->getConstants());
2023
Assert::notSame($dolly->getProperty('a'), $class->getProperty('a'));
2124
Assert::notSame($dolly->getMethod('a'), $class->getMethod('a'));
25+
Assert::notSame($dolly->getMethod('a')->getParameter('foo'), $class->getMethod('a')->getParameter('foo'));

0 commit comments

Comments
 (0)