|
7 | 7 | * @category PHP |
8 | 8 | * @package PHP_CodeSniffer |
9 | 9 | * @author Sergei Morozov <[email protected]> |
10 | | - * @copyright 2013 Sergei Morozov |
| 10 | + * @copyright 2014 Sergei Morozov |
11 | 11 | * @license http://mit-license.org/ MIT Licence |
12 | | - * @link http://github.com/morozov/diff-sniffer |
| 12 | + * @link http://github.com/morozov/diff-sniffer-core |
13 | 13 | */ |
14 | 14 |
|
15 | 15 | /** |
|
20 | 20 | * @category PHP |
21 | 21 | * @package PHP_CodeSniffer |
22 | 22 | * @author Sergei Morozov <[email protected]> |
23 | | - * @copyright 2013 Sergei Morozov |
| 23 | + * @copyright 2014 Sergei Morozov |
24 | 24 | * @license http://mit-license.org/ MIT Licence |
25 | | - * @link http://github.com/morozov/diff-sniffer |
| 25 | + * @link http://github.com/morozov/diff-sniffer-core |
26 | 26 | */ |
27 | 27 | class PHP_CodeSniffer_Reports_Xml implements PHP_CodeSniffer_Report |
28 | 28 | { |
29 | 29 | /** |
30 | | - * Prints errors and warnings found only in lines modified in commit. |
| 30 | + * Temporary diff file path. |
31 | 31 | * |
32 | | - * Errors and warnings are displayed together, grouped by file. |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + protected $diffPath; |
| 35 | + |
| 36 | + /** |
| 37 | + * The directory where the files to be checked reside. |
33 | 38 | * |
34 | | - * @param array $report Prepared report. |
35 | | - * @param boolean $showSources Show sources? |
36 | | - * @param int $width Maximum allowed lne width. |
37 | | - * @param boolean $toScreen Is the report being printed to screen? |
| 39 | + * @var string |
| 40 | + */ |
| 41 | + protected $baseDir; |
| 42 | + |
| 43 | + /** |
| 44 | + * Constructor. |
| 45 | + */ |
| 46 | + public function __construct() |
| 47 | + { |
| 48 | + $diffPath = $this->getPath('PHPCS_DIFF_PATH'); |
| 49 | + if (!is_file($diffPath)) { |
| 50 | + throw new PHP_CodeSniffer_Exception( |
| 51 | + $diffPath . ' is not a file' |
| 52 | + ); |
| 53 | + } |
| 54 | + $this->diffPath = $diffPath; |
| 55 | + |
| 56 | + $baseDir = $this->getPath('PHPCS_BASE_DIR'); |
| 57 | + if (!is_dir($baseDir)) { |
| 58 | + throw new PHP_CodeSniffer_Exception( |
| 59 | + $diffPath . ' is not a directory' |
| 60 | + ); |
| 61 | + } |
| 62 | + $this->baseDir = $baseDir; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Retrieves the path specified by environment variable. |
| 67 | + * |
| 68 | + * @param string $varName Environment variable name |
38 | 69 | * |
39 | 70 | * @return string |
| 71 | + * @throws PHP_CodeSniffer_Exception |
40 | 72 | */ |
41 | | - public function generate( |
42 | | - $report, |
43 | | - $showSources = false, |
44 | | - $width = 80, |
45 | | - $toScreen = true |
46 | | - ) { |
47 | | - if (!isset($_SERVER['PHPCS_DIFF_PATH'])) { |
| 73 | + protected function getPath($varName) |
| 74 | + { |
| 75 | + if (!isset($_SERVER[$varName])) { |
48 | 76 | throw new PHP_CodeSniffer_Exception( |
49 | | - 'PHPCS_DIFF_PATH environment variable is not set' |
| 77 | + $varName . ' environment variable is not set' |
50 | 78 | ); |
51 | 79 | } |
52 | | - $diffPath = $_SERVER['PHPCS_DIFF_PATH']; |
53 | | - $diff = file_get_contents($diffPath); |
54 | 80 |
|
55 | | - if (!isset($_SERVER['PHPCS_DIFF_PATH'])) { |
| 81 | + $path = realpath($_SERVER[$varName]); |
| 82 | + |
| 83 | + if (false === $path) { |
56 | 84 | throw new PHP_CodeSniffer_Exception( |
57 | | - 'PHPCS_DIFF_PATH environment variable is not set' |
| 85 | + $_SERVER[$varName] . ' path does not exist' |
58 | 86 | ); |
59 | 87 | } |
60 | | - $baseDir = $_SERVER['PHPCS_BASE_DIR']; |
61 | 88 |
|
62 | | - $report = $this->filter($report, $baseDir, $diff); |
63 | | - |
64 | | - $full = new PHP_CodeSniffer_Reports_Full(); |
65 | | - return $full->generate($report, $showSources, $width, $toScreen); |
| 89 | + return $path; |
66 | 90 | } |
67 | 91 |
|
68 | 92 | /** |
69 | | - * Filters report producing another one containing only changed lines |
| 93 | + * Generate a partial report for a single processed file. |
70 | 94 | * |
71 | | - * @param array $report Original report |
72 | | - * @param string $baseDir The directory where the files are located |
73 | | - * @param string $diff Diff that should be used for filtering |
| 95 | + * Function should return TRUE if it printed or stored data about the file |
| 96 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
| 97 | + * its data should be counted in the grand totals. |
74 | 98 | * |
75 | | - * @return array |
| 99 | + * @param array $report Prepared report data. |
| 100 | + * @param boolean $showSources Show sources? |
| 101 | + * @param int $width Maximum allowed line width. |
| 102 | + * |
| 103 | + * @return boolean |
76 | 104 | */ |
77 | | - protected function filter(array $report, $baseDir, $diff) |
78 | | - { |
| 105 | + public function generateFileReport( |
| 106 | + $report, |
| 107 | + $showSources=false, |
| 108 | + $width=80 |
| 109 | + ) { |
| 110 | + $diff = $this->getStagedDiff(); |
79 | 111 | $changes = $this->getChanges($diff); |
80 | 112 |
|
81 | | - $files = array(); |
82 | | - foreach ($changes as $relPath => $lines) { |
83 | | - $absPath = $baseDir . DIRECTORY_SEPARATOR |
84 | | - . str_replace('/', DIRECTORY_SEPARATOR, $relPath); |
85 | | - |
86 | | - if (isset($report['files'][$absPath])) { |
87 | | - $files[$relPath]['messages'] = array_intersect_key( |
88 | | - $report['files'][$absPath]['messages'], |
89 | | - array_flip($lines) |
90 | | - ); |
91 | | - } |
92 | | - } |
| 113 | + $report = $this->filterReport($report, $changes); |
93 | 114 |
|
94 | | - return $this->getReport($files); |
| 115 | + $full = new PHP_CodeSniffer_Reports_Full(); |
| 116 | + return $full->generateFileReport($report, $showSources, $width); |
95 | 117 | } |
96 | 118 |
|
97 | 119 | /** |
98 | | - * Generates report from file data |
| 120 | + * Prints all errors and warnings for each file processed. |
99 | 121 | * |
100 | | - * @param array $files File data |
| 122 | + * @param string $cachedData Any partial report data that was returned from |
| 123 | + * generateFileReport during the run. |
| 124 | + * @param int $totalFiles Total number of files processed during the run. |
| 125 | + * @param int $totalErrors Total number of errors found during the run. |
| 126 | + * @param int $totalWarnings Total number of warnings found during the run. |
| 127 | + * @param boolean $showSources Show sources? |
| 128 | + * @param int $width Maximum allowed line width. |
| 129 | + * @param boolean $toScreen Is the report being printed to screen? |
101 | 130 | * |
102 | | - * @return array |
| 131 | + * @return void |
103 | 132 | */ |
104 | | - protected function getReport(array $files) |
105 | | - { |
106 | | - $totals = array( |
107 | | - 'warnings' => 0, |
108 | | - 'errors' => 0, |
109 | | - ); |
110 | | - |
111 | | - foreach ($files as $path => $file) { |
112 | | - $files[$path] = array_merge($file, $totals); |
113 | | - foreach ($file['messages'] as $columns) { |
114 | | - foreach ($columns as $messages) { |
115 | | - foreach ($messages as $message) { |
116 | | - switch ($message['type']) { |
117 | | - case 'ERROR': |
118 | | - $key = 'errors'; |
119 | | - break; |
120 | | - case 'WARNING': |
121 | | - $key = 'warnings'; |
122 | | - break; |
123 | | - default: |
124 | | - $key = null; |
125 | | - continue; |
126 | | - } |
127 | | - $files[$path][$key]++; |
128 | | - $totals[$key]++; |
129 | | - } |
130 | | - } |
131 | | - } |
| 133 | + public function generate( |
| 134 | + $cachedData, |
| 135 | + $totalFiles, |
| 136 | + $totalErrors, |
| 137 | + $totalWarnings, |
| 138 | + $showSources=false, |
| 139 | + $width=80, |
| 140 | + $toScreen=true |
| 141 | + ) { |
| 142 | + echo $cachedData; |
| 143 | + |
| 144 | + if ($toScreen === true |
| 145 | + && PHP_CODESNIFFER_INTERACTIVE === false |
| 146 | + && class_exists('PHP_Timer', false) === true |
| 147 | + ) { |
| 148 | + echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL; |
132 | 149 | } |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Returns diff contents |
| 154 | + * |
| 155 | + * @return string |
| 156 | + * @throws PHP_CodeSniffer_Exception |
| 157 | + */ |
| 158 | + protected function getStagedDiff() |
| 159 | + { |
| 160 | + $contents = file_get_contents($this->diffPath); |
133 | 161 |
|
134 | | - return array( |
135 | | - 'totals' => $totals, |
136 | | - 'files' => $files, |
137 | | - ); |
| 162 | + return $contents; |
138 | 163 | } |
139 | 164 |
|
140 | 165 | /** |
@@ -168,4 +193,65 @@ protected function getChanges($lines) |
168 | 193 | } |
169 | 194 | return $changes; |
170 | 195 | } |
| 196 | + |
| 197 | + /** |
| 198 | + * Filters report producing another one containing only changed lines |
| 199 | + * |
| 200 | + * @param array $report Original report |
| 201 | + * @param array $changes Staged changes |
| 202 | + * |
| 203 | + * @return array |
| 204 | + */ |
| 205 | + protected function filterReport(array $report, array $changes) |
| 206 | + { |
| 207 | + $filename = $report['filename']; |
| 208 | + if (strpos($filename, $this->baseDir . DIRECTORY_SEPARATOR) === 0) { |
| 209 | + $relative = substr($filename, strlen($this->baseDir) + 1); |
| 210 | + if (isset($changes[$relative])) { |
| 211 | + $report['filename'] = $relative; |
| 212 | + $report['messages'] = array_intersect_key( |
| 213 | + $report['messages'], |
| 214 | + array_flip($changes[$relative]) |
| 215 | + ); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + return $this->repairReport($report); |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * Generates report from file data |
| 224 | + * |
| 225 | + * @param array $report File data |
| 226 | + * |
| 227 | + * @return array |
| 228 | + */ |
| 229 | + protected function repairReport(array $report) |
| 230 | + { |
| 231 | + $repaired = array_merge($report, array( |
| 232 | + 'errors' => 0, |
| 233 | + 'warnings' => 0, |
| 234 | + )); |
| 235 | + |
| 236 | + foreach ($report['messages'] as $columns) { |
| 237 | + foreach ($columns as $messages) { |
| 238 | + foreach ($messages as $message) { |
| 239 | + switch($message['type']) { |
| 240 | + case 'ERROR'; |
| 241 | + $key = 'errors'; |
| 242 | + break; |
| 243 | + case 'WARNING'; |
| 244 | + $key = 'warnings'; |
| 245 | + break; |
| 246 | + default; |
| 247 | + $key = null; |
| 248 | + continue; |
| 249 | + } |
| 250 | + $repaired[$key]++; |
| 251 | + } |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + return $repaired; |
| 256 | + } |
171 | 257 | } |
0 commit comments