Skip to content

Commit f45bf62

Browse files
authored
Refactor: Improve ClonableTrait readability (#57)
1 parent d7da839 commit f45bf62

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/Internal/ClonableTrait.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use ReflectionProperty;
99

1010
use function array_column;
11-
use function array_diff;
1211
use function array_filter;
1312
use function BenTools\ETL\array_fill_from;
1413
use function get_object_vars;
@@ -27,17 +26,19 @@ trait ClonableTrait
2726
*/
2827
public function cloneWith(array $cloneArgs = []): static
2928
{
30-
static $refl, $writableProps, $writablePropNames, $constructorParamNames;
29+
static $refl, $notPromotedWritablePropNames, $constructorParamNames;
3130
$refl ??= new ReflectionClass($this);
3231
$constructorParamNames ??= array_column($refl->getConstructor()->getParameters(), 'name');
33-
$writableProps ??= array_filter(
34-
$refl->getProperties(),
35-
fn (ReflectionProperty $property) => !$property->isReadOnly(),
32+
$notPromotedWritablePropNames ??= array_column(
33+
array_filter(
34+
$refl->getProperties(),
35+
fn (ReflectionProperty $property) => !$property->isReadOnly() && !$property->isPromoted(),
36+
),
37+
'name'
3638
);
37-
$writablePropNames ??= array_diff(array_column($writableProps, 'name'), $constructorParamNames);
3839

3940
$clone = new static(...array_fill_from($constructorParamNames, get_object_vars($this), $cloneArgs));
40-
$notPromotedProps = array_fill_from($writablePropNames, get_object_vars($this), $cloneArgs);
41+
$notPromotedProps = array_fill_from($notPromotedWritablePropNames, get_object_vars($this), $cloneArgs);
4142
foreach ($notPromotedProps as $prop => $value) {
4243
$clone->{$prop} = $value;
4344
}

0 commit comments

Comments
 (0)