Skip to content

Commit 9ede4df

Browse files
committed
Squiz SuperfluousWhitespaceSniff now has an option to ignore blank lines. PSR2 makes use of this new feature.
1 parent b70ffaa commit 9ede4df

7 files changed

+66
-3
lines changed

CodeSniffer/Standards/PSR2/ruleset.xml

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
</rule>
3636

3737
<!-- There MUST NOT be trailing whitespace at the end of non-blank lines. -->
38+
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
39+
<properties>
40+
<property name="ignoreBlankLines" value="true"/>
41+
</properties>
42+
</rule>
3843
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.StartFile">
3944
<severity>0</severity>
4045
</rule>

CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php

+18
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ class Squiz_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff implements PHP_CodeSnif
4343
'CSS',
4444
);
4545

46+
/**
47+
* If TRUE, whitespace rules are not checked for blank lines.
48+
*
49+
* Blank lines are those that contain only whitespace.
50+
*
51+
* @var boolean
52+
*/
53+
public $ignoreBlankLines = false;
54+
55+
4656
/**
4757
* Returns an array of tokens this test wants to listen for.
4858
*
@@ -164,10 +174,18 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
164174
Check for end of line whitespace.
165175
*/
166176

177+
// Ignore whitespace that is not at the end of a line.
167178
if (strpos($tokens[$stackPtr]['content'], $phpcsFile->eolChar) === false) {
168179
return;
169180
}
170181

182+
// Ignore blank lines if required.
183+
if ($this->ignoreBlankLines === true
184+
&& $tokens[($stackPtr - 1)]['line'] !== $tokens[$stackPtr]['line']
185+
) {
186+
return;
187+
}
188+
171189
$tokenContent = rtrim($tokens[$stackPtr]['content'], $phpcsFile->eolChar);
172190
if (empty($tokenContent) === false) {
173191
if (preg_match('|^.*\s+$|', $tokenContent) !== 0) {

CodeSniffer/Standards/Squiz/Tests/WhiteSpace/SuperfluousWhitespaceUnitTest.css

+11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@
88
.HelpWidgetType-new-bug-title {
99
float: left;
1010
}
11+
1112
.HelpWidgetType-new-bug-title{
1213
float: left;
1314
}
1415

16+
/* @codingStandardsChangeSetting Squiz.WhiteSpace.SuperfluousWhitespace ignoreBlankLines true */
17+
.HelpWidgetType-new-bug-title{
18+
float: left;
19+
}
20+
21+
.HelpWidgetType-new-bug-title{
22+
float: left;
23+
}
24+
/* @codingStandardsChangeSetting Squiz.WhiteSpace.SuperfluousWhitespace ignoreBlankLines false */
25+

CodeSniffer/Standards/Squiz/Tests/WhiteSpace/SuperfluousWhitespaceUnitTest.inc

+12
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,17 @@ function myFunction2()
3333

3434
}
3535

36+
// @codingStandardsChangeSetting Squiz.WhiteSpace.SuperfluousWhitespace ignoreBlankLines true
37+
38+
function myFunction2()
39+
{
40+
echo 'code here';
41+
42+
echo 'code here';
43+
44+
}
45+
46+
// @codingStandardsChangeSetting Squiz.WhiteSpace.SuperfluousWhitespace ignoreBlankLines false
47+
3648
?>
3749

CodeSniffer/Standards/Squiz/Tests/WhiteSpace/SuperfluousWhitespaceUnitTest.js

+12
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,16 @@ function myFunction2()
3232

3333
}
3434

35+
// @codingStandardsChangeSetting Squiz.WhiteSpace.SuperfluousWhitespace ignoreBlankLines true
36+
37+
function myFunction2()
38+
{
39+
alert('code here');
40+
41+
alert('code here');
42+
43+
}
44+
45+
// @codingStandardsChangeSetting Squiz.WhiteSpace.SuperfluousWhitespace ignoreBlankLines false
46+
3547

CodeSniffer/Standards/Squiz/Tests/WhiteSpace/SuperfluousWhitespaceUnitTest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function getErrorList($testFile='SuperfluousWhitespaceUnitTest.inc')
5555
16 => 1,
5656
23 => 1,
5757
30 => 1,
58-
36 => 1,
58+
48 => 1,
5959
);
6060
break;
6161
case 'SuperfluousWhitespaceUnitTest.js':
@@ -68,15 +68,16 @@ public function getErrorList($testFile='SuperfluousWhitespaceUnitTest.inc')
6868
15 => 1,
6969
22 => 1,
7070
29 => 1,
71-
35 => 1,
71+
47 => 1,
7272
);
7373
break;
7474
case 'SuperfluousWhitespaceUnitTest.css':
7575
return array(
7676
1 => 1,
7777
8 => 1,
7878
9 => 1,
79-
14 => 1,
79+
11 => 1,
80+
25 => 1,
8081
);
8182
break;
8283
default:

package.xml

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
3434
-- Thanks to Andy Grunwald for the patch
3535
- Squiz FileCommentSniff now uses tags names inside the error codes for many messages
3636
-- This allows you to exclude specific missing, out of order etc., tags
37+
- Squiz SuperfluousWhitespaceSniff now has an option to ignore blank lines
38+
-- This will stop errors being reported for lines that contain only whitespace
39+
-- Set the ignoreBlankLines property to true in your ruleset.xml file to enable this
40+
- PSR2 no longer reports errors for whitespace at the end of blank lines
3741
- Fixed gitblame report not working on Windows
3842
-- Thanks to Rogerio Prado de Jesus
3943
- Fixed an incorrect error in Squiz OperatorSpacingSniff for default values inside a closure definition

0 commit comments

Comments
 (0)