Selector.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 SebastianBergmann\CodeCoverage\Filter;
  12. use SebastianBergmann\CodeCoverage\NoCodeCoverageDriverAvailableException;
  13. use SebastianBergmann\CodeCoverage\NoCodeCoverageDriverWithPathCoverageSupportAvailableException;
  14. use SebastianBergmann\Environment\Runtime;
  15. final class Selector
  16. {
  17. /**
  18. * @throws NoCodeCoverageDriverAvailableException
  19. * @throws PcovNotAvailableException
  20. * @throws XdebugNotAvailableException
  21. * @throws XdebugNotEnabledException
  22. */
  23. public function forLineCoverage(Filter $filter): Driver
  24. {
  25. $runtime = new Runtime;
  26. if ($runtime->hasPCOV()) {
  27. return new PcovDriver($filter);
  28. }
  29. if ($runtime->hasXdebug()) {
  30. $driver = new XdebugDriver($filter);
  31. $driver->enableDeadCodeDetection();
  32. return $driver;
  33. }
  34. throw new NoCodeCoverageDriverAvailableException;
  35. }
  36. /**
  37. * @throws NoCodeCoverageDriverWithPathCoverageSupportAvailableException
  38. * @throws XdebugNotAvailableException
  39. * @throws XdebugNotEnabledException
  40. */
  41. public function forLineAndPathCoverage(Filter $filter): Driver
  42. {
  43. if ((new Runtime)->hasXdebug()) {
  44. $driver = new XdebugDriver($filter);
  45. $driver->enableDeadCodeDetection();
  46. $driver->enableBranchAndPathCoverage();
  47. return $driver;
  48. }
  49. throw new NoCodeCoverageDriverWithPathCoverageSupportAvailableException;
  50. }
  51. }