Completed
Pull Request — master (#62)
by Alessandro
05:54
created

CoverageFetcher   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 90.48%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
c 4
b 0
f 0
lcom 1
cbo 5
dl 0
loc 81
ccs 19
cts 21
cp 0.9048
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fetch() 0 18 3
A coverageFileIsValid() 0 17 3
A overrideCoverageClassDefinition() 0 13 1
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 3
    /**
24
     * CoverageFetcher constructor.
25 3
     * @param TempFilenameFactory $tempFilenameFactory
26 3
     * @param TestResultHandlerInterface $failureHandler
27
     */
28
    public function __construct(TempFilenameFactory $tempFilenameFactory, TestResultHandlerInterface $failureHandler)
29
    {
30
        $this->tempFilenameFactory = $tempFilenameFactory;
31
        $this->resultHandler = $failureHandler;
32 3
    }
33
34 3
    /**
35 3
     * @param AbstractParaunitProcess $process
36
     * @return CodeCoverage
37 3
     */
38 2
    public function fetch(AbstractParaunitProcess $process)
39 2
    {
40
        $tempFilename = $this->tempFilenameFactory->getFilenameForCoverage($process->getUniqueId());
41 3
        $codeCoverage = null;
42 1
43
        if ($this->coverageFileIsValid($tempFilename)) {
44
            $codeCoverage = require $tempFilename;
45 2
            unlink($tempFilename);
46
        }
47
48
        if ($codeCoverage instanceof CodeCoverage) {
49
            return $codeCoverage;
50
        }
51
52 3
        $this->resultHandler->addProcessToFilenames($process);
53
54 3
        return new CodeCoverage();
55 1
    }
56
57
    /**
58
     * @param string $tempFilename
59 2
     * @return bool
60 2
     */
61 2
    private function coverageFileIsValid($tempFilename)
62
    {
63 2
        if (! file_exists($tempFilename)) {
64
            return false;
65
        }
66
67
        try {
68
            $this->overrideCoverageClassDefinition($tempFilename);
69
70
            $verificationProcess = new Process('php --syntax-check ' . $tempFilename);
71
            $verificationProcess->run();
72
73
            return $verificationProcess->getExitCode() === 0;
74
        } catch (\Exception $e) {
75
            return false;
76
        }
77
    }
78
79
    /**
80
     * @param string $tempFilename
81
     */
82
    private function overrideCoverageClassDefinition($tempFilename)
83
    {
84
        $fileContent = str_replace(
85
            array(
86
                'new SebastianBergmann\CodeCoverage\CodeCoverage',
87
                'new PHP_CodeCoverage'
88
            ),
89
            'new Paraunit\Proxy\Coverage\CodeCoverage',
90
            file_get_contents($tempFilename)
91
        );
92
93
        file_put_contents($tempFilename, $fileContent);
94
    }
95
}
96