Completed
Pull Request — master (#94)
by Alessandro
04:33
created

CoverageFetcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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