Skip to content

Commit 1c3e52f

Browse files
committed
Generic/InlineControlStructure: remove unreachable condition
This commit removes an unreachable condition in the fixer part of the sniff. The original version of this condition was added in the early days by the commit that enabled this sniff to fix errors: squizlabs/PHP_CodeSniffer@a54c619#diff-4b3945c2100b0a92a56509de1b797bf58ad804cf36233c95c492479b665655dcL108-L110 The only two tests that were added with the commit mentioned above that trigger the condition that is removed by this commit are tests using `while` loops without body: squizlabs/PHP_CodeSniffer@a54c619#diff-4b3945c2100b0a92a56509de1b797bf58ad804cf36233c95c492479b665655dcL108-L110 I believe control structures without a body are the only cases where `$next` would be equal to `$end`. Thus, these are the only cases where the removed condition would be executed. But subsequent commits changed the sniff to bail early and not get to the fixer part when handling control structures without a body: - 13c803b changed the sniff to ignore while/for without a body and updated the existing tests: squizlabs/PHP_CodeSniffer@13c803b#diff-2f069f3fe33bacdfc80485b97303aec66c98c451d07e6d86e41982b81ab1a294L49-R51 - f4afa10 expanded the same approach to also include elseif/if/foreach control structures Those changes rendered the condition that is removed by this commit unreachable.
1 parent 716cbf6 commit 1c3e52f

File tree

1 file changed

+46
-74
lines changed

1 file changed

+46
-74
lines changed

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

Lines changed: 46 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -263,94 +263,66 @@ public function process(File $phpcsFile, $stackPtr)
263263
$endLine = $end;
264264
}
265265

266-
if ($next !== $end) {
267-
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
268-
// Account for a comment on the end of the line.
269-
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
270-
if (isset($tokens[($endLine + 1)]) === false
271-
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
272-
) {
273-
break;
274-
}
275-
}
276-
277-
if (isset(Tokens::$commentTokens[$tokens[$endLine]['code']]) === false
278-
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
279-
|| isset(Tokens::$commentTokens[$tokens[($endLine - 1)]['code']]) === false)
280-
) {
281-
$endLine = $end;
282-
}
283-
}
284-
285-
if ($endLine !== $end) {
286-
$endToken = $endLine;
287-
$addedContent = '';
288-
} else {
289-
$endToken = $end;
290-
$addedContent = $phpcsFile->eolChar;
291-
292-
if ($tokens[$end]['code'] !== T_SEMICOLON
293-
&& $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET
266+
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
267+
// Account for a comment on the end of the line.
268+
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
269+
if (isset($tokens[($endLine + 1)]) === false
270+
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
294271
) {
295-
$phpcsFile->fixer->addContent($end, '; ');
272+
break;
296273
}
297274
}
298275

299-
$next = $phpcsFile->findNext(T_WHITESPACE, ($endToken + 1), null, true);
300-
if ($next !== false
301-
&& ($tokens[$next]['code'] === T_ELSE
302-
|| $tokens[$next]['code'] === T_ELSEIF)
276+
if (isset(Tokens::$commentTokens[$tokens[$endLine]['code']]) === false
277+
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
278+
|| isset(Tokens::$commentTokens[$tokens[($endLine - 1)]['code']]) === false)
303279
) {
304-
$phpcsFile->fixer->addContentBefore($next, '} ');
305-
} else {
306-
$indent = '';
307-
for ($first = $stackPtr; $first > 0; $first--) {
308-
if ($tokens[$first]['column'] === 1) {
309-
break;
310-
}
311-
}
280+
$endLine = $end;
281+
}
282+
}
312283

313-
if ($tokens[$first]['code'] === T_WHITESPACE) {
314-
$indent = $tokens[$first]['content'];
315-
} else if ($tokens[$first]['code'] === T_INLINE_HTML
316-
|| $tokens[$first]['code'] === T_OPEN_TAG
317-
) {
318-
$addedContent = '';
319-
}
284+
if ($endLine !== $end) {
285+
$endToken = $endLine;
286+
$addedContent = '';
287+
} else {
288+
$endToken = $end;
289+
$addedContent = $phpcsFile->eolChar;
320290

321-
$addedContent .= $indent.'}';
322-
if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) {
323-
$addedContent .= $phpcsFile->eolChar;
324-
}
291+
if ($tokens[$end]['code'] !== T_SEMICOLON
292+
&& $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET
293+
) {
294+
$phpcsFile->fixer->addContent($end, '; ');
295+
}
296+
}
325297

326-
$phpcsFile->fixer->addContent($endToken, $addedContent);
327-
}//end if
298+
$next = $phpcsFile->findNext(T_WHITESPACE, ($endToken + 1), null, true);
299+
if ($next !== false
300+
&& ($tokens[$next]['code'] === T_ELSE
301+
|| $tokens[$next]['code'] === T_ELSEIF)
302+
) {
303+
$phpcsFile->fixer->addContentBefore($next, '} ');
328304
} else {
329-
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
330-
// Account for a comment on the end of the line.
331-
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
332-
if (isset($tokens[($endLine + 1)]) === false
333-
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
334-
) {
335-
break;
336-
}
305+
$indent = '';
306+
for ($first = $stackPtr; $first > 0; $first--) {
307+
if ($tokens[$first]['column'] === 1) {
308+
break;
337309
}
310+
}
338311

339-
if ($tokens[$endLine]['code'] !== T_COMMENT
340-
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
341-
|| $tokens[($endLine - 1)]['code'] !== T_COMMENT)
342-
) {
343-
$endLine = $end;
344-
}
312+
if ($tokens[$first]['code'] === T_WHITESPACE) {
313+
$indent = $tokens[$first]['content'];
314+
} else if ($tokens[$first]['code'] === T_INLINE_HTML
315+
|| $tokens[$first]['code'] === T_OPEN_TAG
316+
) {
317+
$addedContent = '';
345318
}
346319

347-
if ($endLine !== $end) {
348-
$phpcsFile->fixer->replaceToken($end, '');
349-
$phpcsFile->fixer->addNewlineBefore($endLine);
350-
$phpcsFile->fixer->addContent($endLine, '}');
351-
} else {
352-
$phpcsFile->fixer->replaceToken($end, '}');
320+
$addedContent .= $indent.'}';
321+
if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) {
322+
$addedContent .= $phpcsFile->eolChar;
353323
}
324+
325+
$phpcsFile->fixer->addContent($endToken, $addedContent);
354326
}//end if
355327

356328
$phpcsFile->fixer->endChangeset();

0 commit comments

Comments
 (0)