Passed
Pull Request — master (#15)
by ANTHONIUS
03:07
created

Processor::setCurrentTestCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
crap 1.037
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the doyo/code-coverage project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Doyo\Bridge\CodeCoverage;
15
16
use SebastianBergmann\CodeCoverage\CodeCoverage;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Doyo\Bridge\CodeCoverage\CodeCoverage. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use SebastianBergmann\CodeCoverage\Driver\Driver;
18
use SebastianBergmann\CodeCoverage\Filter;
19
20
/**
21
 * Provide bridge to PHP Code Coverage.
22
 */
23
class Processor implements ProcessorInterface
24
{
25
    /**
26
     * @var CodeCoverage
27
     */
28
    private $codeCoverage;
29
30
    /**
31
     * @var TestCase[]
32
     */
33
    private $testCases = [];
34
35
    private $completed = false;
36
37
    /**
38
     * @var Driver
39
     */
40
    private $driver;
41
42
    /**
43
     * @var Filter
44
     */
45
    private $filter;
46
47
    /**
48
     * @var TestCase
49
     */
50
    private $currentTestCase;
51
52
    private $codeCoverageOptions = [];
53
54 7
    public function __construct(
55
        $driver = null,
56
        $filter = null,
57
        $codeCoverageOptions = []
58
    )
59
    {
60 7
        $this->driver = $driver;
61 7
        $this->filter = $filter;
62 7
        $this->codeCoverageOptions = $codeCoverageOptions;
63
    }
64
65 2
    public function setCurrentTestCase(TestCase $testCase)
66
    {
67 2
        $this->currentTestCase = $testCase;
68
    }
69
70 88
    public function getCurrentTestCase()
71
    {
72 88
        return $this->currentTestCase;
73
    }
74
75 1
    public function start(TestCase $testCase, $clear = false)
76
    {
77 1
        $this->setCurrentTestCase($testCase);
78 1
        $this->addTestCase($testCase);
79 1
        $this->getCodeCoverage()->start($testCase->getName(), $clear);
80
    }
81
82 88
    public function stop(bool $append = true, $linesToBeCovered = [], array $linesToBeUsed = [], bool $ignoreForceCoversAnnotation = false): array
83
    {
84 88
        return $this->getCodeCoverage()->stop($append, $linesToBeCovered, $linesToBeUsed, $ignoreForceCoversAnnotation);
85
    }
86
87
    public function merge($processor)
88
    {
89
        $codeCoverage = $processor;
90
        if ($processor instanceof self) {
91
            $codeCoverage = $processor->getCodeCoverage();
92
        }
93
        $this->getCodeCoverage()->merge($codeCoverage);
94
    }
95
96
    public function clear()
97
    {
98
        $this->getCodeCoverage()->clear();
99
    }
100
101 1
    public function setCodeCoverage(CodeCoverage $codeCoverage)
102
    {
103 1
        $this->codeCoverage = $codeCoverage;
104
    }
105
106
    /**
107
     * @return CodeCoverage
108
     */
109 88
    public function getCodeCoverage()
110
    {
111 88
        if (null === $this->codeCoverage) {
112 3
            $this->codeCoverage = new CodeCoverage($this->driver, $this->filter);
113 3
            $this->codeCoverage->setDisableIgnoredLines(true);
114
        }
115
116 88
        return $this->codeCoverage;
117
    }
118
119 2
    public function addTestCase(TestCase $testCase)
120
    {
121 2
        $this->testCases[$testCase->getName()] = $testCase;
122
    }
123
124 1
    public function complete()
125
    {
126 1
        $coverage  = $this->getCodeCoverage();
127 1
        $testCases = $this->testCases;
128 1
        $tests     = $coverage->getTests();
129
130 1
        foreach ($testCases as $testCase) {
131 1
            $name                   = $testCase->getName();
132 1
            $tests[$name]['status'] = $testCase->getResult();
133
        }
134
135 1
        $coverage->setTests($tests);
136 1
        $this->completed = true;
137
    }
138
}
139