Totals.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Xml;
  11. use function sprintf;
  12. use DOMElement;
  13. use DOMNode;
  14. use SebastianBergmann\CodeCoverage\Util\Percentage;
  15. /**
  16. * @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
  17. */
  18. final class Totals
  19. {
  20. private readonly DOMNode $container;
  21. private readonly DOMElement $linesNode;
  22. private readonly DOMElement $methodsNode;
  23. private readonly DOMElement $functionsNode;
  24. private readonly DOMElement $classesNode;
  25. private readonly DOMElement $traitsNode;
  26. public function __construct(DOMElement $container)
  27. {
  28. $this->container = $container;
  29. $dom = $container->ownerDocument;
  30. $this->linesNode = $dom->createElementNS(
  31. 'https://schema.phpunit.de/coverage/1.0',
  32. 'lines',
  33. );
  34. $this->methodsNode = $dom->createElementNS(
  35. 'https://schema.phpunit.de/coverage/1.0',
  36. 'methods',
  37. );
  38. $this->functionsNode = $dom->createElementNS(
  39. 'https://schema.phpunit.de/coverage/1.0',
  40. 'functions',
  41. );
  42. $this->classesNode = $dom->createElementNS(
  43. 'https://schema.phpunit.de/coverage/1.0',
  44. 'classes',
  45. );
  46. $this->traitsNode = $dom->createElementNS(
  47. 'https://schema.phpunit.de/coverage/1.0',
  48. 'traits',
  49. );
  50. $container->appendChild($this->linesNode);
  51. $container->appendChild($this->methodsNode);
  52. $container->appendChild($this->functionsNode);
  53. $container->appendChild($this->classesNode);
  54. $container->appendChild($this->traitsNode);
  55. }
  56. public function container(): DOMNode
  57. {
  58. return $this->container;
  59. }
  60. public function setNumLines(int $loc, int $cloc, int $ncloc, int $executable, int $executed): void
  61. {
  62. $this->linesNode->setAttribute('total', (string) $loc);
  63. $this->linesNode->setAttribute('comments', (string) $cloc);
  64. $this->linesNode->setAttribute('code', (string) $ncloc);
  65. $this->linesNode->setAttribute('executable', (string) $executable);
  66. $this->linesNode->setAttribute('executed', (string) $executed);
  67. $this->linesNode->setAttribute(
  68. 'percent',
  69. $executable === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($executed, $executable)->asFloat()),
  70. );
  71. }
  72. public function setNumClasses(int $count, int $tested): void
  73. {
  74. $this->classesNode->setAttribute('count', (string) $count);
  75. $this->classesNode->setAttribute('tested', (string) $tested);
  76. $this->classesNode->setAttribute(
  77. 'percent',
  78. $count === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($tested, $count)->asFloat()),
  79. );
  80. }
  81. public function setNumTraits(int $count, int $tested): void
  82. {
  83. $this->traitsNode->setAttribute('count', (string) $count);
  84. $this->traitsNode->setAttribute('tested', (string) $tested);
  85. $this->traitsNode->setAttribute(
  86. 'percent',
  87. $count === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($tested, $count)->asFloat()),
  88. );
  89. }
  90. public function setNumMethods(int $count, int $tested): void
  91. {
  92. $this->methodsNode->setAttribute('count', (string) $count);
  93. $this->methodsNode->setAttribute('tested', (string) $tested);
  94. $this->methodsNode->setAttribute(
  95. 'percent',
  96. $count === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($tested, $count)->asFloat()),
  97. );
  98. }
  99. public function setNumFunctions(int $count, int $tested): void
  100. {
  101. $this->functionsNode->setAttribute('count', (string) $count);
  102. $this->functionsNode->setAttribute('tested', (string) $tested);
  103. $this->functionsNode->setAttribute(
  104. 'percent',
  105. $count === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($tested, $count)->asFloat()),
  106. );
  107. }
  108. }