Skip to content

Commit ba84161

Browse files
committed
Attributes: Reset callbacks upon clone
1 parent c131a28 commit ba84161

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/Attributes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,5 +609,10 @@ public function __clone()
609609
foreach ($this->attributes as &$attribute) {
610610
$attribute = clone $attribute;
611611
}
612+
613+
// Reset callbacks to avoid memory leaks
614+
$this->callbacks = [];
615+
$this->newCallbacks = [];
616+
$this->setterCallbacks = [];
612617
}
613618
}

tests/AttributesTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,4 +668,18 @@ public function testRenderThrowsIfLegacyAndNewAttributeCallbacksConflict(): void
668668

669669
$attributes->render();
670670
}
671+
672+
/**
673+
* @depends testRenderHandlesCallbackResultsCorrectly
674+
*/
675+
public function testCallbacksAreResetUponClone(): void
676+
{
677+
$attributes = (new Attributes())
678+
->setCallback('callback', fn() => 'foo')
679+
->registerAttributeCallback('callback2', fn() => 'bar');
680+
681+
$clone = clone $attributes;
682+
683+
$this->assertSame('', $clone->render());
684+
}
671685
}

0 commit comments

Comments
 (0)