Crap4j.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /*
  3. * This file is part of the php-code-coverage package.
  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 SebastianBergmann\CodeCoverage\CodeCoverage;
  12. use SebastianBergmann\CodeCoverage\InvalidArgumentException;
  13. use SebastianBergmann\CodeCoverage\Node\File;
  14. use SebastianBergmann\CodeCoverage\RuntimeException;
  15. class Crap4j
  16. {
  17. /**
  18. * @var int
  19. */
  20. private $threshold;
  21. /**
  22. * @param int $threshold
  23. */
  24. public function __construct($threshold = 30)
  25. {
  26. if (!\is_int($threshold)) {
  27. throw InvalidArgumentException::create(
  28. 1,
  29. 'integer'
  30. );
  31. }
  32. $this->threshold = $threshold;
  33. }
  34. /**
  35. * @param CodeCoverage $coverage
  36. * @param string $target
  37. * @param string $name
  38. *
  39. * @return string
  40. *
  41. * @throws \SebastianBergmann\CodeCoverage\RuntimeException
  42. */
  43. public function process(CodeCoverage $coverage, $target = null, $name = null)
  44. {
  45. $document = new \DOMDocument('1.0', 'UTF-8');
  46. $document->formatOutput = true;
  47. $root = $document->createElement('crap_result');
  48. $document->appendChild($root);
  49. $project = $document->createElement('project', \is_string($name) ? $name : '');
  50. $root->appendChild($project);
  51. $root->appendChild($document->createElement('timestamp', \date('Y-m-d H:i:s', (int) $_SERVER['REQUEST_TIME'])));
  52. $stats = $document->createElement('stats');
  53. $methodsNode = $document->createElement('methods');
  54. $report = $coverage->getReport();
  55. unset($coverage);
  56. $fullMethodCount = 0;
  57. $fullCrapMethodCount = 0;
  58. $fullCrapLoad = 0;
  59. $fullCrap = 0;
  60. foreach ($report as $item) {
  61. $namespace = 'global';
  62. if (!$item instanceof File) {
  63. continue;
  64. }
  65. $file = $document->createElement('file');
  66. $file->setAttribute('name', $item->getPath());
  67. $classes = $item->getClassesAndTraits();
  68. foreach ($classes as $className => $class) {
  69. foreach ($class['methods'] as $methodName => $method) {
  70. $crapLoad = $this->getCrapLoad($method['crap'], $method['ccn'], $method['coverage']);
  71. $fullCrap += $method['crap'];
  72. $fullCrapLoad += $crapLoad;
  73. $fullMethodCount++;
  74. if ($method['crap'] >= $this->threshold) {
  75. $fullCrapMethodCount++;
  76. }
  77. $methodNode = $document->createElement('method');
  78. if (!empty($class['package']['namespace'])) {
  79. $namespace = $class['package']['namespace'];
  80. }
  81. $methodNode->appendChild($document->createElement('package', $namespace));
  82. $methodNode->appendChild($document->createElement('className', $className));
  83. $methodNode->appendChild($document->createElement('methodName', $methodName));
  84. $methodNode->appendChild($document->createElement('methodSignature', \htmlspecialchars($method['signature'])));
  85. $methodNode->appendChild($document->createElement('fullMethod', \htmlspecialchars($method['signature'])));
  86. $methodNode->appendChild($document->createElement('crap', $this->roundValue($method['crap'])));
  87. $methodNode->appendChild($document->createElement('complexity', $method['ccn']));
  88. $methodNode->appendChild($document->createElement('coverage', $this->roundValue($method['coverage'])));
  89. $methodNode->appendChild($document->createElement('crapLoad', \round($crapLoad)));
  90. $methodsNode->appendChild($methodNode);
  91. }
  92. }
  93. }
  94. $stats->appendChild($document->createElement('name', 'Method Crap Stats'));
  95. $stats->appendChild($document->createElement('methodCount', $fullMethodCount));
  96. $stats->appendChild($document->createElement('crapMethodCount', $fullCrapMethodCount));
  97. $stats->appendChild($document->createElement('crapLoad', \round($fullCrapLoad)));
  98. $stats->appendChild($document->createElement('totalCrap', $fullCrap));
  99. if ($fullMethodCount > 0) {
  100. $crapMethodPercent = $this->roundValue((100 * $fullCrapMethodCount) / $fullMethodCount);
  101. } else {
  102. $crapMethodPercent = 0;
  103. }
  104. $stats->appendChild($document->createElement('crapMethodPercent', $crapMethodPercent));
  105. $root->appendChild($stats);
  106. $root->appendChild($methodsNode);
  107. $buffer = $document->saveXML();
  108. if ($target !== null) {
  109. if (!\is_dir(\dirname($target))) {
  110. \mkdir(\dirname($target), 0777, true);
  111. }
  112. if (@\file_put_contents($target, $buffer) === false) {
  113. throw new RuntimeException(
  114. \sprintf(
  115. 'Could not write to "%s',
  116. $target
  117. )
  118. );
  119. }
  120. }
  121. return $buffer;
  122. }
  123. /**
  124. * @param float $crapValue
  125. * @param int $cyclomaticComplexity
  126. * @param float $coveragePercent
  127. *
  128. * @return float
  129. */
  130. private function getCrapLoad($crapValue, $cyclomaticComplexity, $coveragePercent)
  131. {
  132. $crapLoad = 0;
  133. if ($crapValue >= $this->threshold) {
  134. $crapLoad += $cyclomaticComplexity * (1.0 - $coveragePercent / 100);
  135. $crapLoad += $cyclomaticComplexity / $this->threshold;
  136. }
  137. return $crapLoad;
  138. }
  139. /**
  140. * @param float $value
  141. *
  142. * @return float
  143. */
  144. private function roundValue($value)
  145. {
  146. return \round($value, 2);
  147. }
  148. }