Skip to content

Commit 4847e28

Browse files
committed
Fixed bug #782 : Conditional function declarations cause fixing conflicts in Squiz standard
1 parent 5bc2148 commit 4847e28

10 files changed

+100
-14
lines changed

CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,12 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
172172
break;
173173
}//end for
174174

175-
$found = ($tokens[$next]['line'] - $tokens[$opener]['line']);
176-
if ($found !== 1) {
177-
$error = 'Expected 1 newline after opening brace; %s found';
178-
$data = array($found);
179-
$fix = $phpcsFile->addFixableError($error, $opener, 'NewlineAfterOpenBrace', $data);
175+
if ($tokens[$next]['line'] === $tokens[$opener]['line']) {
176+
$error = 'Newline required after opening brace';
177+
$fix = $phpcsFile->addFixableError($error, $opener, 'NewlineAfterOpenBrace');
180178
if ($fix === true) {
181179
$phpcsFile->fixer->beginChangeset();
182180
for ($i = ($opener + 1); $i < $next; $i++) {
183-
if ($found > 0 && $tokens[$i]['line'] === $tokens[$next]['line']) {
184-
break;
185-
}
186-
187181
if (trim($tokens[$i]['content']) !== '') {
188182
break;
189183
}

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,18 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
134134
}
135135
}
136136

137-
if ($tokens[$firstContent]['line'] >= ($tokens[$scopeOpener]['line'] + 2)) {
137+
// We ignore spacing for some structures that tend to have their own rules.
138+
$ignore = array(
139+
T_FUNCTION => true,
140+
T_CLASS => true,
141+
T_INTERFACE => true,
142+
T_TRAIT => true,
143+
T_DOC_COMMENT_OPEN_TAG => true,
144+
);
145+
146+
if (isset($ignore[$tokens[$firstContent]['code']]) === false
147+
&& $tokens[$firstContent]['line'] >= ($tokens[$scopeOpener]['line'] + 2)
148+
) {
138149
$error = 'Blank line found at start of control structure';
139150
$fix = $phpcsFile->addFixableError($error, $scopeOpener, 'SpacingAfterOpen');
140151

@@ -159,7 +170,21 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
159170
true
160171
);
161172

162-
if ($tokens[$lastContent]['line'] <= ($tokens[$scopeCloser]['line'] - 2)) {
173+
$lastNonEmptyContent = $phpcsFile->findPrevious(
174+
PHP_CodeSniffer_Tokens::$emptyTokens,
175+
($scopeCloser - 1),
176+
null,
177+
true
178+
);
179+
180+
$checkToken = $lastContent;
181+
if (isset($tokens[$lastNonEmptyContent]['scope_condition']) === true) {
182+
$checkToken = $tokens[$lastNonEmptyContent]['scope_condition'];
183+
}
184+
185+
if (isset($ignore[$tokens[$checkToken]['code']]) === false
186+
&& $tokens[$lastContent]['line'] <= ($tokens[$scopeCloser]['line'] - 2)
187+
) {
163188
$errorToken = $scopeCloser;
164189
for ($i = ($scopeCloser - 1); $i > $lastContent; $i--) {
165190
if ($tokens[$i]['line'] < $tokens[$scopeCloser]['line']) {

CodeSniffer/Standards/Squiz/Tests/ControlStructures/ControlSignatureUnitTest.inc.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,12 @@ echo $i;
120120
} while ($i > 0);
121121

122122
if ($i === 0) {
123+
123124
$i = 1
124125
}
125126

126127
if ($a) {
128+
127129
} elseif ($b) {
128130
}
129131

@@ -157,6 +159,7 @@ try {
157159
}
158160

159161
switch ($foo) {
162+
160163
case 'bar':
161164
break;
162165

CodeSniffer/Standards/Squiz/Tests/ControlStructures/ControlSignatureUnitTest.js.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ i = 0;
120120
} while (i > 0);
121121

122122
if (i === 0) {
123+
123124
i = 1
124125
}
125126

CodeSniffer/Standards/Squiz/Tests/ControlStructures/ControlSignatureUnitTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,15 @@ public function getErrorList($testFile='ControlSignatureUnitTest.inc')
6565
99 => 1,
6666
108 => 1,
6767
112 => 1,
68-
115 => 1,
6968
);
7069

7170
if ($testFile === 'ControlSignatureUnitTest.inc') {
72-
$errors[120] = 1;
7371
$errors[122] = 1;
7472
$errors[130] = 2;
7573
$errors[134] = 1;
7674
$errors[150] = 1;
7775
$errors[153] = 1;
78-
$errors[158] = 2;
76+
$errors[158] = 1;
7977
$errors[165] = 1;
8078
$errors[170] = 2;
8179
$errors[186] = 2;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,24 @@ try {
197197
catch (Exception $e) {
198198
// Something
199199
}
200+
201+
if ($foo) {
202+
203+
204+
/**
205+
* Comment
206+
*/
207+
function foo() {
208+
// Code here
209+
}
210+
211+
212+
/**
213+
* Comment
214+
*/
215+
class bar() {
216+
217+
}//end class
218+
219+
220+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,19 @@ class MyClass
280280
function test() {
281281

282282
}//end test()
283+
284+
285+
if ($foo) {
286+
/**
287+
* Comment
288+
*/
289+
function foo() {
290+
// Code here
291+
}
292+
/**
293+
* Comment
294+
*/
295+
function bar() {
296+
// Code here
297+
}
298+
}

CodeSniffer/Standards/Squiz/Tests/WhiteSpace/FunctionSpacingUnitTest.inc.fixed

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,25 @@ class MyClass
302302
function test() {
303303

304304
}//end test()
305+
306+
307+
if ($foo) {
308+
309+
310+
/**
311+
* Comment
312+
*/
313+
function foo() {
314+
// Code here
315+
}
316+
317+
318+
/**
319+
* Comment
320+
*/
321+
function bar() {
322+
// Code here
323+
}
324+
325+
326+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public function getErrorList()
6565
252 => 1,
6666
275 => 1,
6767
276 => 1,
68+
289 => 1,
69+
291 => 1,
70+
297 => 1,
6871
);
6972

7073
}//end getErrorList()

package.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
7373
- Fixed bug #769 : Incorrect detection of variable reference operator when used with short array syntax
7474
-- Thanks to Klaus Purer for the patch
7575
- Fixed bug #772 : Syntax error when using PHPCBF on alternative style foreach loops
76+
- Fixed bug #782 : Conditional function declarations cause fixing conflicts in Squiz standard
77+
-- Squiz.ControlStructures.ControlSignature no longer enforces a single newline after open brace
78+
-- Squiz.WhiteSpace.ControlStructureSpacing can be used to checl spacing at the start/end of control structures
7679
</notes>
7780
<contents>
7881
<dir name="/">

0 commit comments

Comments
 (0)