PHP.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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\Report;
  11. use function dirname;
  12. use function file_put_contents;
  13. use function serialize;
  14. use function str_contains;
  15. use SebastianBergmann\CodeCoverage\CodeCoverage;
  16. use SebastianBergmann\CodeCoverage\Driver\WriteOperationFailedException;
  17. use SebastianBergmann\CodeCoverage\Util\Filesystem;
  18. final class PHP
  19. {
  20. public function process(CodeCoverage $coverage, ?string $target = null): string
  21. {
  22. $coverage->clearCache();
  23. $buffer = "<?php
  24. return \unserialize(<<<'END_OF_COVERAGE_SERIALIZATION'" . PHP_EOL . serialize($coverage) . PHP_EOL . 'END_OF_COVERAGE_SERIALIZATION' . PHP_EOL . ');';
  25. if ($target !== null) {
  26. if (!str_contains($target, '://')) {
  27. Filesystem::createDirectory(dirname($target));
  28. }
  29. if (@file_put_contents($target, $buffer) === false) {
  30. throw new WriteOperationFailedException($target);
  31. }
  32. }
  33. return $buffer;
  34. }
  35. }