/library/phpdocx/lib/php_codesniffer/CodeSniffer/Reports/VersionControl.php

https://github.com/r1zib/salesforce · PHP · 214 lines · 117 code · 36 blank · 61 comment · 17 complexity · 3087c8e53b3bce5826cd212f617c6f55 MD5 · raw file

  1. <?php
  2. /**
  3. * Version control report base class for PHP_CodeSniffer.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Ben Selby <benmatselby@gmail.com>
  10. * @copyright 2009 SQLI <www.sqli.com>
  11. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  13. * @version CVS: $Id: IsCamelCapsTest.php 240585 2007-08-02 00:05:40Z squiz $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. /**
  17. * Version control report base class for PHP_CodeSniffer.
  18. *
  19. * PHP version 5
  20. *
  21. * @category PHP
  22. * @package PHP_CodeSniffer
  23. * @author Ben Selby <benmatselby@gmail.com>
  24. * @copyright 2009 SQLI <www.sqli.com>
  25. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  26. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  27. * @version Release: 1.2.2
  28. * @link http://pear.php.net/package/PHP_CodeSniffer
  29. */
  30. abstract class PHP_CodeSniffer_Reports_VersionControl implements PHP_CodeSniffer_Report
  31. {
  32. /**
  33. * The name of the report we want in the output.
  34. *
  35. * @var string
  36. */
  37. protected $reportName = 'VERSION CONTROL';
  38. /**
  39. * Prints the author of all errors and warnings, as given by "version control blame".
  40. *
  41. * @param array $report Prepared report.
  42. * @param boolean $showWarnings Show warnings?
  43. * @param boolean $showSources Show sources?
  44. * @param integer $width Maximum allowed lne width.
  45. *
  46. * @return string
  47. */
  48. public function generate(
  49. $report,
  50. $showWarnings=true,
  51. $showSources=false,
  52. $width=80
  53. ) {
  54. $authors = array();
  55. $praise = array();
  56. $sources = array();
  57. $width = max($width, 70);
  58. $errorsShown = 0;
  59. foreach ($report['files'] as $filename => $file) {
  60. $blames = $this->getBlameContent($filename);
  61. foreach ($file['messages'] as $line => $lineErrors) {
  62. $author = $this->getAuthor($blames[($line - 1)]);
  63. if ($author === false) {
  64. continue;
  65. }
  66. if (isset($authors[$author]) === false) {
  67. $authors[$author] = 0;
  68. $praise[$author] = array(
  69. 'good' => 0,
  70. 'bad' => 0,
  71. );
  72. }
  73. $praise[$author]['bad']++;
  74. foreach ($lineErrors as $column => $colErrors) {
  75. foreach ($colErrors as $error) {
  76. $errorsShown++;
  77. $authors[$author]++;
  78. if ($showSources === true) {
  79. $source = $error['source'];
  80. if (isset($sources[$author][$source]) === false) {
  81. $sources[$author][$source] = 1;
  82. } else {
  83. $sources[$author][$source]++;
  84. }
  85. }
  86. }
  87. }
  88. unset($blames[($line - 1)]);
  89. }//end foreach
  90. // No go through and give the authors some credit for
  91. // all the lines that do not have errors.
  92. foreach ($blames as $line) {
  93. $author = $this->getAuthor($line);
  94. if (false === $author) {
  95. continue;
  96. }
  97. if (isset($authors[$author]) === false) {
  98. // This author doesn't have any errors.
  99. if (PHP_CODESNIFFER_VERBOSITY === 0) {
  100. continue;
  101. }
  102. $authors[$author] = 0;
  103. $praise[$author] = array(
  104. 'good' => 0,
  105. 'bad' => 0,
  106. );
  107. }
  108. $praise[$author]['good']++;
  109. }//end foreach
  110. }//end foreach
  111. if ($errorsShown === 0) {
  112. // Nothing to show.
  113. return 0;
  114. }
  115. arsort($authors);
  116. echo PHP_EOL.'PHP CODE SNIFFER '.$this->reportName.' BLAME SUMMARY'.PHP_EOL;
  117. echo str_repeat('-', $width).PHP_EOL;
  118. if ($showSources === true) {
  119. echo 'AUTHOR SOURCE'.str_repeat(' ', ($width - 43)).'(Author %) (Overall %) COUNT'.PHP_EOL;
  120. echo str_repeat('-', $width).PHP_EOL;
  121. } else {
  122. echo 'AUTHOR'.str_repeat(' ', ($width - 34)).'(Author %) (Overall %) COUNT'.PHP_EOL;
  123. echo str_repeat('-', $width).PHP_EOL;
  124. }
  125. foreach ($authors as $author => $count) {
  126. if ($praise[$author]['good'] === 0) {
  127. $percent = 0;
  128. } else {
  129. $total = ($praise[$author]['bad'] + $praise[$author]['good']);
  130. $percent = round(($praise[$author]['bad'] / $total * 100), 2);
  131. }
  132. $overallPercent = '('.round((($count / $errorsShown) * 100), 2).')';
  133. $authorPercent = '('.$percent.')';
  134. $line = str_repeat(' ', (6 - strlen($count))).$count;
  135. $line = str_repeat(' ', (12 - strlen($overallPercent))).$overallPercent.$line;
  136. $line = str_repeat(' ', (11 - strlen($authorPercent))).$authorPercent.$line;
  137. $line = $author.str_repeat(' ', ($width - strlen($author) - strlen($line))).$line;
  138. echo $line.PHP_EOL;
  139. if ($showSources === true && isset($sources[$author]) === true) {
  140. $errors = $sources[$author];
  141. asort($errors);
  142. $errors = array_reverse($errors);
  143. foreach ($errors as $source => $count) {
  144. if ($source === 'count') {
  145. continue;
  146. }
  147. $line = str_repeat(' ', (5 - strlen($count))).$count;
  148. echo ' '.$source.str_repeat(' ', ($width - 14 - strlen($source))).$line.PHP_EOL;
  149. }
  150. }
  151. }//end foreach
  152. echo str_repeat('-', $width).PHP_EOL;
  153. echo 'A TOTAL OF '.$errorsShown.' SNIFF VIOLATION(S) ';
  154. echo 'WERE COMMITTED BY '.count($authors).' AUTHOR(S)'.PHP_EOL;
  155. echo str_repeat('-', $width).PHP_EOL.PHP_EOL;
  156. if (class_exists('PHP_Timer', false) === true) {
  157. echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL;
  158. }
  159. return $errorsShown;
  160. }//end generate()
  161. /**
  162. * Extract the author from a blame line.
  163. *
  164. * @param string $line Line to parse.
  165. *
  166. * @return mixed string or false if impossible to recover.
  167. */
  168. abstract protected function getAuthor($line);
  169. /**
  170. * Gets the blame output.
  171. *
  172. * @param string $filename File to blame.
  173. *
  174. * @return array
  175. */
  176. abstract protected function getBlameContent($filename);
  177. }//end class
  178. ?>