PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpunit/php-code-coverage/src/CodeCoverage/Report/Text.php

https://gitlab.com/techniconline/kmc
PHP | 246 lines | 204 code | 22 blank | 20 comment | 10 complexity | 29079cb611aada2ba64ec0720229c55e MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the PHP_CodeCoverage 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. /**
  11. * Generates human readable output from an PHP_CodeCoverage object.
  12. *
  13. * The output gets put into a text file our written to the CLI.
  14. *
  15. * @since Class available since Release 1.1.0
  16. */
  17. class PHP_CodeCoverage_Report_Text
  18. {
  19. protected $lowUpperBound;
  20. protected $highLowerBound;
  21. protected $showUncoveredFiles;
  22. protected $showOnlySummary;
  23. protected $colors = array(
  24. 'green' => "\x1b[30;42m",
  25. 'yellow' => "\x1b[30;43m",
  26. 'red' => "\x1b[37;41m",
  27. 'header' => "\x1b[1;37;40m",
  28. 'reset' => "\x1b[0m",
  29. 'eol' => "\x1b[2K",
  30. );
  31. public function __construct($lowUpperBound, $highLowerBound, $showUncoveredFiles, $showOnlySummary)
  32. {
  33. $this->lowUpperBound = $lowUpperBound;
  34. $this->highLowerBound = $highLowerBound;
  35. $this->showUncoveredFiles = $showUncoveredFiles;
  36. $this->showOnlySummary = $showOnlySummary;
  37. }
  38. /**
  39. * @param PHP_CodeCoverage $coverage
  40. * @param bool $showColors
  41. * @return string
  42. */
  43. public function process(PHP_CodeCoverage $coverage, $showColors = false)
  44. {
  45. $output = PHP_EOL . PHP_EOL;
  46. $report = $coverage->getReport();
  47. unset($coverage);
  48. $colors = array(
  49. 'header' => '',
  50. 'classes' => '',
  51. 'methods' => '',
  52. 'lines' => '',
  53. 'reset' => '',
  54. 'eol' => ''
  55. );
  56. if ($showColors) {
  57. $colors['classes'] = $this->getCoverageColor(
  58. $report->getNumTestedClassesAndTraits(),
  59. $report->getNumClassesAndTraits()
  60. );
  61. $colors['methods'] = $this->getCoverageColor(
  62. $report->getNumTestedMethods(),
  63. $report->getNumMethods()
  64. );
  65. $colors['lines'] = $this->getCoverageColor(
  66. $report->getNumExecutedLines(),
  67. $report->getNumExecutableLines()
  68. );
  69. $colors['reset'] = $this->colors['reset'];
  70. $colors['header'] = $this->colors['header'];
  71. $colors['eol'] = $this->colors['eol'];
  72. }
  73. $classes = sprintf(
  74. ' Classes: %6s (%d/%d)',
  75. PHP_CodeCoverage_Util::percent(
  76. $report->getNumTestedClassesAndTraits(),
  77. $report->getNumClassesAndTraits(),
  78. true
  79. ),
  80. $report->getNumTestedClassesAndTraits(),
  81. $report->getNumClassesAndTraits()
  82. );
  83. $methods = sprintf(
  84. ' Methods: %6s (%d/%d)',
  85. PHP_CodeCoverage_Util::percent(
  86. $report->getNumTestedMethods(),
  87. $report->getNumMethods(),
  88. true
  89. ),
  90. $report->getNumTestedMethods(),
  91. $report->getNumMethods()
  92. );
  93. $lines = sprintf(
  94. ' Lines: %6s (%d/%d)',
  95. PHP_CodeCoverage_Util::percent(
  96. $report->getNumExecutedLines(),
  97. $report->getNumExecutableLines(),
  98. true
  99. ),
  100. $report->getNumExecutedLines(),
  101. $report->getNumExecutableLines()
  102. );
  103. $padding = max(array_map('strlen', array($classes, $methods, $lines)));
  104. if ($this->showOnlySummary) {
  105. $title = 'Code Coverage Report Summary:';
  106. $padding = max($padding, strlen($title));
  107. $output .= $this->format($colors['header'], $padding, $title);
  108. } else {
  109. $date = date(' Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
  110. $title = 'Code Coverage Report:';
  111. $output .= $this->format($colors['header'], $padding, $title);
  112. $output .= $this->format($colors['header'], $padding, $date);
  113. $output .= $this->format($colors['header'], $padding, '');
  114. $output .= $this->format($colors['header'], $padding, ' Summary:');
  115. }
  116. $output .= $this->format($colors['classes'], $padding, $classes);
  117. $output .= $this->format($colors['methods'], $padding, $methods);
  118. $output .= $this->format($colors['lines'], $padding, $lines);
  119. if ($this->showOnlySummary) {
  120. return $output . PHP_EOL;
  121. }
  122. $classCoverage = array();
  123. foreach ($report as $item) {
  124. if (!$item instanceof PHP_CodeCoverage_Report_Node_File) {
  125. continue;
  126. }
  127. $classes = $item->getClassesAndTraits();
  128. foreach ($classes as $className => $class) {
  129. $classStatements = 0;
  130. $coveredClassStatements = 0;
  131. $coveredMethods = 0;
  132. $classMethods = 0;
  133. foreach ($class['methods'] as $method) {
  134. if ($method['executableLines'] == 0) {
  135. continue;
  136. }
  137. $classMethods++;
  138. $classStatements += $method['executableLines'];
  139. $coveredClassStatements += $method['executedLines'];
  140. if ($method['coverage'] == 100) {
  141. $coveredMethods++;
  142. }
  143. }
  144. if (!empty($class['package']['namespace'])) {
  145. $namespace = '\\' . $class['package']['namespace'] . '::';
  146. } elseif (!empty($class['package']['fullPackage'])) {
  147. $namespace = '@' . $class['package']['fullPackage'] . '::';
  148. } else {
  149. $namespace = '';
  150. }
  151. $classCoverage[$namespace . $className] = array(
  152. 'namespace' => $namespace,
  153. 'className ' => $className,
  154. 'methodsCovered' => $coveredMethods,
  155. 'methodCount' => $classMethods,
  156. 'statementsCovered' => $coveredClassStatements,
  157. 'statementCount' => $classStatements,
  158. );
  159. }
  160. }
  161. ksort($classCoverage);
  162. $methodColor = '';
  163. $linesColor = '';
  164. $resetColor = '';
  165. foreach ($classCoverage as $fullQualifiedPath => $classInfo) {
  166. if ($classInfo['statementsCovered'] != 0 ||
  167. $this->showUncoveredFiles
  168. ) {
  169. if ($showColors) {
  170. $methodColor = $this->getCoverageColor($classInfo['methodsCovered'], $classInfo['methodCount']);
  171. $linesColor = $this->getCoverageColor($classInfo['statementsCovered'], $classInfo['statementCount']);
  172. $resetColor = $colors['reset'];
  173. }
  174. $output .= PHP_EOL . $fullQualifiedPath . PHP_EOL
  175. . ' ' . $methodColor . 'Methods: ' . $this->printCoverageCounts($classInfo['methodsCovered'], $classInfo['methodCount'], 2) . $resetColor . ' '
  176. . ' ' . $linesColor . 'Lines: ' . $this->printCoverageCounts($classInfo['statementsCovered'], $classInfo['statementCount'], 3) . $resetColor;
  177. }
  178. }
  179. return $output . PHP_EOL;
  180. }
  181. protected function getCoverageColor($numberOfCoveredElements, $totalNumberOfElements)
  182. {
  183. $coverage = PHP_CodeCoverage_Util::percent(
  184. $numberOfCoveredElements,
  185. $totalNumberOfElements
  186. );
  187. if ($coverage >= $this->highLowerBound) {
  188. return $this->colors['green'];
  189. } elseif ($coverage > $this->lowUpperBound) {
  190. return $this->colors['yellow'];
  191. }
  192. return $this->colors['red'];
  193. }
  194. protected function printCoverageCounts($numberOfCoveredElements, $totalNumberOfElements, $presicion)
  195. {
  196. $format = '%' . $presicion . 's';
  197. return PHP_CodeCoverage_Util::percent(
  198. $numberOfCoveredElements,
  199. $totalNumberOfElements,
  200. true,
  201. true
  202. ) .
  203. ' (' . sprintf($format, $numberOfCoveredElements) . '/' .
  204. sprintf($format, $totalNumberOfElements) . ')';
  205. }
  206. private function format($color, $padding, $string)
  207. {
  208. $reset = $color ? $this->colors['reset'] : '';
  209. return $color . str_pad($string, $padding) . $reset . PHP_EOL;
  210. }
  211. }