/tests/lib/PHP/CodeSniffer/Reporting.php

https://github.com/timglabisch/pimcore · PHP · 231 lines · 134 code · 38 blank · 59 comment · 17 complexity · 8bdd12add8a997c32f3892cfed727ea8 MD5 · raw file

  1. <?php
  2. /**
  3. * A class to manage reporting.
  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. if (is_file(dirname(__FILE__).'/../CodeSniffer.php') === true) {
  17. include_once dirname(__FILE__).'/../CodeSniffer.php';
  18. } else {
  19. include_once 'PHP/CodeSniffer.php';
  20. }
  21. /**
  22. * A class to manage reporting.
  23. *
  24. * @category PHP
  25. * @package PHP_CodeSniffer
  26. * @author Gabriele Santini <gsantini@sqli.com>
  27. * @author Greg Sherwood <gsherwood@squiz.net>
  28. * @copyright 2009 SQLI <www.sqli.com>
  29. * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
  30. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  31. * @version Release: 1.4.3
  32. * @link http://pear.php.net/package/PHP_CodeSniffer
  33. */
  34. class PHP_CodeSniffer_Reporting
  35. {
  36. /**
  37. * Produce the appropriate report object based on $type parameter.
  38. *
  39. * @param string $type Demanded report type.
  40. *
  41. * @return PHP_CodeSniffer_Report
  42. * @throws PHP_CodeSniffer_Exception If report is not available.
  43. */
  44. public function factory($type)
  45. {
  46. $type = ucfirst($type);
  47. $filename = $type.'.php';
  48. $reportClassName = 'PHP_CodeSniffer_Reports_'.$type;
  49. if (class_exists($reportClassName, true) === false) {
  50. throw new PHP_CodeSniffer_Exception('Report type "'.$type.'" not found.');
  51. }
  52. $reportClass = new $reportClassName();
  53. if (false === ($reportClass instanceof PHP_CodeSniffer_Report)) {
  54. throw new PHP_CodeSniffer_Exception('Class "'.$reportClassName.'" must implement the "PHP_CodeSniffer_Report" interface.');
  55. }
  56. return $reportClass;
  57. }//end factory()
  58. /**
  59. * Actually generates the report.
  60. *
  61. * @param string $report Report type.
  62. * @param array $filesViolations Collected violations.
  63. * @param boolean $showSources Show sources?
  64. * @param string $reportFile Report file to generate.
  65. * @param integer $reportWidth Report max width.
  66. *
  67. * @return integer
  68. */
  69. public function printReport(
  70. $report,
  71. $filesViolations,
  72. $showSources,
  73. $reportFile='',
  74. $reportWidth=80
  75. ) {
  76. if ($reportFile !== null) {
  77. $reportDir = dirname($reportFile);
  78. if ($reportDir === '.') {
  79. // Passed report file is a filename in the current directory.
  80. $reportFile = PHPCS_CWD.'/'.basename($reportFile);
  81. } else {
  82. $reportDir = realpath(PHPCS_CWD.'/'.$reportDir);
  83. if ($reportDir !== false) {
  84. // Report file path is relative.
  85. $reportFile = $reportDir.'/'.basename($reportFile);
  86. }
  87. }
  88. }
  89. $reportClass = self::factory($report);
  90. $reportData = $this->prepare($filesViolations);
  91. $toScreen = true;
  92. if ($reportFile !== null) {
  93. $toScreen = false;
  94. ob_start();
  95. }
  96. $numErrors = $reportClass->generate($reportData, $showSources, $reportWidth, $toScreen);
  97. if ($reportFile !== null) {
  98. $generatedReport = ob_get_contents();
  99. if (PHP_CODESNIFFER_VERBOSITY > 0) {
  100. ob_end_flush();
  101. } else {
  102. ob_end_clean();
  103. }
  104. $generatedReport = trim($generatedReport);
  105. file_put_contents($reportFile, $generatedReport.PHP_EOL);
  106. }
  107. return $numErrors;
  108. }//end printReport()
  109. /**
  110. * Pre-process and package violations for all files.
  111. *
  112. * Used by error reports to get a packaged list of all errors in each file.
  113. *
  114. * @param array $filesViolations List of found violations.
  115. *
  116. * @return array
  117. */
  118. public function prepare(array $filesViolations)
  119. {
  120. $report = array(
  121. 'totals' => array(
  122. 'warnings' => 0,
  123. 'errors' => 0,
  124. ),
  125. 'files' => array(),
  126. );
  127. foreach ($filesViolations as $filename => $fileViolations) {
  128. $warnings = $fileViolations['warnings'];
  129. $errors = $fileViolations['errors'];
  130. $numWarnings = $fileViolations['numWarnings'];
  131. $numErrors = $fileViolations['numErrors'];
  132. $report['files'][$filename] = array(
  133. 'errors' => 0,
  134. 'warnings' => 0,
  135. 'messages' => array(),
  136. );
  137. if ($numErrors === 0 && $numWarnings === 0) {
  138. // Prefect score!
  139. continue;
  140. }
  141. $report['files'][$filename]['errors'] = $numErrors;
  142. $report['files'][$filename]['warnings'] = $numWarnings;
  143. $report['totals']['errors'] += $numErrors;
  144. $report['totals']['warnings'] += $numWarnings;
  145. // Merge errors and warnings.
  146. foreach ($errors as $line => $lineErrors) {
  147. foreach ($lineErrors as $column => $colErrors) {
  148. $newErrors = array();
  149. foreach ($colErrors as $data) {
  150. $newErrors[] = array(
  151. 'message' => $data['message'],
  152. 'source' => $data['source'],
  153. 'severity' => $data['severity'],
  154. 'type' => 'ERROR',
  155. );
  156. }//end foreach
  157. $errors[$line][$column] = $newErrors;
  158. }//end foreach
  159. ksort($errors[$line]);
  160. }//end foreach
  161. foreach ($warnings as $line => $lineWarnings) {
  162. foreach ($lineWarnings as $column => $colWarnings) {
  163. $newWarnings = array();
  164. foreach ($colWarnings as $data) {
  165. $newWarnings[] = array(
  166. 'message' => $data['message'],
  167. 'source' => $data['source'],
  168. 'severity' => $data['severity'],
  169. 'type' => 'WARNING',
  170. );
  171. }//end foreach
  172. if (isset($errors[$line]) === false) {
  173. $errors[$line] = array();
  174. }
  175. if (isset($errors[$line][$column]) === true) {
  176. $errors[$line][$column] = array_merge(
  177. $newWarnings,
  178. $errors[$line][$column]
  179. );
  180. } else {
  181. $errors[$line][$column] = $newWarnings;
  182. }
  183. }//end foreach
  184. ksort($errors[$line]);
  185. }//end foreach
  186. ksort($errors);
  187. $report['files'][$filename]['messages'] = $errors;
  188. }//end foreach
  189. ksort($report['files']);
  190. return $report;
  191. }//end prepare()
  192. }//end class
  193. ?>