Skip to content

Commit 5c13c68

Browse files
committed
Generic/InlineControlStructure: removed another unnecessary code block
The original version of this part of the code that is now being removed was added in the early days by the commit that enabled this sniff to fix errors: squizlabs/PHP_CodeSniffer@a54c619#diff-4b3945c2100b0a92a56509de1b797bf58ad804cf36233c95c492479b665655dcR148-R154 The only two tests that were added with the commit mentioned above that trigger the removed condition are tests using `while` loops without body: squizlabs/PHP_CodeSniffer@a54c619#diff-116c49a7b0b31f724fc25409e31ba119d7f023146818bcb63edbe8f4071422e2R42-R43 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 two previous 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-R50). 29d0be3 expanded the same approach for `do while`/`else`/`elseif`/`if`/`foreach` control structures. After the removal of the `$next !== $end` check, the `$next` variable became unused allowing for further simplification of the code by removing the place where it was being defined. Note for reviewers: this commit is easier to evaluate when ignoring whitespaces.
1 parent 49a8309 commit 5c13c68

File tree

1 file changed

+48
-81
lines changed

1 file changed

+48
-81
lines changed

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

Lines changed: 48 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -251,102 +251,69 @@ public function process(File $phpcsFile, $stackPtr)
251251
}
252252

253253
$nextContent = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($end + 1), null, true);
254-
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
255-
// Looks for completely empty statements.
256-
$next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), ($end + 1), true);
257-
} else {
258-
$next = ($end + 1);
259-
$endLine = $end;
260-
}
261-
262-
if ($next !== $end) {
263-
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
264-
// Account for a comment on the end of the line.
265-
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
266-
if (isset($tokens[($endLine + 1)]) === false
267-
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
268-
) {
269-
break;
270-
}
271-
}
272-
273-
if (isset(Tokens::COMMENT_TOKENS[$tokens[$endLine]['code']]) === false
274-
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
275-
|| isset(Tokens::COMMENT_TOKENS[$tokens[($endLine - 1)]['code']]) === false)
276-
) {
277-
$endLine = $end;
278-
}
279-
}
280-
281-
if ($endLine !== $end) {
282-
$endToken = $endLine;
283-
$addedContent = '';
284-
} else {
285-
$endToken = $end;
286-
$addedContent = $phpcsFile->eolChar;
287254

288-
if ($tokens[$end]['code'] !== T_SEMICOLON
289-
&& $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET
255+
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
256+
// Account for a comment on the end of the line.
257+
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
258+
if (isset($tokens[($endLine + 1)]) === false
259+
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
290260
) {
291-
$phpcsFile->fixer->addContent($end, '; ');
261+
break;
292262
}
293263
}
294264

295-
$next = $phpcsFile->findNext(T_WHITESPACE, ($endToken + 1), null, true);
296-
if ($next !== false
297-
&& ($tokens[$next]['code'] === T_ELSE
298-
|| $tokens[$next]['code'] === T_ELSEIF)
265+
if (isset(Tokens::COMMENT_TOKENS[$tokens[$endLine]['code']]) === false
266+
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
267+
|| isset(Tokens::COMMENT_TOKENS[$tokens[($endLine - 1)]['code']]) === false)
299268
) {
300-
$phpcsFile->fixer->addContentBefore($next, '} ');
301-
} else {
302-
$indent = '';
303-
for ($first = $stackPtr; $first > 0; $first--) {
304-
if ($tokens[$first]['column'] === 1) {
305-
break;
306-
}
307-
}
269+
$endLine = $end;
270+
}
271+
} else {
272+
$endLine = $end;
273+
}
308274

309-
if ($tokens[$first]['code'] === T_WHITESPACE) {
310-
$indent = $tokens[$first]['content'];
311-
} else if ($tokens[$first]['code'] === T_INLINE_HTML
312-
|| $tokens[$first]['code'] === T_OPEN_TAG
313-
) {
314-
$addedContent = '';
315-
}
275+
if ($endLine !== $end) {
276+
$endToken = $endLine;
277+
$addedContent = '';
278+
} else {
279+
$endToken = $end;
280+
$addedContent = $phpcsFile->eolChar;
316281

317-
$addedContent .= $indent.'}';
318-
if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) {
319-
$addedContent .= $phpcsFile->eolChar;
320-
}
282+
if ($tokens[$end]['code'] !== T_SEMICOLON
283+
&& $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET
284+
) {
285+
$phpcsFile->fixer->addContent($end, '; ');
286+
}
287+
}
321288

322-
$phpcsFile->fixer->addContent($endToken, $addedContent);
323-
}//end if
289+
$next = $phpcsFile->findNext(T_WHITESPACE, ($endToken + 1), null, true);
290+
if ($next !== false
291+
&& ($tokens[$next]['code'] === T_ELSE
292+
|| $tokens[$next]['code'] === T_ELSEIF)
293+
) {
294+
$phpcsFile->fixer->addContentBefore($next, '} ');
324295
} else {
325-
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
326-
// Account for a comment on the end of the line.
327-
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
328-
if (isset($tokens[($endLine + 1)]) === false
329-
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
330-
) {
331-
break;
332-
}
296+
$indent = '';
297+
for ($first = $stackPtr; $first > 0; $first--) {
298+
if ($tokens[$first]['column'] === 1) {
299+
break;
333300
}
301+
}
334302

335-
if ($tokens[$endLine]['code'] !== T_COMMENT
336-
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
337-
|| $tokens[($endLine - 1)]['code'] !== T_COMMENT)
338-
) {
339-
$endLine = $end;
340-
}
303+
if ($tokens[$first]['code'] === T_WHITESPACE) {
304+
$indent = $tokens[$first]['content'];
305+
} else if ($tokens[$first]['code'] === T_INLINE_HTML
306+
|| $tokens[$first]['code'] === T_OPEN_TAG
307+
) {
308+
$addedContent = '';
341309
}
342310

343-
if ($endLine !== $end) {
344-
$phpcsFile->fixer->replaceToken($end, '');
345-
$phpcsFile->fixer->addNewlineBefore($endLine);
346-
$phpcsFile->fixer->addContent($endLine, '}');
347-
} else {
348-
$phpcsFile->fixer->replaceToken($end, '}');
311+
$addedContent .= $indent.'}';
312+
if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) {
313+
$addedContent .= $phpcsFile->eolChar;
349314
}
315+
316+
$phpcsFile->fixer->addContent($endToken, $addedContent);
350317
}//end if
351318

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

0 commit comments

Comments
 (0)