From d7e10e40ee82556e663e2fc2a64133a1b814ff9b Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sat, 1 Feb 2020 23:43:46 -0500 Subject: [PATCH] Prefer PCOV for code coverage --- README.md | 2 +- RoboFile.php | 39 ++++++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 0658d785..f6932abe 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ There is also a `test:quick` Robo task which excludes slower tests, and a `test: ### Test coverage -Computing the coverage of tests can be done by running `./robo coverage`, after which an HTML-format coverage report will be written to `/tests/coverage/`. Either [Xdebug](https://xdebug.org) or [phpdbg](https://php.net/manual/en/book.phpdbg.php) is required for this. Xdebug is generally recommended as it is better maintained, though phpdbg is significantly faster. If using Xdebug, the extension need not be enabled globally; PHPUnit will enable it when needed. +Computing the coverage of tests can be done by running `./robo coverage`, after which an HTML-format coverage report will be written to `/tests/coverage/`. Either [PCOV](https://github.com/krakjoe/pcov), [Xdebug](https://xdebug.org), or [phpdbg](https://php.net/manual/en/book.phpdbg.php) is required for this. PCOV is generally recommended as it is faster than Xdebug; phpdbg is faster still, but less accurate. If using either PCOV or Xdebug, the extension need not be enabled globally; PHPUnit will enable it when needed. ## Enforcing coding style diff --git a/RoboFile.php b/RoboFile.php index f382090e..6a8472b6 100644 --- a/RoboFile.php +++ b/RoboFile.php @@ -53,9 +53,9 @@ class RoboFile extends \Robo\Tasks { * tests/coverage/. Additional reports may be produced by passing * arguments to this task as one would to PHPUnit. * - * Robo first tries to use phpdbg and will fall back to Xdebug if available. - * Because Xdebug slows down non-coverage tasks, however, phpdbg is highly - * recommended if debugging facilities are not otherwise needed. + * Robo first tries to use pcov and will fall back first to xdebug then + * phpdbg. Neither pcov nor xdebug need to be enabled to be used; they + * only need to be present in the extension load path to be used. */ public function coverage(array $args): Result { // run tests with code coverage reporting enabled @@ -89,17 +89,30 @@ class RoboFile extends \Robo\Tasks { } protected function findCoverageEngine(): string { - if (IS_WIN) { - $dbg = dirname(\PHP_BINARY)."\\phpdbg.exe"; - $dbg = file_exists($dbg) ? $dbg : ""; + $dir = rtrim(ini_get("extension_dir"), "/").\DIRECTORY_SEPARATOR; + $ext = IS_WIN ? "dll" : (IS_MAC ? "dylib" : "so"); + $php = escapeshellarg(\PHP_BINARY); + $code = escapeshellarg(BASE."lib"); + if (extension_loaded("pcov")) { + return "$php -d pcov.enabled=1 -d pcov.directory=$code"; + } elseif (extension_loaded("xdebug")) { + return $php; + } elseif (file_exists($dir."pcov.$ext")) { + return "$php -d extension=pcov.$ext -d pcov.enabled=1 -d pcov.directory=$code"; + } elseif (file_exists($dir."pcov.$ext")) { + return "$php -d zend_extension=xdebug.$ext"; } else { - $dbg = trim(`which phpdbg 2>/dev/null`); - } - if ($dbg) { - return escapeshellarg($dbg)." -qrr"; - } else { - $ext = IS_WIN ? "dll" : (IS_MAC ? "dylib" : "so"); - return escapeshellarg(\PHP_BINARY)." -d zend_extension=xdebug.$ext"; + if (IS_WIN) { + $dbg = dirname(\PHP_BINARY)."\\phpdbg.exe"; + $dbg = file_exists($dbg) ? $dbg : ""; + } else { + $dbg = trim(`which phpdbg 2>/dev/null`); + } + if ($dbg) { + return escapeshellarg($dbg)." -qrr"; + } else { + return $php; + } } }