Completed
Pull Request — master (#214)
by Mikhail
02:13
created

CoverageMerger::getReporter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2.0625
1
<?php
2
3
namespace ParaTest\Coverage;
4
5
use SebastianBergmann\CodeCoverage\CodeCoverage;
6
7
class CoverageMerger
8
{
9
    /**
10
     * @var \PHP_CodeCoverage|CodeCoverage
11
     */
12
    private $coverage = null;
13
14
    /**
15
     * @param \PHP_CodeCoverage|CodeCoverage $coverage
16
     */
17 2
    private function addCoverage($coverage)
18
    {
19 2
        if ($this->coverage == null) {
20 2
            $this->setCoverage($coverage);
21 2
        } else {
22 2
            $this->mergeCoverage($coverage);
23
        }
24 2
    }
25
26
    /**
27
     * @param \PHP_CodeCoverage|CodeCoverage $coverage
28
     */
29 2
    private function setCoverage($coverage)
30
    {
31 2
        $this->coverage = unserialize(serialize($coverage));
32 2
    }
33
34
    /**
35
     * @param \PHP_CodeCoverage|CodeCoverage $coverage
36
     */
37 2
    private function mergeCoverage($coverage)
38
    {
39 2
        $this->coverage->merge($coverage);
40 2
    }
41
42
    /**
43
     * Returns coverage object from file.
44
     *
45
     * @param string $coverageFile Coverage file.
46
     *
47
     * @return \PHP_CodeCoverage|CodeCoverage
48
     */
49 2
    private function getCoverageObject($coverageFile)
50
    {
51 2
        $coverage = file_get_contents($coverageFile);
52
53 2
        if (substr($coverage, 0, 5) === '<?php') {
54
            return include $coverageFile;
55
        }
56
57
        // the PHPUnit 3.x and below
58 2
        return unserialize($coverage);
59
    }
60
61
    /**
62
     * Adds the coverage contained in $coverageFile and deletes the file afterwards
63
     * @param string $coverageFile
64
     * @throws \RuntimeException When coverage file is empty
65
     */
66 2
    public function addCoverageFromFile($coverageFile)
67
    {
68 2
        if ($coverageFile === null || !file_exists($coverageFile)) {
69
            return;
70
        }
71
72 2
        if (filesize($coverageFile) === 0) {
73
            throw new \RuntimeException("Coverage file $coverageFile is empty. This means a PHPUnit process has crashed.");
74
        }
75
76 2
        $this->addCoverage($this->getCoverageObject($coverageFile));
77 2
        unlink($coverageFile);
78 2
    }
79
80
    /**
81
     * Get coverage report generator
82
     *
83
     * @return CoverageReporterInterface
84
     */
85 2
    public function getReporter()
86
    {
87 2
        if ($this->coverage instanceof \PHP_CodeCoverage) {
1 ignored issue
show
Bug introduced by
The class PHP_CodeCoverage does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
88 2
            return new CoverageReporterLegacy($this->coverage);
89
        }
90
91
        return new CoverageReporter($this->coverage);
92
    }
93
}
94