PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/squizlabs/php_codesniffer/CodeSniffer/Reporting.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 318 lines | 175 code | 52 blank | 91 comment | 28 complexity | 12f15a2248a0823f6ead2c40f0e7f34d 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-2014 SQLI <www.sqli.com>
  12. * @copyright 2006-2014 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-2014 SQLI <www.sqli.com>
  29. * @copyright 2006-2014 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: @package_version@
  32. * @link http://pear.php.net/package/PHP_CodeSniffer
  33. */
  34. class PHP_CodeSniffer_Reporting
  35. {
  36. /**
  37. * Total number of files that contain errors or warnings.
  38. *
  39. * @var int
  40. */
  41. public $totalFiles = 0;
  42. /**
  43. * Total number of errors found during the run.
  44. *
  45. * @var int
  46. */
  47. public $totalErrors = 0;
  48. /**
  49. * Total number of warnings found during the run.
  50. *
  51. * @var int
  52. */
  53. public $totalWarnings = 0;
  54. /**
  55. * A list of reports that have written partial report output.
  56. *
  57. * @var array
  58. */
  59. private $_cachedReports = array();
  60. /**
  61. * A cache of report objects.
  62. *
  63. * @var array
  64. */
  65. private $_reports = array();
  66. /**
  67. * Produce the appropriate report object based on $type parameter.
  68. *
  69. * @param string $type The type of the report.
  70. *
  71. * @return PHP_CodeSniffer_Report
  72. * @throws PHP_CodeSniffer_Exception If report is not available.
  73. */
  74. public function factory($type)
  75. {
  76. $type = ucfirst($type);
  77. if (isset($this->_reports[$type]) === true) {
  78. return $this->_reports[$type];
  79. }
  80. $filename = $type.'.php';
  81. $reportClassName = 'PHP_CodeSniffer_Reports_'.$type;
  82. if (class_exists($reportClassName, true) === false) {
  83. throw new PHP_CodeSniffer_Exception('Report type "'.$type.'" not found.');
  84. }
  85. $reportClass = new $reportClassName();
  86. if (false === ($reportClass instanceof PHP_CodeSniffer_Report)) {
  87. throw new PHP_CodeSniffer_Exception('Class "'.$reportClassName.'" must implement the "PHP_CodeSniffer_Report" interface.');
  88. }
  89. $this->_reports[$type] = $reportClass;
  90. return $this->_reports[$type];
  91. }//end factory()
  92. /**
  93. * Actually generates the report.
  94. *
  95. * @param PHP_CodeSniffer_File $phpcsFile The file that has been processed.
  96. * @param array $cliValues An array of command line arguments.
  97. *
  98. * @return void
  99. */
  100. public function cacheFileReport(PHP_CodeSniffer_File $phpcsFile, array $cliValues)
  101. {
  102. if (isset($cliValues['reports']) === false) {
  103. // This happens during unit testing, or any time someone just wants
  104. // the error data and not the printed report.
  105. return;
  106. }
  107. $reportData = $this->prepareFileReport($phpcsFile);
  108. $errorsShown = false;
  109. foreach ($cliValues['reports'] as $report => $output) {
  110. $reportClass = self::factory($report);
  111. ob_start();
  112. $result = $reportClass->generateFileReport($reportData, $cliValues['showSources'], $cliValues['reportWidth']);
  113. if ($result === true) {
  114. $errorsShown = true;
  115. }
  116. $generatedReport = ob_get_contents();
  117. ob_end_clean();
  118. if ($generatedReport !== '') {
  119. $flags = FILE_APPEND;
  120. if (in_array($report, $this->_cachedReports) === false) {
  121. $this->_cachedReports[] = $report;
  122. $flags = null;
  123. }
  124. if ($output === null) {
  125. if ($cliValues['reportFile'] !== null) {
  126. $output = $cliValues['reportFile'];
  127. } else {
  128. $output = sys_get_temp_dir().'/phpcs-'.$report.'.tmp';
  129. }
  130. }
  131. file_put_contents($output, $generatedReport, $flags);
  132. }
  133. }//end foreach
  134. if ($errorsShown === true) {
  135. $this->totalFiles++;
  136. $this->totalErrors += $reportData['errors'];
  137. $this->totalWarnings += $reportData['warnings'];
  138. }
  139. }//end cacheFileReport()
  140. /**
  141. * Actually generates the report.
  142. *
  143. * @param string $report Report type.
  144. * @param boolean $showSources Show sources?
  145. * @param string $reportFile Report file to generate.
  146. * @param integer $reportWidth Report max width.
  147. *
  148. * @return integer
  149. */
  150. public function printReport(
  151. $report,
  152. $showSources,
  153. $reportFile='',
  154. $reportWidth=80
  155. ) {
  156. $reportClass = self::factory($report);
  157. if ($reportFile !== null) {
  158. $filename = $reportFile;
  159. $toScreen = false;
  160. ob_start();
  161. } else {
  162. $filename = sys_get_temp_dir().'/phpcs-'.$report.'.tmp';
  163. $toScreen = true;
  164. }
  165. if (file_exists($filename) === true) {
  166. $reportCache = file_get_contents($filename);
  167. } else {
  168. $reportCache = '';
  169. }
  170. $reportClass->generate(
  171. $reportCache,
  172. $this->totalFiles,
  173. $this->totalErrors,
  174. $this->totalWarnings,
  175. $showSources,
  176. $reportWidth,
  177. $toScreen
  178. );
  179. if ($reportFile !== null) {
  180. $generatedReport = ob_get_contents();
  181. ob_end_clean();
  182. if (PHP_CODESNIFFER_VERBOSITY > 0) {
  183. echo $generatedReport;
  184. }
  185. $generatedReport = trim($generatedReport);
  186. file_put_contents($reportFile, $generatedReport.PHP_EOL);
  187. } else if (file_exists($filename) === true) {
  188. unlink($filename);
  189. }
  190. return ($this->totalErrors + $this->totalWarnings);
  191. }//end printReport()
  192. /**
  193. * Pre-process and package violations for all files.
  194. *
  195. * Used by error reports to get a packaged list of all errors in each file.
  196. *
  197. * @param PHP_CodeSniffer_File $phpcsFile The file that has been processed.
  198. *
  199. * @return array
  200. */
  201. public function prepareFileReport(PHP_CodeSniffer_File $phpcsFile)
  202. {
  203. $report = array(
  204. 'filename' => $phpcsFile->getFilename(),
  205. 'errors' => $phpcsFile->getErrorCount(),
  206. 'warnings' => $phpcsFile->getWarningCount(),
  207. 'messages' => array(),
  208. );
  209. if ($report['errors'] === 0 && $report['warnings'] === 0) {
  210. // Prefect score!
  211. return $report;
  212. }
  213. $errors = array();
  214. // Merge errors and warnings.
  215. foreach ($phpcsFile->getErrors() as $line => $lineErrors) {
  216. if (is_array($lineErrors) === false) {
  217. continue;
  218. }
  219. foreach ($lineErrors as $column => $colErrors) {
  220. $newErrors = array();
  221. foreach ($colErrors as $data) {
  222. $newErrors[] = array(
  223. 'message' => $data['message'],
  224. 'source' => $data['source'],
  225. 'severity' => $data['severity'],
  226. 'type' => 'ERROR',
  227. );
  228. }//end foreach
  229. $errors[$line][$column] = $newErrors;
  230. }//end foreach
  231. ksort($errors[$line]);
  232. }//end foreach
  233. foreach ($phpcsFile->getWarnings() as $line => $lineWarnings) {
  234. if (is_array($lineWarnings) === false) {
  235. continue;
  236. }
  237. foreach ($lineWarnings as $column => $colWarnings) {
  238. $newWarnings = array();
  239. foreach ($colWarnings as $data) {
  240. $newWarnings[] = array(
  241. 'message' => $data['message'],
  242. 'source' => $data['source'],
  243. 'severity' => $data['severity'],
  244. 'type' => 'WARNING',
  245. );
  246. }//end foreach
  247. if (isset($errors[$line]) === false) {
  248. $errors[$line] = array();
  249. }
  250. if (isset($errors[$line][$column]) === true) {
  251. $errors[$line][$column] = array_merge(
  252. $newWarnings,
  253. $errors[$line][$column]
  254. );
  255. } else {
  256. $errors[$line][$column] = $newWarnings;
  257. }
  258. }//end foreach
  259. ksort($errors[$line]);
  260. }//end foreach
  261. ksort($errors);
  262. $report['messages'] = $errors;
  263. return $report;
  264. }//end prepareFileReport()
  265. }//end class
  266. ?>