PHPDBG.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of the php-code-coverage package.
  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. use SebastianBergmann\CodeCoverage\RuntimeException;
  12. /**
  13. * Driver for PHPDBG's code coverage functionality.
  14. *
  15. * @codeCoverageIgnore
  16. */
  17. final class PHPDBG implements Driver
  18. {
  19. /**
  20. * @throws RuntimeException
  21. */
  22. public function __construct()
  23. {
  24. if (\PHP_SAPI !== 'phpdbg') {
  25. throw new RuntimeException(
  26. 'This driver requires the PHPDBG SAPI'
  27. );
  28. }
  29. if (!\function_exists('phpdbg_start_oplog')) {
  30. throw new RuntimeException(
  31. 'This build of PHPDBG does not support code coverage'
  32. );
  33. }
  34. }
  35. /**
  36. * Start collection of code coverage information.
  37. */
  38. public function start(bool $determineUnusedAndDead = true): void
  39. {
  40. \phpdbg_start_oplog();
  41. }
  42. /**
  43. * Stop collection of code coverage information.
  44. */
  45. public function stop(): array
  46. {
  47. static $fetchedLines = [];
  48. $dbgData = \phpdbg_end_oplog();
  49. if ($fetchedLines == []) {
  50. $sourceLines = \phpdbg_get_executable();
  51. } else {
  52. $newFiles = \array_diff(\get_included_files(), \array_keys($fetchedLines));
  53. $sourceLines = [];
  54. if ($newFiles) {
  55. $sourceLines = phpdbg_get_executable(['files' => $newFiles]);
  56. }
  57. }
  58. foreach ($sourceLines as $file => $lines) {
  59. foreach ($lines as $lineNo => $numExecuted) {
  60. $sourceLines[$file][$lineNo] = self::LINE_NOT_EXECUTED;
  61. }
  62. }
  63. $fetchedLines = \array_merge($fetchedLines, $sourceLines);
  64. return $this->detectExecutedLines($fetchedLines, $dbgData);
  65. }
  66. /**
  67. * Convert phpdbg based data into the format CodeCoverage expects
  68. */
  69. private function detectExecutedLines(array $sourceLines, array $dbgData): array
  70. {
  71. foreach ($dbgData as $file => $coveredLines) {
  72. foreach ($coveredLines as $lineNo => $numExecuted) {
  73. // phpdbg also reports $lineNo=0 when e.g. exceptions get thrown.
  74. // make sure we only mark lines executed which are actually executable.
  75. if (isset($sourceLines[$file][$lineNo])) {
  76. $sourceLines[$file][$lineNo] = self::LINE_EXECUTED;
  77. }
  78. }
  79. }
  80. return $sourceLines;
  81. }
  82. }