Crap4j.php 5.6 KB

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