/vendor/squizlabs/php_codesniffer/CodeSniffer/Reports/VersionControl.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 260 lines · 129 code · 39 blank · 92 comment · 20 complexity · f3e79f42b01b1254846f2527e162bbe4 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-2014 SQLI <www.sqli.com>
  11. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  13. * @link http://pear.php.net/package/PHP_CodeSniffer
  14. */
  15. /**
  16. * Version control report base class for PHP_CodeSniffer.
  17. *
  18. * PHP version 5
  19. *
  20. * @category PHP
  21. * @package PHP_CodeSniffer
  22. * @author Ben Selby <benmatselby@gmail.com>
  23. * @copyright 2009-2014 SQLI <www.sqli.com>
  24. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  25. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  26. * @version Release: 1.2.2
  27. * @link http://pear.php.net/package/PHP_CodeSniffer
  28. */
  29. abstract class PHP_CodeSniffer_Reports_VersionControl implements PHP_CodeSniffer_Report
  30. {
  31. /**
  32. * The name of the report we want in the output.
  33. *
  34. * @var string
  35. */
  36. protected $reportName = 'VERSION CONTROL';
  37. /**
  38. * A cache of author stats collected during the run.
  39. *
  40. * @var array
  41. */
  42. private $_authorCache = array();
  43. /**
  44. * A cache of blame stats collected during the run.
  45. *
  46. * @var array
  47. */
  48. private $_praiseCache = array();
  49. /**
  50. * A cache of source stats collected during the run.
  51. *
  52. * @var array
  53. */
  54. private $_sourceCache = array();
  55. /**
  56. * Generate a partial report for a single processed file.
  57. *
  58. * Function should return TRUE if it printed or stored data about the file
  59. * and FALSE if it ignored the file. Returning TRUE indicates that the file and
  60. * its data should be counted in the grand totals.
  61. *
  62. * @param array $report Prepared report data.
  63. * @param boolean $showSources Show sources?
  64. * @param int $width Maximum allowed line width.
  65. *
  66. * @return boolean
  67. */
  68. public function generateFileReport(
  69. $report,
  70. $showSources=false,
  71. $width=80
  72. ) {
  73. $blames = $this->getBlameContent($report['filename']);
  74. foreach ($report['messages'] as $line => $lineErrors) {
  75. $author = 'Unknown';
  76. if (isset($blames[($line - 1)]) === true) {
  77. $blameAuthor = $this->getAuthor($blames[($line - 1)]);
  78. if ($blameAuthor !== false) {
  79. $author = $blameAuthor;
  80. }
  81. }
  82. if (isset($this->_authorCache[$author]) === false) {
  83. $this->_authorCache[$author] = 0;
  84. $this->_praiseCache[$author] = array(
  85. 'good' => 0,
  86. 'bad' => 0,
  87. );
  88. }
  89. $this->_praiseCache[$author]['bad']++;
  90. foreach ($lineErrors as $column => $colErrors) {
  91. foreach ($colErrors as $error) {
  92. $this->_authorCache[$author]++;
  93. if ($showSources === true) {
  94. $source = $error['source'];
  95. if (isset($this->_sourceCache[$author][$source]) === false) {
  96. $this->_sourceCache[$author][$source] = 1;
  97. } else {
  98. $this->_sourceCache[$author][$source]++;
  99. }
  100. }
  101. }
  102. }
  103. unset($blames[($line - 1)]);
  104. }//end foreach
  105. // No go through and give the authors some credit for
  106. // all the lines that do not have errors.
  107. foreach ($blames as $line) {
  108. $author = $this->getAuthor($line);
  109. if ($author === false) {
  110. $author = 'Unknown';
  111. }
  112. if (isset($this->_authorCache[$author]) === false) {
  113. // This author doesn't have any errors.
  114. if (PHP_CODESNIFFER_VERBOSITY === 0) {
  115. continue;
  116. }
  117. $this->_authorCache[$author] = 0;
  118. $this->_praiseCache[$author] = array(
  119. 'good' => 0,
  120. 'bad' => 0,
  121. );
  122. }
  123. $this->_praiseCache[$author]['good']++;
  124. }//end foreach
  125. return true;
  126. }//end generateFileReport()
  127. /**
  128. * Prints the author of all errors and warnings, as given by "version control blame".
  129. *
  130. * @param string $cachedData Any partial report data that was returned from
  131. * generateFileReport during the run.
  132. * @param int $totalFiles Total number of files processed during the run.
  133. * @param int $totalErrors Total number of errors found during the run.
  134. * @param int $totalWarnings Total number of warnings found during the run.
  135. * @param boolean $showSources Show sources?
  136. * @param int $width Maximum allowed line width.
  137. * @param boolean $toScreen Is the report being printed to screen?
  138. *
  139. * @return void
  140. */
  141. public function generate(
  142. $cachedData,
  143. $totalFiles,
  144. $totalErrors,
  145. $totalWarnings,
  146. $showSources=false,
  147. $width=80,
  148. $toScreen=true
  149. ) {
  150. $errorsShown = ($totalErrors + $totalWarnings);
  151. if ($errorsShown === 0) {
  152. // Nothing to show.
  153. return;
  154. }
  155. $width = max($width, 70);
  156. arsort($this->_authorCache);
  157. echo PHP_EOL.'PHP CODE SNIFFER '.$this->reportName.' BLAME SUMMARY'.PHP_EOL;
  158. echo str_repeat('-', $width).PHP_EOL;
  159. if ($showSources === true) {
  160. echo 'AUTHOR SOURCE'.str_repeat(' ', ($width - 43)).'(Author %) (Overall %) COUNT'.PHP_EOL;
  161. echo str_repeat('-', $width).PHP_EOL;
  162. } else {
  163. echo 'AUTHOR'.str_repeat(' ', ($width - 34)).'(Author %) (Overall %) COUNT'.PHP_EOL;
  164. echo str_repeat('-', $width).PHP_EOL;
  165. }
  166. foreach ($this->_authorCache as $author => $count) {
  167. if ($this->_praiseCache[$author]['good'] === 0) {
  168. $percent = 0;
  169. } else {
  170. $total = ($this->_praiseCache[$author]['bad'] + $this->_praiseCache[$author]['good']);
  171. $percent = round(($this->_praiseCache[$author]['bad'] / $total * 100), 2);
  172. }
  173. $overallPercent = '('.round((($count / $errorsShown) * 100), 2).')';
  174. $authorPercent = '('.$percent.')';
  175. $line = str_repeat(' ', (6 - strlen($count))).$count;
  176. $line = str_repeat(' ', (12 - strlen($overallPercent))).$overallPercent.$line;
  177. $line = str_repeat(' ', (11 - strlen($authorPercent))).$authorPercent.$line;
  178. $line = $author.str_repeat(' ', ($width - strlen($author) - strlen($line))).$line;
  179. echo $line.PHP_EOL;
  180. if ($showSources === true && isset($this->_sourceCache[$author]) === true) {
  181. $errors = $this->_sourceCache[$author];
  182. asort($errors);
  183. $errors = array_reverse($errors);
  184. foreach ($errors as $source => $count) {
  185. if ($source === 'count') {
  186. continue;
  187. }
  188. $line = str_repeat(' ', (5 - strlen($count))).$count;
  189. echo ' '.$source.str_repeat(' ', ($width - 14 - strlen($source))).$line.PHP_EOL;
  190. }
  191. }
  192. }//end foreach
  193. echo str_repeat('-', $width).PHP_EOL;
  194. echo 'A TOTAL OF '.$errorsShown.' SNIFF VIOLATION(S) ';
  195. echo 'WERE COMMITTED BY '.count($this->_authorCache).' AUTHOR(S)'.PHP_EOL;
  196. echo str_repeat('-', $width).PHP_EOL.PHP_EOL;
  197. if ($toScreen === true
  198. && PHP_CODESNIFFER_INTERACTIVE === false
  199. && class_exists('PHP_Timer', false) === true
  200. ) {
  201. echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL;
  202. }
  203. }//end generate()
  204. /**
  205. * Extract the author from a blame line.
  206. *
  207. * @param string $line Line to parse.
  208. *
  209. * @return mixed string or false if impossible to recover.
  210. */
  211. abstract protected function getAuthor($line);
  212. /**
  213. * Gets the blame output.
  214. *
  215. * @param string $filename File to blame.
  216. *
  217. * @return array
  218. */
  219. abstract protected function getBlameContent($filename);
  220. }//end class
  221. ?>