Skip to content

Commit

Permalink
Fix: Improve object cloning performance (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek authored Nov 6, 2023
1 parent 537a906 commit 6b79bf3
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/Internal/ClonableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

use function array_column;
use function array_combine;
use function array_fill;
use function array_intersect_key;
use function count;
use function get_object_vars;

/**
* @internal
Expand All @@ -23,21 +27,11 @@ trait ClonableTrait
*/
private function clone(array $overridenProps = []): self
{
static $refl, $properties, $constructorParams;
static $refl, $constructorParams, $emptyProps;
$refl ??= new ReflectionClass($this);
$properties ??= array_combine(array_column($refl->getProperties(), 'name'), $refl->getProperties());
$constructorParams ??= $refl->getConstructor()->getParameters();
$constructorParams ??= array_column($refl->getConstructor()->getParameters(), 'name');
$emptyProps ??= array_combine($constructorParams, array_fill(0, count($constructorParams), null));

$args = (function () use ($properties, $constructorParams) {
foreach ($constructorParams as $param) {
$key = $param->getName();
yield $key => $properties[$key]->getValue($this);
}
})();

return new self(...[
...$args,
...$overridenProps,
]);
return new self(...($overridenProps + array_intersect_key(get_object_vars($this), $emptyProps)));
}
}

0 comments on commit 6b79bf3

Please sign in to comment.