PcovDriver.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. use const pcov\inclusive;
  12. use function array_intersect;
  13. use function extension_loaded;
  14. use function pcov\clear;
  15. use function pcov\collect;
  16. use function pcov\start;
  17. use function pcov\stop;
  18. use function pcov\waiting;
  19. use function phpversion;
  20. use SebastianBergmann\CodeCoverage\Data\RawCodeCoverageData;
  21. use SebastianBergmann\CodeCoverage\Filter;
  22. /**
  23. * @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
  24. */
  25. final class PcovDriver extends Driver
  26. {
  27. private readonly Filter $filter;
  28. /**
  29. * @throws PcovNotAvailableException
  30. */
  31. public function __construct(Filter $filter)
  32. {
  33. $this->ensurePcovIsAvailable();
  34. $this->filter = $filter;
  35. }
  36. public function start(): void
  37. {
  38. start();
  39. }
  40. public function stop(): RawCodeCoverageData
  41. {
  42. stop();
  43. $filesToCollectCoverageFor = waiting();
  44. $collected = [];
  45. if ($filesToCollectCoverageFor) {
  46. if (!$this->filter->isEmpty()) {
  47. $filesToCollectCoverageFor = array_intersect($filesToCollectCoverageFor, $this->filter->files());
  48. }
  49. $collected = collect(inclusive, $filesToCollectCoverageFor);
  50. clear();
  51. }
  52. return RawCodeCoverageData::fromXdebugWithoutPathCoverage($collected);
  53. }
  54. public function nameAndVersion(): string
  55. {
  56. return 'PCOV ' . phpversion('pcov');
  57. }
  58. /**
  59. * @throws PcovNotAvailableException
  60. */
  61. private function ensurePcovIsAvailable(): void
  62. {
  63. if (!extension_loaded('pcov')) {
  64. throw new PcovNotAvailableException;
  65. }
  66. }
  67. }