Skip to content

Commit 199d465

Browse files
committed
Further fix for bug #414
1 parent b381e77 commit 199d465

File tree

4 files changed

+191
-34
lines changed

4 files changed

+191
-34
lines changed

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

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
4949
$ignore[] = T_VAR;
5050
$ignore[] = T_WHITESPACE;
5151

52-
$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
52+
$start = $stackPtr;
53+
$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
5354
if (isset(PHP_CodeSniffer_Tokens::$commentTokens[$tokens[$prev]['code']]) === true) {
5455
// Assume the comment belongs to the member var if it is on a line by itself.
5556
$prevContent = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($prev - 1), null, true);
@@ -74,37 +75,27 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
7475
$phpcsFile->fixer->endChangeset();
7576
}
7677
}//end if
77-
}//end if
7878

79-
$start = $prev;
80-
} else {
81-
$start = $stackPtr;
79+
$start = $prev;
80+
}//end if
8281
}//end if
8382

8483
// There needs to be 1 blank line before the var, not counting comments.
85-
$prevLineToken = null;
86-
for ($i = ($start - 1); $i > 0; $i--) {
87-
if (isset(PHP_CodeSniffer_Tokens::$commentTokens[$tokens[$i]['code']]) === true) {
88-
// Skip comments.
89-
continue;
90-
} else if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) === false) {
91-
// Not the end of the line.
92-
continue;
93-
} else {
94-
$prevLineToken = $i;
95-
break;
84+
if ($start === $stackPtr) {
85+
// No comment found.
86+
$first = $phpcsFile->findFirstOnLine(PHP_CodeSniffer_Tokens::$emptyTokens, $start, true);
87+
if ($first === false) {
88+
$first = $start;
9689
}
97-
}
98-
99-
if (is_null($prevLineToken) === true) {
100-
// Never found the previous line, which means
101-
// there are 0 blank lines before the member var.
102-
$foundLines = 0;
90+
} else if ($tokens[$start]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
91+
$first = $tokens[$start]['comment_opener'];
10392
} else {
104-
$prevContent = $phpcsFile->findPrevious(T_WHITESPACE, $prevLineToken, null, true);
105-
$foundLines = ($tokens[$prevLineToken]['line'] - $tokens[$prevContent]['line']);
106-
}//end if
93+
$first = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($start - 1), null, true);
94+
$first = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$commentTokens, ($first + 1));
95+
}
10796

97+
$prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($first - 1), null, true);
98+
$foundLines = ($tokens[$first]['line'] - $tokens[$prev]['line'] - 1);
10899
if ($foundLines === 1) {
109100
return;
110101
}
@@ -113,17 +104,21 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
113104
$data = array($foundLines);
114105
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);
115106
if ($fix === true) {
116-
if ($foundLines === 0) {
117-
$phpcsFile->fixer->addNewline($prevLineToken);
118-
} else {
119-
$phpcsFile->fixer->beginChangeset();
120-
for ($i = ($prevContent + 1); $i <= $prevLineToken; $i++) {
121-
$phpcsFile->fixer->replaceToken($i, '');
107+
$phpcsFile->fixer->beginChangeset();
108+
for ($i = ($prev + 1); $i < $first; $i++) {
109+
if ($tokens[$i]['line'] === $tokens[$prev]['line']) {
110+
continue;
122111
}
123112

124-
$phpcsFile->fixer->addNewline($prevLineToken);
125-
$phpcsFile->fixer->endChangeset();
113+
if ($tokens[$i]['line'] === $tokens[$first]['line']) {
114+
$phpcsFile->fixer->addNewline(($i - 1));
115+
break;
116+
}
117+
118+
$phpcsFile->fixer->replaceToken($i, '');
126119
}
120+
121+
$phpcsFile->fixer->endChangeset();
127122
}//end if
128123

129124
}//end processMemberVar()

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,16 @@ class Foo
135135
*/
136136
private $bar;
137137

138-
}
138+
}
139+
140+
class Foo
141+
{
142+
143+
/**
144+
* @var integer
145+
*/
146+
private $foo; // comment
147+
148+
private $bar;
149+
150+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
class MyClass
3+
{
4+
5+
public $var1 = 'value';
6+
7+
public $var2 = 'value';
8+
9+
public $var3 = 'value';
10+
11+
}//end class
12+
13+
14+
interface MyInterface
15+
{
16+
17+
public $var1 = 'value';
18+
19+
public $var2 = 'value';
20+
21+
protected $var3 = 'value';
22+
}//end interface
23+
24+
25+
class MyClass
26+
{
27+
28+
public $var1 = 'value';
29+
30+
private $var2 = 'value';
31+
32+
protected $var3 = 'value';
33+
34+
35+
}//end class
36+
37+
38+
39+
class MyClass
40+
{
41+
42+
public $var1 = 'value';
43+
}//end class
44+
45+
46+
interface MyInterface
47+
{
48+
49+
public $var1 = 'value';
50+
function myFunction();
51+
}//end interface
52+
53+
54+
class MyClass
55+
{
56+
57+
/**
58+
* The actions that this wizard step requires.
59+
*
60+
* @var array
61+
* @since 4.0.0
62+
*/
63+
protected $actions = array();
64+
65+
/**
66+
* TRUE if this step should be performed after the asset is created.
67+
*
68+
* @var boolean
69+
* @since 4.0.0
70+
*/
71+
protected $postStep = FALSE;
72+
73+
74+
}//end class
75+
76+
class MyClass
77+
{
78+
79+
/**
80+
* The actions that this wizard step requires.
81+
*
82+
* @var array
83+
* @since 4.0.0
84+
*/
85+
protected $actions = array();
86+
87+
}//end class
88+
89+
class MyClass
90+
{
91+
92+
/**
93+
* The actions that this wizard step requires.
94+
*
95+
* @var array
96+
* @since 4.0.0
97+
*/
98+
var $actions = array();
99+
100+
/**
101+
* The actions that this wizard step requires.
102+
*
103+
* @var array
104+
* @since 4.0.0
105+
*/
106+
protected $actions = array();
107+
108+
/**
109+
* The actions that this wizard step requires.
110+
*
111+
* @var array
112+
* @since 4.0.0
113+
*/
114+
protected $actions = array();
115+
116+
}//end class
117+
118+
class Foo
119+
{
120+
121+
private $foo; // comment
122+
123+
private $bar;
124+
125+
}
126+
127+
class Foo
128+
{
129+
130+
private $foo; // comment
131+
132+
/**
133+
* @var type
134+
*/
135+
private $bar;
136+
137+
}
138+
139+
class Foo
140+
{
141+
142+
/**
143+
* @var integer
144+
*/
145+
private $foo; // comment
146+
147+
private $bar;
148+
149+
}

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,6 +2138,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
21382138
<tasks:replace from="@package_version@" to="version" type="package-info" />
21392139
</file>
21402140
<file baseinstalldir="PHP" name="MemberVarSpacingUnitTest.inc" role="test" />
2141+
<file baseinstalldir="PHP" name="MemberVarSpacingUnitTest.inc.fixed" role="test" />
21412142
<file baseinstalldir="PHP" name="MemberVarSpacingUnitTest.php" role="test">
21422143
<tasks:replace from="@package_version@" to="version" type="package-info" />
21432144
</file>

0 commit comments

Comments
 (0)