From e431542449ecd7219cf2fc9d7ecd278ae7d281cf Mon Sep 17 00:00:00 2001 From: Ben Challis Date: Tue, 30 Apr 2024 09:34:04 +0100 Subject: [PATCH] Add property garbage collection --- src/PropertyGarbageCollection.php | 46 +++++++++++++++++++++++++++++++ src/TestCase.php | 1 + 2 files changed, 47 insertions(+) create mode 100644 src/PropertyGarbageCollection.php diff --git a/src/PropertyGarbageCollection.php b/src/PropertyGarbageCollection.php new file mode 100644 index 0000000..189fc59 --- /dev/null +++ b/src/PropertyGarbageCollection.php @@ -0,0 +1,46 @@ +getProperties() as $property) { + if ($property->isStatic() || \str_starts_with($property->getDeclaringClass()->getName(), 'PHPUnit\\')) { + continue; + } + + unset($this->{$property->getName()}); // @phpstan-ignore-line + } + + while (($parent = $reflection->getParentClass()) !== false) { + if (\str_starts_with($parent->getName(), 'PHPUnit\\')) { + break; + } + + foreach ($parent->getProperties() as $property) { + if ($property->isStatic() || \str_starts_with($property->getDeclaringClass()->getName(), 'PHPUnit\\')) { + continue; + } + + (function () use ($property): void { + unset($this->{$property->getName()}); // @phpstan-ignore-line + })->bindTo($this, $parent->getName())->call($this); + } + } + + \gc_collect_cycles(); + } +} diff --git a/src/TestCase.php b/src/TestCase.php index 2dd26f8..570c696 100644 --- a/src/TestCase.php +++ b/src/TestCase.php @@ -9,4 +9,5 @@ abstract class TestCase extends PHPUnitTestCase { use StrictMocking; + use PropertyGarbageCollection; }