MemoryGuard::endTest()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.6111
cc 5
nc 4
nop 2
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @package   phpunit-garbage-collector
7
 * @version   1.0.0
8
 * @author    John Rayes <[email protected]>
9
 * @copyright Copyright (c) 2022, John Rayes
10
 * @license   http://opensource.org/licenses/MIT MIT
11
 */
12
13
namespace live627\PHPUnitGarbageCollector;
14
15
use PHPUnit\Framework\Test;
16
use PHPUnit\Framework\TestListener;
17
use PHPUnit\Framework\TestListenerDefaultImplementation;
18
use ReflectionObject;
19
20
class MemoryGuard implements TestListener
0 ignored issues
show
Deprecated Code introduced by
The interface PHPUnit\Framework\TestListener has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

20
class MemoryGuard implements /** @scrutinizer ignore-deprecated */ TestListener

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
21
{
22
	use TestListenerDefaultImplementation;
0 ignored issues
show
Deprecated Code introduced by
The trait PHPUnit\Framework\TestLi...erDefaultImplementation has been deprecated: The `TestListener` interface is deprecated ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

22
	use /** @scrutinizer ignore-deprecated */ TestListenerDefaultImplementation;

This trait has been deprecated. The supplier of the trait has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the trait will be removed and what other trait to use instead.

Loading history...
23
24
	private IgnoreTestPolicy $ignorePolicy;
25
26 2
	public function __construct(IgnoreTestPolicy $ignorePolicy = null)
27
	{
28 2
		$this->ignorePolicy = $ignorePolicy ?: new NeverIgnoreTestPolicy;
29
	}
30
31 2
	public function endTest(Test $test, float $time): void
32
	{
33 2
		$testReflection = new ReflectionObject($test);
34
35 2
		if ($this->ignorePolicy->shouldIgnore($testReflection))
36 1
			return;
37
38 1
			foreach ($testReflection->getProperties() as $prop)
39
			{
40 1
				if ($prop->isStatic() || strpos($prop->getDeclaringClass()->getName(), 'PHPUnit\\') === 0)
41 1
					continue;
42
43 1
				unset($test->{$prop->getName()});
44
			}
45
	}
46
}
47
48
class NeverIgnoreTestPolicy implements IgnoreTestPolicy
49
{
50 1
	public function shouldIgnore(ReflectionObject $testReflection): bool
51
	{
52 1
		return false;
53
	}
54
}
55