Skip to content

Commit 4193dfc

Browse files
committed
ClassType: deep clone
1 parent 82ae5fd commit 4193dfc

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,4 +442,13 @@ private function validate(array $names): void
442442
}
443443
}
444444
}
445+
446+
447+
public function __clone()
448+
{
449+
$clone = function ($item) { return clone $item; };
450+
$this->consts = array_map($clone, $this->consts);
451+
$this->properties = array_map($clone, $this->properties);
452+
$this->methods = array_map($clone, $this->methods);
453+
}
445454
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\PhpGenerator\ClassType;
6+
use Tester\Assert;
7+
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
$class = new ClassType('Example');
13+
14+
$class->addConstant('A', 10);
15+
$class->addProperty('a');
16+
$class->addMethod('a');
17+
18+
$dolly = clone $class;
19+
20+
Assert::notSame($dolly->getConstants(), $class->getConstants());
21+
Assert::notSame($dolly->getProperty('a'), $class->getProperty('a'));
22+
Assert::notSame($dolly->getMethod('a'), $class->getMethod('a'));

0 commit comments

Comments
 (0)