NeverIgnoreTestPolicy   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
wmc 1
lcom 0
cbo 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A shouldIgnore() 0 4 1
1
<?php
2
3
namespace MyBuilder\PhpunitAccelerator;
4
5
use PHPUnit\Framework\TestListener as BaseTestListener;
6
use PHPUnit\Framework\TestListenerDefaultImplementation;
7
8
class TestListener implements BaseTestListener
9
{
10
    use TestListenerDefaultImplementation;
11
12
    private $ignorePolicy;
13
14
    const PHPUNIT_PROPERTY_PREFIX = 'PHPUnit_';
15
16
    public function __construct(IgnoreTestPolicy $ignorePolicy = null)
17
    {
18
        $this->ignorePolicy = ($ignorePolicy) ?: new NeverIgnoreTestPolicy();
19
    }
20
21
    public function endTest(\PHPUnit\Framework\Test $test, float $time): void
22
    {
23
        $testReflection = new \ReflectionObject($test);
24
25
        if ($this->ignorePolicy->shouldIgnore($testReflection)) {
26
            return;
27
        }
28
29
        $this->safelyFreeProperties($test, $testReflection->getProperties());
30
    }
31
32
    private function safelyFreeProperties(\PHPUnit\Framework\Test $test, array $properties)
33
    {
34
        foreach ($properties as $property) {
35
            if ($this->isSafeToFreeProperty($property)) {
36
                $this->freeProperty($test, $property);
37
            }
38
        }
39
    }
40
41
    private function isSafeToFreeProperty(\ReflectionProperty $property)
42
    {
43
        return !$property->isStatic() && $this->isNotPhpUnitProperty($property);
44
    }
45
46
    private function isNotPhpUnitProperty(\ReflectionProperty $property)
47
    {
48
        return 0 !== strpos($property->getDeclaringClass()->getName(), self::PHPUNIT_PROPERTY_PREFIX);
0 ignored issues
show
introduced by
Consider using $property->class. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
49
    }
50
51
    private function freeProperty(\PHPUnit\Framework\Test $test, \ReflectionProperty $property)
52
    {
53
        $property->setAccessible(true);
54
        $property->setValue($test, null);
55
    }
56
}
57
58
class NeverIgnoreTestPolicy implements IgnoreTestPolicy
59
{
60
    public function shouldIgnore(\ReflectionObject $testReflection)
61
    {
62
        return false;
63
    }
64
}
65