Driver.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 function sprintf;
  12. use SebastianBergmann\CodeCoverage\BranchAndPathCoverageNotSupportedException;
  13. use SebastianBergmann\CodeCoverage\Data\RawCodeCoverageData;
  14. use SebastianBergmann\CodeCoverage\DeadCodeDetectionNotSupportedException;
  15. /**
  16. * @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
  17. */
  18. abstract class Driver
  19. {
  20. /**
  21. * @var int
  22. *
  23. * @see http://xdebug.org/docs/code_coverage
  24. */
  25. public const LINE_NOT_EXECUTABLE = -2;
  26. /**
  27. * @var int
  28. *
  29. * @see http://xdebug.org/docs/code_coverage
  30. */
  31. public const LINE_NOT_EXECUTED = -1;
  32. /**
  33. * @var int
  34. *
  35. * @see http://xdebug.org/docs/code_coverage
  36. */
  37. public const LINE_EXECUTED = 1;
  38. /**
  39. * @var int
  40. *
  41. * @see http://xdebug.org/docs/code_coverage
  42. */
  43. public const BRANCH_NOT_HIT = 0;
  44. /**
  45. * @var int
  46. *
  47. * @see http://xdebug.org/docs/code_coverage
  48. */
  49. public const BRANCH_HIT = 1;
  50. private bool $collectBranchAndPathCoverage = false;
  51. private bool $detectDeadCode = false;
  52. public function canCollectBranchAndPathCoverage(): bool
  53. {
  54. return false;
  55. }
  56. public function collectsBranchAndPathCoverage(): bool
  57. {
  58. return $this->collectBranchAndPathCoverage;
  59. }
  60. /**
  61. * @throws BranchAndPathCoverageNotSupportedException
  62. */
  63. public function enableBranchAndPathCoverage(): void
  64. {
  65. if (!$this->canCollectBranchAndPathCoverage()) {
  66. throw new BranchAndPathCoverageNotSupportedException(
  67. sprintf(
  68. '%s does not support branch and path coverage',
  69. $this->nameAndVersion(),
  70. ),
  71. );
  72. }
  73. $this->collectBranchAndPathCoverage = true;
  74. }
  75. public function disableBranchAndPathCoverage(): void
  76. {
  77. $this->collectBranchAndPathCoverage = false;
  78. }
  79. public function canDetectDeadCode(): bool
  80. {
  81. return false;
  82. }
  83. public function detectsDeadCode(): bool
  84. {
  85. return $this->detectDeadCode;
  86. }
  87. /**
  88. * @throws DeadCodeDetectionNotSupportedException
  89. */
  90. public function enableDeadCodeDetection(): void
  91. {
  92. if (!$this->canDetectDeadCode()) {
  93. throw new DeadCodeDetectionNotSupportedException(
  94. sprintf(
  95. '%s does not support dead code detection',
  96. $this->nameAndVersion(),
  97. ),
  98. );
  99. }
  100. $this->detectDeadCode = true;
  101. }
  102. public function disableDeadCodeDetection(): void
  103. {
  104. $this->detectDeadCode = false;
  105. }
  106. abstract public function nameAndVersion(): string;
  107. abstract public function start(): void;
  108. abstract public function stop(): RawCodeCoverageData;
  109. }