Completed
Push — master ( 3f2b21...64f10e )
by Alessandro
06:51
created

CoverageFetcher::coverageFileIsValid()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 7
cts 9
cp 0.7778
rs 9.4285
cc 3
eloc 10
nc 6
nop 1
crap 3.0987
1
<?php
2
3
namespace Paraunit\Coverage;
4
5
use Paraunit\Configuration\TempFilenameFactory;
6
use Paraunit\Process\AbstractParaunitProcess;
7
use Paraunit\Proxy\Coverage\CodeCoverage;
8
use Paraunit\TestResult\Interfaces\TestResultHandlerInterface;
9
use Symfony\Component\Process\Process;
10
11
/**
12
 * Class CoverageFetcher
13
 * @package Paraunit\Coverage
14
 */
15
class CoverageFetcher
16
{
17
    /** @var  TempFilenameFactory */
18
    private $tempFilenameFactory;
19
20
    /** @var TestResultHandlerInterface */
21
    private $resultHandler;
22
23
    /**
24
     * CoverageFetcher constructor.
25
     * @param TempFilenameFactory $tempFilenameFactory
26
     * @param TestResultHandlerInterface $failureHandler
27
     */
28 5
    public function __construct(TempFilenameFactory $tempFilenameFactory, TestResultHandlerInterface $failureHandler)
29
    {
30 5
        $this->tempFilenameFactory = $tempFilenameFactory;
31 5
        $this->resultHandler = $failureHandler;
32 5
    }
33
34
    /**
35
     * @param AbstractParaunitProcess $process
36
     * @return CodeCoverage
37
     */
38 5
    public function fetch(AbstractParaunitProcess $process)
39
    {
40 5
        $tempFilename = $this->tempFilenameFactory->getFilenameForCoverage($process->getUniqueId());
41 5
        $codeCoverage = null;
42
43 5
        if ($this->coverageFileIsValid($tempFilename)) {
44 4
            $codeCoverage = require $tempFilename;
45 4
            unlink($tempFilename);
46 4
        }
47
48 5
        if ($codeCoverage instanceof CodeCoverage) {
49 3
            return $codeCoverage;
50
        }
51
52 2
        $this->resultHandler->addProcessToFilenames($process);
53
54 2
        return new CodeCoverage();
55
    }
56
57
    /**
58
     * @param string $tempFilename
59
     * @return bool
60
     */
61 5
    private function coverageFileIsValid($tempFilename)
62
    {
63 5
        if (! file_exists($tempFilename)) {
64 1
            return false;
65
        }
66
67
        try {
68 4
            $this->overrideCoverageClassDefinition($tempFilename);
69
70 4
            $verificationProcess = new Process('php --syntax-check ' . $tempFilename);
71 4
            $verificationProcess->run();
72
73 4
            return $verificationProcess->getExitCode() === 0;
74
        } catch (\Exception $e) {
75
            return false;
76
        }
77
    }
78
79
    /**
80
     * @param string $tempFilename
81
     */
82 4
    private function overrideCoverageClassDefinition($tempFilename)
83
    {
84 4
        $fileContent = str_replace(
85
            array(
86 4
                'new SebastianBergmann\CodeCoverage\CodeCoverage',
87
                'new PHP_CodeCoverage'
88 4
            ),
89 4
            'new Paraunit\Proxy\Coverage\CodeCoverage',
90 4
            file_get_contents($tempFilename)
91 4
        );
92
93 4
        file_put_contents($tempFilename, $fileContent);
94 4
    }
95
}
96