Skip to content
This repository was archived by the owner on May 25, 2020. It is now read-only.

Commit 3ffda52

Browse files
committed
Implemented PHP_CodeSniffer 1.5 support
1 parent 4556719 commit 3ffda52

File tree

3 files changed

+183
-91
lines changed

3 files changed

+183
-91
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"license": "MIT",
1111
"require": {
1212
"morozov/bootstrap": "1.0.*",
13-
"squizlabs/php_codesniffer": "1.4.*"
13+
"squizlabs/php_codesniffer": "1.5.*"
1414
},
1515
"autoload": {
1616
"files": ["src/functions.php"],

composer.lock

Lines changed: 14 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DiffSniffer/CodeSniffer/Reports/Xml.php

Lines changed: 168 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* @category PHP
88
* @package PHP_CodeSniffer
99
* @author Sergei Morozov <[email protected]>
10-
* @copyright 2013 Sergei Morozov
10+
* @copyright 2014 Sergei Morozov
1111
* @license http://mit-license.org/ MIT Licence
12-
* @link http://github.com/morozov/diff-sniffer
12+
* @link http://github.com/morozov/diff-sniffer-core
1313
*/
1414

1515
/**
@@ -20,121 +20,146 @@
2020
* @category PHP
2121
* @package PHP_CodeSniffer
2222
* @author Sergei Morozov <[email protected]>
23-
* @copyright 2013 Sergei Morozov
23+
* @copyright 2014 Sergei Morozov
2424
* @license http://mit-license.org/ MIT Licence
25-
* @link http://github.com/morozov/diff-sniffer
25+
* @link http://github.com/morozov/diff-sniffer-core
2626
*/
2727
class PHP_CodeSniffer_Reports_Xml implements PHP_CodeSniffer_Report
2828
{
2929
/**
30-
* Prints errors and warnings found only in lines modified in commit.
30+
* Temporary diff file path.
3131
*
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.
3338
*
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
3869
*
3970
* @return string
71+
* @throws PHP_CodeSniffer_Exception
4072
*/
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])) {
4876
throw new PHP_CodeSniffer_Exception(
49-
'PHPCS_DIFF_PATH environment variable is not set'
77+
$varName . ' environment variable is not set'
5078
);
5179
}
52-
$diffPath = $_SERVER['PHPCS_DIFF_PATH'];
53-
$diff = file_get_contents($diffPath);
5480

55-
if (!isset($_SERVER['PHPCS_DIFF_PATH'])) {
81+
$path = realpath($_SERVER[$varName]);
82+
83+
if (false === $path) {
5684
throw new PHP_CodeSniffer_Exception(
57-
'PHPCS_DIFF_PATH environment variable is not set'
85+
$_SERVER[$varName] . ' path does not exist'
5886
);
5987
}
60-
$baseDir = $_SERVER['PHPCS_BASE_DIR'];
6188

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;
6690
}
6791

6892
/**
69-
* Filters report producing another one containing only changed lines
93+
* Generate a partial report for a single processed file.
7094
*
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.
7498
*
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
76104
*/
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();
79111
$changes = $this->getChanges($diff);
80112

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);
93114

94-
return $this->getReport($files);
115+
$full = new PHP_CodeSniffer_Reports_Full();
116+
return $full->generateFileReport($report, $showSources, $width);
95117
}
96118

97119
/**
98-
* Generates report from file data
120+
* Prints all errors and warnings for each file processed.
99121
*
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?
101130
*
102-
* @return array
131+
* @return void
103132
*/
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;
132149
}
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);
133161

134-
return array(
135-
'totals' => $totals,
136-
'files' => $files,
137-
);
162+
return $contents;
138163
}
139164

140165
/**
@@ -168,4 +193,65 @@ protected function getChanges($lines)
168193
}
169194
return $changes;
170195
}
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+
}
171257
}

0 commit comments

Comments
 (0)