Skip to content

Commit 1c7ba41

Browse files
Refactor
1 parent a5ba7cb commit 1c7ba41

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

src/CodeCoverage.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ final class CodeCoverage
138138
*/
139139
public function __construct(Driver $driver = null, Filter $filter = null)
140140
{
141-
if ($driver === null) {
142-
$driver = $this->selectDriver();
143-
}
144-
145141
if ($filter === null) {
146142
$filter = new Filter;
147143
}
148144

145+
if ($driver === null) {
146+
$driver = $this->selectDriver($filter);
147+
}
148+
149149
$this->driver = $driver;
150150
$this->filter = $filter;
151151

@@ -878,7 +878,7 @@ private function getAllowedLines(array $linesToBeCovered, array $linesToBeUsed):
878878
/**
879879
* @throws RuntimeException
880880
*/
881-
private function selectDriver(): Driver
881+
private function selectDriver(Filter $filter): Driver
882882
{
883883
$runtime = new Runtime;
884884

@@ -891,7 +891,7 @@ private function selectDriver(): Driver
891891
}
892892

893893
if ($runtime->hasXdebug()) {
894-
return new Xdebug;
894+
return new Xdebug($filter);
895895
}
896896

897897
throw new RuntimeException('No code coverage driver available');

src/Driver/Xdebug.php

+21-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\Driver;
1111

12+
use SebastianBergmann\CodeCoverage\Filter;
1213
use SebastianBergmann\CodeCoverage\RuntimeException;
1314

1415
/**
@@ -23,10 +24,15 @@ final class Xdebug implements Driver
2324
*/
2425
private $cacheNumLines = [];
2526

27+
/**
28+
* @var Filter
29+
*/
30+
private $filter;
31+
2632
/**
2733
* @throws RuntimeException
2834
*/
29-
public function __construct()
35+
public function __construct(Filter $filter = null)
3036
{
3137
if (!\extension_loaded('xdebug')) {
3238
throw new RuntimeException('This driver requires Xdebug');
@@ -35,6 +41,12 @@ public function __construct()
3541
if (!\ini_get('xdebug.coverage_enable')) {
3642
throw new RuntimeException('xdebug.coverage_enable=On has to be set in php.ini');
3743
}
44+
45+
if ($filter === null) {
46+
$filter = new Filter;
47+
}
48+
49+
$this->filter = $filter;
3850
}
3951

4052
/**
@@ -66,13 +78,15 @@ private function cleanup(array $data): array
6678
foreach (\array_keys($data) as $file) {
6779
unset($data[$file][0]);
6880

69-
if (\strpos($file, 'xdebug://debug-eval') !== 0 && $file !== 'Standard input code' && \file_exists($file)) {
70-
$numLines = $this->getNumberOfLinesInFile($file);
81+
if (!$this->filter->isFile($file)) {
82+
continue;
83+
}
84+
85+
$numLines = $this->getNumberOfLinesInFile($file);
7186

72-
foreach (\array_keys($data[$file]) as $line) {
73-
if ($line > $numLines) {
74-
unset($data[$file][$line]);
75-
}
87+
foreach (\array_keys($data[$file]) as $line) {
88+
if ($line > $numLines) {
89+
unset($data[$file][$line]);
7690
}
7791
}
7892
}

src/Filter.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ public function isFile(string $filename): bool
102102
\strpos($filename, 'runtime-created function') !== false ||
103103
\strpos($filename, 'runkit created function') !== false ||
104104
\strpos($filename, 'assert code') !== false ||
105-
\strpos($filename, 'regexp code') !== false) {
105+
\strpos($filename, 'regexp code') !== false ||
106+
\strpos($filename, 'Standard input code') !== false) {
106107
$isFile = false;
107108
} else {
108109
$isFile = \file_exists($filename);

0 commit comments

Comments
 (0)