Completed
Push — master ( 7209dc...45a6f3 )
by Alessandro
07:05
created

CoverageFetcher   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.1%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 75
ccs 27
cts 29
cp 0.931
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetch() 0 16 3
A coverageFileIsValid() 0 17 3
A overrideCoverageClassDefinition() 0 12 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 Symfony\Component\Process\Exception\RuntimeException;
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
    /**
21
     * CoverageFetcher constructor.
22
     * @param TempFilenameFactory $tempFilenameFactory
23
     */
24 4
    public function __construct(TempFilenameFactory $tempFilenameFactory)
25
    {
26 4
        $this->tempFilenameFactory = $tempFilenameFactory;
27 4
    }
28
29
    /**
30
     * @param AbstractParaunitProcess $process
31
     * @return CodeCoverage
32
     */
33 4
    public function fetch(AbstractParaunitProcess $process)
34
    {
35 4
        $tempFilename = $this->tempFilenameFactory->getFilenameForCoverage($process->getUniqueId());
36 4
        $codeCoverage = null;
37
38 4
        if ($this->coverageFileIsValid($tempFilename)) {
39 3
            $codeCoverage = require $tempFilename;
40 3
            unlink($tempFilename);
41 3
        }
42
43 4
        if ($codeCoverage instanceof CodeCoverage) {
44 2
            return $codeCoverage;
45
        }
46
47 2
        return new CodeCoverage();
48
    }
49
50
    /**
51
     * @param string $tempFilename
52
     * @return bool
53
     */
54 4
    private function coverageFileIsValid($tempFilename)
55
    {
56 4
        if (! file_exists($tempFilename)) {
57 1
            return false;
58
        }
59
60
        try {
61 3
            $this->overrideCoverageClassDefinition($tempFilename);
62
63 3
            $verificationProcess = new Process('php --syntax-check ' . $tempFilename);
64 3
            $verificationProcess->run();
65
66 3
            return $verificationProcess->getExitCode() === 0;
67
        } catch (\Exception $e) {
68
            return false;
69
        }
70
    }
71
72
    /**
73
     * @param string $tempFilename
74
     * @return bool
75
     * @throws RuntimeException
76
     */
77 3
    private function overrideCoverageClassDefinition($tempFilename)
78
    {
79 3
        $className1 = 'new SebastianBergmann\\\\CodeCoverage\\\\CodeCoverage';
80 3
        $className2 = 'new PHP_CodeCoverage';
81 3
        $newClassName = 'new Paraunit\\\\Proxy\\\\Coverage\\\\CodeCoverage';
82
        
83 3
        $commandLine = "sed -r -i -e 's/($className1|$className2)/$newClassName/' $tempFilename";
84
85 3
        $editProcess = new Process($commandLine);
86
87 3
        return $editProcess->run() === 0;
88
    }
89
}
90