Skip to content

Commit ea18874

Browse files
committed
Squiz.WhiteSpace.MemberVarSpacing now has settings for spacing between member vars and spacing before the first member var (refs #1355 and #725)
1 parent 5302fbb commit ea18874

File tree

5 files changed

+169
-8
lines changed

5 files changed

+169
-8
lines changed

package.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
5252
- Function call sniffs now check variable function names and self/static object creation
5353
-- Specific sniffs are Generic.Functions.FunctionCallArgumentSpacing, PEAR.Functions.FunctionCallSignature, and PSR2.Methods.FunctionCallSignature
5454
-- Thanks to Michał Bundyra for the patch
55-
- Generic.Files.LineLength can now be configure to ignore all comment lines, no matter their length
55+
- Generic.Files.LineLength can now be configured to ignore all comment lines, no matter their length
5656
-- Set the ignoreComments property to TRUE (default is FALSE) in your ruleset.xml file to enable this
5757
-- Thanks to Juliette Reinders Folmer for the patch
5858
- PEAR.Functions.FunctionDeclaration now removes a blank line if it creates one by moving the curly brace during fixing
@@ -63,6 +63,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
6363
- Squiz.WhiteSpace.ControlStructureSpacing now ignores comments when checking blank lines at the top of control structures
6464
- Squiz.WhiteSpace.ObjectOperatorSpacing now detects and fixes spaces around double colons
6565
-- Thanks to Julius Šmatavičius for the patch
66+
- Squiz.WhiteSpace.MemberVarSpacing can now be configured to check any number of blank lines between member vars
67+
-- Set the spacing property (default is 1) in your ruleset.xml file to set the spacing
68+
- Squiz.WhiteSpace.MemberVarSpacing can now be configured to check a different number of blank lines before the first member var
69+
-- Set the spacingBeforeFirst property (default is 1) in your ruleset.xml file to set the spacing
6670
- Added a new PHP_CodeSniffer\Util\Tokens::$ooScopeTokens static member var for quickly checking object scope
6771
-- Includes T_CLASS, T_ANON_CLASS, T_INTERFACE, and T_TRAIT
6872
-- Thanks to Juliette Reinders Folmer for the patch

src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@
1616
class MemberVarSpacingSniff extends AbstractVariableSniff
1717
{
1818

19+
/**
20+
* The number of blank lines between member vars.
21+
*
22+
* @var integer
23+
*/
24+
public $spacing = 1;
25+
26+
/**
27+
* The number of blank lines before the fist member var.
28+
*
29+
* @var integer
30+
*/
31+
public $spacingBeforeFirst = 1;
32+
1933

2034
/**
2135
* Processes the function tokens within the class.
@@ -70,7 +84,7 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
7084
}//end if
7185
}//end if
7286

73-
// There needs to be 1 blank line before the var, not counting comments.
87+
// There needs to be n blank lines before the var, not counting comments.
7488
if ($start === $stackPtr) {
7589
// No comment found.
7690
$first = $phpcsFile->findFirstOnLine(Tokens::$emptyTokens, $start, true);
@@ -84,15 +98,31 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
8498
$first = $phpcsFile->findNext(Tokens::$commentTokens, ($first + 1));
8599
}
86100

87-
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($first - 1), null, true);
101+
// Determine if this is the first member var.
102+
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($first - 1), null, true);
103+
if ($tokens[$prev]['code'] === T_OPEN_CURLY_BRACKET
104+
&& isset(Tokens::$ooScopeTokens[$tokens[$tokens[$prev]['scope_condition']]['code']]) === true
105+
) {
106+
$errorMsg = 'Expected %s blank line(s) before first member var; %s found';
107+
$errorCode = 'FirstIncorrect';
108+
$spacing = (int) $this->spacingBeforeFirst;
109+
} else {
110+
$errorMsg = 'Expected %s blank line(s) before member var; %s found';
111+
$errorCode = 'Incorrect';
112+
$spacing = (int) $this->spacing;
113+
}
114+
88115
$foundLines = ($tokens[$first]['line'] - $tokens[$prev]['line'] - 1);
89-
if ($foundLines === 1) {
116+
if ($foundLines === $spacing) {
90117
return;
91118
}
92119

93-
$error = 'Expected 1 blank line before member var; %s found';
94-
$data = array($foundLines);
95-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);
120+
$data = array(
121+
$spacing,
122+
$foundLines,
123+
);
124+
125+
$fix = $phpcsFile->addFixableError($errorMsg, $stackPtr, $errorCode, $data);
96126
if ($fix === true) {
97127
$phpcsFile->fixer->beginChangeset();
98128
for ($i = ($prev + 1); $i < $first; $i++) {
@@ -101,7 +131,10 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
101131
}
102132

103133
if ($tokens[$i]['line'] === $tokens[$first]['line']) {
104-
$phpcsFile->fixer->addNewline(($i - 1));
134+
for ($x = 1; $x <= $spacing; $x++) {
135+
$phpcsFile->fixer->addNewlineBefore($i);
136+
}
137+
105138
break;
106139
}
107140

src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.inc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,63 @@ class Foo
152152
private $caseStudy = null;
153153

154154
}
155+
156+
// @codingStandardsChangeSetting Squiz.WhiteSpace.MemberVarSpacing spacing 2
157+
158+
class MyClass
159+
{
160+
public $var1 = 'value';
161+
162+
163+
public $var2 = 'value';
164+
165+
public $var3 = 'value';
166+
167+
}//end class
168+
169+
// @codingStandardsChangeSetting Squiz.WhiteSpace.MemberVarSpacing spacing 1
170+
// @codingStandardsChangeSetting Squiz.WhiteSpace.MemberVarSpacing spacingBeforeFirst 0
171+
172+
class MyClass
173+
{
174+
public $var1 = 'value';
175+
176+
177+
public $var2 = 'value';
178+
179+
public $var3 = 'value';
180+
181+
}//end class
182+
183+
class MyClass
184+
{
185+
186+
public $var1 = 'value';
187+
188+
public $var2 = 'value';
189+
190+
public $var3 = 'value';
191+
192+
}//end class
193+
194+
// @codingStandardsChangeSetting Squiz.WhiteSpace.MemberVarSpacing spacing 0
195+
196+
class MyClass
197+
{
198+
public $var1 = 'value';
199+
200+
public $var2 = 'value';
201+
public $var3 = 'value';
202+
203+
}//end class
204+
205+
interface MyInterface
206+
{
207+
208+
/* testing */
209+
public $var1 = 'value';
210+
211+
public $var2 = 'value';
212+
public $var3 = 'value';
213+
214+
}//end class

src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.inc.fixed

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,60 @@ class Foo
150150
private $caseStudy = null;
151151

152152
}
153+
154+
// @codingStandardsChangeSetting Squiz.WhiteSpace.MemberVarSpacing spacing 2
155+
156+
class MyClass
157+
{
158+
159+
public $var1 = 'value';
160+
161+
162+
public $var2 = 'value';
163+
164+
165+
public $var3 = 'value';
166+
167+
}//end class
168+
169+
// @codingStandardsChangeSetting Squiz.WhiteSpace.MemberVarSpacing spacing 1
170+
// @codingStandardsChangeSetting Squiz.WhiteSpace.MemberVarSpacing spacingBeforeFirst 0
171+
172+
class MyClass
173+
{
174+
public $var1 = 'value';
175+
176+
public $var2 = 'value';
177+
178+
public $var3 = 'value';
179+
180+
}//end class
181+
182+
class MyClass
183+
{
184+
public $var1 = 'value';
185+
186+
public $var2 = 'value';
187+
188+
public $var3 = 'value';
189+
190+
}//end class
191+
192+
// @codingStandardsChangeSetting Squiz.WhiteSpace.MemberVarSpacing spacing 0
193+
194+
class MyClass
195+
{
196+
public $var1 = 'value';
197+
public $var2 = 'value';
198+
public $var3 = 'value';
199+
200+
}//end class
201+
202+
interface MyInterface
203+
{
204+
/* testing */
205+
public $var1 = 'value';
206+
public $var2 = 'value';
207+
public $var3 = 'value';
208+
209+
}//end class

src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public function getErrorList()
3838
106 => 1,
3939
115 => 1,
4040
150 => 1,
41+
160 => 1,
42+
165 => 1,
43+
177 => 1,
44+
186 => 1,
45+
200 => 1,
46+
209 => 1,
47+
211 => 1,
4148
);
4249

4350
}//end getErrorList()

0 commit comments

Comments
 (0)