Completed
Push — master ( 1a3a48...6e0e69 )
by Alessandro
09:39 queued 07:07
created

CoverageFetcher::overrideCoverageClassDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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