PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/CodeSniffer/Reports/Source.php

https://github.com/becoded/PHP_CodeSniffer
PHP | 202 lines | 117 code | 35 blank | 50 comment | 22 complexity | f3b418a5a9c4e3577b0e6dc9602f8e0d MD5 | raw file
  1. <?php
  2. /**
  3. * Source report for PHP_CodeSniffer.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Gabriele Santini <gsantini@sqli.com>
  10. * @author Greg Sherwood <gsherwood@squiz.net>
  11. * @copyright 2009 SQLI <www.sqli.com>
  12. * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
  13. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. /**
  17. * Source report for PHP_CodeSniffer.
  18. *
  19. * PHP version 5
  20. *
  21. * @category PHP
  22. * @package PHP_CodeSniffer
  23. * @author Gabriele Santini <gsantini@sqli.com>
  24. * @author Greg Sherwood <gsherwood@squiz.net>
  25. * @copyright 2009 SQLI <www.sqli.com>
  26. * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
  27. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  28. * @version Release: @package_version@
  29. * @link http://pear.php.net/package/PHP_CodeSniffer
  30. */
  31. class PHP_CodeSniffer_Reports_Source implements PHP_CodeSniffer_Report
  32. {
  33. /**
  34. * Prints the source of all errors and warnings.
  35. *
  36. * @param array $report Prepared report.
  37. * @param boolean $showSources Show sources?
  38. * @param int $width Maximum allowed lne width.
  39. * @param boolean $toScreen Is the report being printed to screen?
  40. *
  41. * @return string
  42. */
  43. public function generate(
  44. $report,
  45. $showSources=false,
  46. $width=80,
  47. $toScreen=true
  48. ) {
  49. $sources = array();
  50. $width = max($width, 70);
  51. $errorsShown = 0;
  52. foreach ($report['files'] as $filename => $file) {
  53. foreach ($file['messages'] as $line => $lineErrors) {
  54. foreach ($lineErrors as $column => $colErrors) {
  55. foreach ($colErrors as $error) {
  56. $errorsShown++;
  57. $source = $error['source'];
  58. if (isset($sources[$source]) === false) {
  59. $sources[$source] = 1;
  60. } else {
  61. $sources[$source]++;
  62. }
  63. }
  64. }
  65. }
  66. }
  67. if ($errorsShown === 0) {
  68. // Nothing to show.
  69. return 0;
  70. }
  71. asort($sources);
  72. $sources = array_reverse($sources);
  73. echo PHP_EOL.'PHP CODE SNIFFER VIOLATION SOURCE SUMMARY'.PHP_EOL;
  74. echo str_repeat('-', $width).PHP_EOL;
  75. if ($showSources === true) {
  76. echo 'SOURCE'.str_repeat(' ', ($width - 11)).'COUNT'.PHP_EOL;
  77. echo str_repeat('-', $width).PHP_EOL;
  78. } else {
  79. echo 'STANDARD CATEGORY SNIFF'.str_repeat(' ', ($width - 40)).'COUNT'.PHP_EOL;
  80. echo str_repeat('-', $width).PHP_EOL;
  81. }
  82. foreach ($sources as $source => $count) {
  83. if ($showSources === true) {
  84. echo $source.str_repeat(' ', ($width - 5 - strlen($source)));
  85. } else {
  86. $parts = explode('.', $source);
  87. if (strlen($parts[0]) > 8) {
  88. $parts[0] = substr($parts[0], 0, ((strlen($parts[0]) - 8) * -1));
  89. }
  90. echo $parts[0].str_repeat(' ', (10 - strlen($parts[0])));
  91. $category = $this->makeFriendlyName($parts[1]);
  92. if (strlen($category) > 18) {
  93. $category = substr($category, 0, ((strlen($category) - 18) * -1));
  94. }
  95. echo $category.str_repeat(' ', (20 - strlen($category)));
  96. $sniff = $this->makeFriendlyName($parts[2]);
  97. if (isset($parts[3]) === true) {
  98. $name = $this->makeFriendlyName($parts[3]);
  99. $name[0] = strtolower($name[0]);
  100. $sniff .= ' '.$name;
  101. }
  102. if (strlen($sniff) > ($width - 37)) {
  103. $sniff = substr($sniff, 0, ($width - 37 - strlen($sniff)));
  104. }
  105. echo $sniff.str_repeat(' ', ($width - 35 - strlen($sniff)));
  106. }//end if
  107. echo $count.PHP_EOL;
  108. }//end foreach
  109. echo str_repeat('-', $width).PHP_EOL;
  110. echo 'A TOTAL OF '.$errorsShown.' SNIFF VIOLATION(S) ';
  111. echo 'WERE FOUND IN '.count($sources).' SOURCE(S)'.PHP_EOL;
  112. echo str_repeat('-', $width).PHP_EOL.PHP_EOL;
  113. if ($toScreen === true
  114. && PHP_CODESNIFFER_INTERACTIVE === false
  115. && class_exists('PHP_Timer', false) === true
  116. ) {
  117. echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL;
  118. }
  119. return $errorsShown;
  120. }//end generate()
  121. /**
  122. * Converts a camel caps name into a readable string.
  123. *
  124. * @param string $name The camel caps name to convert.
  125. *
  126. * @return string
  127. */
  128. public function makeFriendlyName($name)
  129. {
  130. $friendlyName = '';
  131. $length = strlen($name);
  132. $lastWasUpper = false;
  133. $lastWasNumeric = false;
  134. for ($i = 0; $i < $length; $i++) {
  135. if (is_numeric($name[$i]) === true) {
  136. if ($lastWasNumeric === false) {
  137. $friendlyName .= ' ';
  138. }
  139. $lastWasUpper = false;
  140. $lastWasNumeric = true;
  141. } else {
  142. $lastWasNumeric = false;
  143. $char = strtolower($name[$i]);
  144. if ($char === $name[$i]) {
  145. // Lowercase.
  146. $lastWasUpper = false;
  147. } else {
  148. // Uppercase.
  149. if ($lastWasUpper === false) {
  150. $friendlyName .= ' ';
  151. $next = $name[($i + 1)];
  152. if (strtolower($next) === $next) {
  153. // Next char is lowercase so it is a word boundary.
  154. $name[$i] = strtolower($name[$i]);
  155. }
  156. }
  157. $lastWasUpper = true;
  158. }
  159. }//end if
  160. $friendlyName .= $name[$i];
  161. }//end for
  162. $friendlyName = trim($friendlyName);
  163. $friendlyName[0] = strtoupper($friendlyName[0]);
  164. return $friendlyName;
  165. }//end makeFriendlyName()
  166. }//end class
  167. ?>