| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of phpunit/php-code-coverage.
|
|
|
4 |
*
|
|
|
5 |
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
namespace SebastianBergmann\CodeCoverage\Driver;
|
|
|
11 |
|
|
|
12 |
use const PHP_SAPI;
|
|
|
13 |
use const PHP_VERSION;
|
|
|
14 |
use function array_diff;
|
|
|
15 |
use function array_keys;
|
|
|
16 |
use function array_merge;
|
|
|
17 |
use function get_included_files;
|
|
|
18 |
use function phpdbg_end_oplog;
|
|
|
19 |
use function phpdbg_get_executable;
|
|
|
20 |
use function phpdbg_start_oplog;
|
|
|
21 |
use SebastianBergmann\CodeCoverage\RawCodeCoverageData;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
|
|
25 |
*/
|
|
|
26 |
final class PhpdbgDriver extends Driver
|
|
|
27 |
{
|
|
|
28 |
/**
|
|
|
29 |
* @throws PhpdbgNotAvailableException
|
|
|
30 |
*/
|
|
|
31 |
public function __construct()
|
|
|
32 |
{
|
|
|
33 |
if (PHP_SAPI !== 'phpdbg') {
|
|
|
34 |
throw new PhpdbgNotAvailableException;
|
|
|
35 |
}
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
public function start(): void
|
|
|
39 |
{
|
|
|
40 |
phpdbg_start_oplog();
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
public function stop(): RawCodeCoverageData
|
|
|
44 |
{
|
|
|
45 |
static $fetchedLines = [];
|
|
|
46 |
|
|
|
47 |
$dbgData = phpdbg_end_oplog();
|
|
|
48 |
|
|
|
49 |
if ($fetchedLines === []) {
|
|
|
50 |
$sourceLines = phpdbg_get_executable();
|
|
|
51 |
} else {
|
|
|
52 |
$newFiles = array_diff(get_included_files(), array_keys($fetchedLines));
|
|
|
53 |
|
|
|
54 |
$sourceLines = [];
|
|
|
55 |
|
|
|
56 |
if ($newFiles) {
|
|
|
57 |
$sourceLines = phpdbg_get_executable(['files' => $newFiles]);
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
foreach ($sourceLines as $file => $lines) {
|
|
|
62 |
foreach ($lines as $lineNo => $numExecuted) {
|
|
|
63 |
$sourceLines[$file][$lineNo] = self::LINE_NOT_EXECUTED;
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
$fetchedLines = array_merge($fetchedLines, $sourceLines);
|
|
|
68 |
|
|
|
69 |
return RawCodeCoverageData::fromXdebugWithoutPathCoverage(
|
|
|
70 |
$this->detectExecutedLines($fetchedLines, $dbgData)
|
|
|
71 |
);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
public function nameAndVersion(): string
|
|
|
75 |
{
|
|
|
76 |
return 'PHPDBG ' . PHP_VERSION;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
private function detectExecutedLines(array $sourceLines, array $dbgData): array
|
|
|
80 |
{
|
|
|
81 |
foreach ($dbgData as $file => $coveredLines) {
|
|
|
82 |
foreach ($coveredLines as $lineNo => $numExecuted) {
|
|
|
83 |
// phpdbg also reports $lineNo=0 when e.g. exceptions get thrown.
|
|
|
84 |
// make sure we only mark lines executed which are actually executable.
|
|
|
85 |
if (isset($sourceLines[$file][$lineNo])) {
|
|
|
86 |
$sourceLines[$file][$lineNo] = self::LINE_EXECUTED;
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
return $sourceLines;
|
|
|
92 |
}
|
|
|
93 |
}
|