88use ReflectionProperty ;
99
1010use function array_column ;
11- use function array_diff ;
1211use function array_filter ;
1312use function BenTools \ETL \array_fill_from ;
1413use 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