StrictCoverageListener   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
eloc 15
dl 0
loc 33
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B removeCoverageFor() 0 18 7
A addRiskyTest() 0 5 3
1
<?php
2
namespace Yoanm\PhpUnitExtended\Listener;
3
4
use PHPUnit\Framework\OutputError;
5
use PHPUnit\Framework\Test;
6
use PHPUnit\Framework\TestCase;
7
use PHPUnit\Framework\TestListener;
8
use PHPUnit\Framework\TestListenerDefaultImplementation;
9
10
/**
11
 * @see doc/listener/StrictCoverageListener.md
12
 */
13
class StrictCoverageListener 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

13
class StrictCoverageListener 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...
14
{
15
    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

15
    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...
16
17
    /**
18
     * {@inheritdoc}
19
     */
20 3
    public function addRiskyTest(Test $test, \Throwable $exception, float $time) : void
21
    {
22
        /* Must be PHPUnit\Framework\TestCase instance to have access to "getTestResultObject" method */
23 3
        if ($test instanceof TestCase && $exception instanceof OutputError) {
24 3
            $this->removeCoverageFor($test);
25
        }
26
    }
27
28 3
    protected function removeCoverageFor(TestCase $test)
29
    {
30 3
        $coverage = $test->getTestResultObject()->getCodeCoverage();
31 3
        if (null !== $coverage) {
32 3
            $id = $test->toString();
33 3
            $data = $coverage->getData()->lineCoverage();
34 3
            foreach ($data as $fileName => $lineData) {
35 3
                foreach ($lineData as $lineNumber => $testIdList) {
36 3
                    if (is_array($testIdList)) {
37 3
                        foreach ($testIdList as $testIdKey => $testId) {
38 3
                            if ($id === $testId) {
39 3
                                unset($data[$fileName][$lineNumber][$testIdKey]);
40
                            }
41
                        }
42
                    }
43
                }
44
            }
45 3
            $coverage->getData()->setLineCoverage($data);
46
        }
47
    }
48
}
49