-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyGarbageCollection.php
More file actions
46 lines (36 loc) · 1.28 KB
/
PropertyGarbageCollection.php
File metadata and controls
46 lines (36 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
declare(strict_types=1);
namespace Lendable\PHPUnitExtensions;
use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\TestCase;
/**
* @mixin TestCase
*/
trait PropertyGarbageCollection
{
#[After]
final protected function unsetAllProperties(): void
{
$reflection = new \ReflectionObject($this);
foreach ($reflection->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();
}
}