Skip to content

Commit 897cfa8

Browse files
committed
Fixed bug squizlabs#1932 : Generic.ControlStructures.InlineControlStructure fixer moves new PHPCS annotations
1 parent 210ffc6 commit 897cfa8

File tree

5 files changed

+75
-19
lines changed

5 files changed

+75
-19
lines changed

package.xml

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
129129
- Fixed bug #1922 : Equal sign alignment check broken when list syntax used before assignment operator
130130
- Fixed bug #1925 : Generic.Formatting.MultipleStatementAlignment skipping assignments within closures
131131
- Fixed bug #1931 : Generic opening brace placement sniffs do not correctly support function return types
132+
- Fixed bug #1932 : Generic.ControlStructures.InlineControlStructure fixer moves new PHPCS annotations
132133
- Fixed bug #1938 : Generic opening brace placement sniffs incorrectly move PHPCS annotations
133134
-- Thanks to Juliette Reinders Folmer for the patch
134135
- Fixed bug #1939 : phpcs:set annotations do not cause the line they are on to be ignored

src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php

+39-19
Original file line numberDiff line numberDiff line change
@@ -215,30 +215,34 @@ public function process(File $phpcsFile, $stackPtr)
215215
$end = $lastNonEmpty;
216216
}
217217

218-
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true);
219-
220-
if ($next === false || $tokens[$next]['line'] !== $tokens[$end]['line']) {
218+
$nextContent = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true);
219+
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
221220
// Looks for completely empty statements.
222221
$next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), ($end + 1), true);
223-
224-
// Account for a comment on the end of the line.
225-
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
226-
if (isset($tokens[($endLine + 1)]) === false
227-
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
228-
) {
229-
break;
230-
}
231-
}
232-
233-
if ($tokens[$endLine]['code'] !== T_COMMENT) {
234-
$endLine = $end;
235-
}
236222
} else {
237223
$next = ($end + 1);
238224
$endLine = $end;
239225
}
240226

241227
if ($next !== $end) {
228+
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
229+
// Account for a comment on the end of the line.
230+
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
231+
if (isset($tokens[($endLine + 1)]) === false
232+
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
233+
) {
234+
break;
235+
}
236+
}
237+
238+
if (isset(Tokens::$commentTokens[$tokens[$endLine]['code']]) === false
239+
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
240+
|| isset(Tokens::$commentTokens[$tokens[($endLine - 1)]['code']]) === false)
241+
) {
242+
$endLine = $end;
243+
}
244+
}
245+
242246
if ($endLine !== $end) {
243247
$endToken = $endLine;
244248
$addedContent = '';
@@ -262,9 +266,7 @@ public function process(File $phpcsFile, $stackPtr)
262266
} else {
263267
$indent = '';
264268
for ($first = $stackPtr; $first > 0; $first--) {
265-
if ($first === 1
266-
|| $tokens[($first - 1)]['line'] !== $tokens[$first]['line']
267-
) {
269+
if ($tokens[$first]['column'] === 1) {
268270
break;
269271
}
270272
}
@@ -285,6 +287,24 @@ public function process(File $phpcsFile, $stackPtr)
285287
$phpcsFile->fixer->addContent($endToken, $addedContent);
286288
}//end if
287289
} else {
290+
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
291+
// Account for a comment on the end of the line.
292+
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
293+
if (isset($tokens[($endLine + 1)]) === false
294+
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
295+
) {
296+
break;
297+
}
298+
}
299+
300+
if ($tokens[$endLine]['code'] !== T_COMMENT
301+
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
302+
|| $tokens[($endLine - 1)]['code'] !== T_COMMENT)
303+
) {
304+
$endLine = $end;
305+
}
306+
}
307+
288308
if ($endLine !== $end) {
289309
$phpcsFile->fixer->replaceToken($end, '');
290310
$phpcsFile->fixer->addNewlineBefore($endLine);

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.inc

+13
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,16 @@ if ($this) {
222222
if ($that)
223223
foo(${$a[$b]});
224224
}
225+
226+
while (!$this->readLine($tokens, $tag)); //phpcs:ignore Standard.Category.Sniff
227+
228+
while (!$this->readLine($tokens, $tag)); // comment
229+
230+
while (!$this->readLine($tokens, $tag)); /* comment */
231+
232+
foreach ($stringParade as $hit)
233+
$hitParade[] = $hit + 0; // phpcs:ignore Standard.Category.Sniff
234+
235+
if ($bar)
236+
if ($foo) echo 'hi'; /* @phpcs:ignore Standard.Category.Sniff */
237+

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.inc.fixed

+16
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,19 @@ if ($this) {
253253
foo(${$a[$b]});
254254
}
255255
}
256+
257+
while (!$this->readLine($tokens, $tag)) {} //phpcs:ignore Standard.Category.Sniff
258+
259+
while (!$this->readLine($tokens, $tag)) {
260+
// comment
261+
}
262+
while (!$this->readLine($tokens, $tag)) { /* comment */
263+
264+
}
265+
foreach ($stringParade as $hit) {
266+
$hitParade[] = $hit + 0; // phpcs:ignore Standard.Category.Sniff
267+
}
268+
if ($bar) {
269+
if ($foo) { echo 'hi'; /* @phpcs:ignore Standard.Category.Sniff */
270+
}
271+
}

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public function getErrorList($testFile='InlineControlStructureUnitTest.inc')
6868
198 => 1,
6969
206 => 1,
7070
222 => 1,
71+
226 => 1,
72+
228 => 1,
73+
230 => 1,
74+
232 => 1,
75+
235 => 1,
76+
236 => 1,
7177
];
7278
break;
7379
case 'InlineControlStructureUnitTest.js':

0 commit comments

Comments
 (0)