Skip to content

Commit

Permalink
ClassType: cloning includes attributes and parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 12, 2024
1 parent 49b353c commit 43731cd
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/PhpGenerator/ClassLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,10 @@ protected function validateNames(array $names): void
public function validate(): void
{
}


public function __clone(): void
{
$this->attributes = array_map(fn($attr) => clone $attr, $this->attributes);
}
}
3 changes: 2 additions & 1 deletion src/PhpGenerator/ClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,9 @@ public function validate(): void
}


public function __clone()
public function __clone(): void
{
parent::__clone();
$clone = fn($item) => clone $item;
$this->consts = array_map($clone, $this->consts);
$this->methods = array_map($clone, $this->methods);
Expand Down
6 changes: 6 additions & 0 deletions src/PhpGenerator/Closure.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ public function addUse(string $name): Parameter
{
return $this->uses[] = new Parameter($name);
}


public function __clone(): void
{
$this->parameters = array_map(fn($param) => clone $param, $this->parameters);
}
}
3 changes: 2 additions & 1 deletion src/PhpGenerator/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ public function addMember(Method|Constant|EnumCase|TraitUse $member, bool $overw
}


public function __clone()
public function __clone(): void
{
parent::__clone();
$clone = fn($item) => clone $item;
$this->consts = array_map($clone, $this->consts);
$this->methods = array_map($clone, $this->methods);
Expand Down
6 changes: 6 additions & 0 deletions src/PhpGenerator/GlobalFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ public function __toString(): string
{
return (new Printer)->printFunction($this);
}


public function __clone(): void
{
$this->parameters = array_map(fn($param) => clone $param, $this->parameters);
}
}
3 changes: 2 additions & 1 deletion src/PhpGenerator/InterfaceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ public function addMember(Method|Constant $member, bool $overwrite = false): sta
}


public function __clone()
public function __clone(): void
{
parent::__clone();
$clone = fn($item) => clone $item;
$this->consts = array_map($clone, $this->consts);
$this->methods = array_map($clone, $this->methods);
Expand Down
6 changes: 6 additions & 0 deletions src/PhpGenerator/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,10 @@ public function validate(): void
throw new Nette\InvalidStateException("Method $this->name() cannot be abstract and final or private at the same time.");
}
}


public function __clone(): void
{
$this->parameters = array_map(fn($param) => clone $param, $this->parameters);
}
}
3 changes: 2 additions & 1 deletion src/PhpGenerator/TraitType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public function addMember(Method|Property|Constant|TraitUse $member, bool $overw
}


public function __clone()
public function __clone(): void
{
parent::__clone();
$clone = fn($item) => clone $item;
$this->consts = array_map($clone, $this->consts);
$this->methods = array_map($clone, $this->methods);
Expand Down
6 changes: 5 additions & 1 deletion tests/PhpGenerator/ClassType.clone.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ require __DIR__ . '/../bootstrap.php';

$class = new ClassType('Example');

$class->addAttribute('Attr');
$class->addConstant('A', 10);
$class->addProperty('a');
$class->addMethod('a');
$class->addMethod('a')
->addParameter('foo');

$dolly = clone $class;

Assert::notSame($dolly->getAttributes(), $class->getAttributes());
Assert::notSame($dolly->getConstants(), $class->getConstants());
Assert::notSame($dolly->getProperty('a'), $class->getProperty('a'));
Assert::notSame($dolly->getMethod('a'), $class->getMethod('a'));
Assert::notSame($dolly->getMethod('a')->getParameter('foo'), $class->getMethod('a')->getParameter('foo'));

0 comments on commit 43731cd

Please sign in to comment.