CoverageMerger::getCoverageObject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 3
cts 5
cp 0.6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2.2559
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ParaTest\Coverage;
6
7
use SebastianBergmann\CodeCoverage\CodeCoverage;
8
9
class CoverageMerger
10
{
11
    /**
12
     * @var CodeCoverage
13
     */
14
    private $coverage = null;
15
16
    /**
17
     * @param CodeCoverage $coverage
18
     */
19 10
    private function addCoverage(CodeCoverage $coverage)
20
    {
21 10
        if (null === $this->coverage) {
22 10
            $this->coverage = $coverage;
23
        } else {
24 8
            $this->coverage->merge($coverage);
0 ignored issues
show
Documentation introduced by
$coverage is of type object<SebastianBergmann...eCoverage\CodeCoverage>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
25
        }
26 10
    }
27
28
    /**
29
     * Returns coverage object from file.
30
     *
31
     * @param \SplFileObject $coverageFile coverage file
32
     *
33
     * @return CodeCoverage
34
     */
35 9
    private function getCoverageObject(\SplFileObject $coverageFile): CodeCoverage
36
    {
37 9
        if ('<?php' === $coverageFile->fread(5)) {
38 9
            return include $coverageFile->getRealPath();
39
        }
40
41
        $coverageFile->fseek(0);
42
        // the PHPUnit 3.x and below
43
        return unserialize($coverageFile->fread($coverageFile->getSize()));
44
    }
45
46
    /**
47
     * Adds the coverage contained in $coverageFile and deletes the file afterwards.
48
     *
49
     * @param string $coverageFile Code coverage file
50
     *
51
     * @throws \RuntimeException When coverage file is empty
52
     */
53 12
    public function addCoverageFromFile(string $coverageFile = null)
54
    {
55 12
        if ($coverageFile === null || !file_exists($coverageFile)) {
56 2
            return;
57
        }
58
59 10
        $file = new \SplFileObject($coverageFile);
60
61 10
        if (0 === $file->getSize()) {
62 1
            $extra = 'This means a PHPUnit process has crashed.';
63
64 1
            if (!function_exists('xdebug_get_code_coverage')) {
65
                $extra = 'Xdebug is disabled! Enable for coverage.';
66
            }
67
68 1
            throw new \RuntimeException(
69 1
                "Coverage file {$file->getRealPath()} is empty. " . $extra
70
            );
71
        }
72
73 9
        $this->addCoverage($this->getCoverageObject($file));
74
75 9
        unlink($file->getRealPath());
76 9
    }
77
78
    /**
79
     * Get coverage report generator.
80
     *
81
     * @return CoverageReporterInterface
82
     */
83 6
    public function getReporter(): CoverageReporterInterface
84
    {
85 6
        return new CoverageReporter($this->coverage);
86
    }
87
}
88