Skip to content

Commit d8bc30b

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). d4e5d28 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 0bc774e commit d8bc30b

File tree

1 file changed

+48
-81
lines changed

1 file changed

+48
-81
lines changed

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

+48-81
Original file line numberDiff line numberDiff line change
@@ -261,102 +261,69 @@ public function process(File $phpcsFile, $stackPtr)
261261
}
262262

263263
$nextContent = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true);
264-
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
265-
// Looks for completely empty statements.
266-
$next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), ($end + 1), true);
267-
} else {
268-
$next = ($end + 1);
269-
$endLine = $end;
270-
}
271-
272-
if ($next !== $end) {
273-
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
274-
// Account for a comment on the end of the line.
275-
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
276-
if (isset($tokens[($endLine + 1)]) === false
277-
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
278-
) {
279-
break;
280-
}
281-
}
282-
283-
if (isset(Tokens::$commentTokens[$tokens[$endLine]['code']]) === false
284-
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
285-
|| isset(Tokens::$commentTokens[$tokens[($endLine - 1)]['code']]) === false)
286-
) {
287-
$endLine = $end;
288-
}
289-
}
290-
291-
if ($endLine !== $end) {
292-
$endToken = $endLine;
293-
$addedContent = '';
294-
} else {
295-
$endToken = $end;
296-
$addedContent = $phpcsFile->eolChar;
297264

298-
if ($tokens[$end]['code'] !== T_SEMICOLON
299-
&& $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET
265+
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
266+
// Account for a comment on the end of the line.
267+
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
268+
if (isset($tokens[($endLine + 1)]) === false
269+
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
300270
) {
301-
$phpcsFile->fixer->addContent($end, '; ');
271+
break;
302272
}
303273
}
304274

305-
$next = $phpcsFile->findNext(T_WHITESPACE, ($endToken + 1), null, true);
306-
if ($next !== false
307-
&& ($tokens[$next]['code'] === T_ELSE
308-
|| $tokens[$next]['code'] === T_ELSEIF)
275+
if (isset(Tokens::$commentTokens[$tokens[$endLine]['code']]) === false
276+
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
277+
|| isset(Tokens::$commentTokens[$tokens[($endLine - 1)]['code']]) === false)
309278
) {
310-
$phpcsFile->fixer->addContentBefore($next, '} ');
311-
} else {
312-
$indent = '';
313-
for ($first = $stackPtr; $first > 0; $first--) {
314-
if ($tokens[$first]['column'] === 1) {
315-
break;
316-
}
317-
}
279+
$endLine = $end;
280+
}
281+
} else {
282+
$endLine = $end;
283+
}
318284

319-
if ($tokens[$first]['code'] === T_WHITESPACE) {
320-
$indent = $tokens[$first]['content'];
321-
} else if ($tokens[$first]['code'] === T_INLINE_HTML
322-
|| $tokens[$first]['code'] === T_OPEN_TAG
323-
) {
324-
$addedContent = '';
325-
}
285+
if ($endLine !== $end) {
286+
$endToken = $endLine;
287+
$addedContent = '';
288+
} else {
289+
$endToken = $end;
290+
$addedContent = $phpcsFile->eolChar;
326291

327-
$addedContent .= $indent.'}';
328-
if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) {
329-
$addedContent .= $phpcsFile->eolChar;
330-
}
292+
if ($tokens[$end]['code'] !== T_SEMICOLON
293+
&& $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET
294+
) {
295+
$phpcsFile->fixer->addContent($end, '; ');
296+
}
297+
}
331298

332-
$phpcsFile->fixer->addContent($endToken, $addedContent);
333-
}//end if
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)
303+
) {
304+
$phpcsFile->fixer->addContentBefore($next, '} ');
334305
} else {
335-
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
336-
// Account for a comment on the end of the line.
337-
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
338-
if (isset($tokens[($endLine + 1)]) === false
339-
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
340-
) {
341-
break;
342-
}
306+
$indent = '';
307+
for ($first = $stackPtr; $first > 0; $first--) {
308+
if ($tokens[$first]['column'] === 1) {
309+
break;
343310
}
311+
}
344312

345-
if ($tokens[$endLine]['code'] !== T_COMMENT
346-
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
347-
|| $tokens[($endLine - 1)]['code'] !== T_COMMENT)
348-
) {
349-
$endLine = $end;
350-
}
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 = '';
351319
}
352320

353-
if ($endLine !== $end) {
354-
$phpcsFile->fixer->replaceToken($end, '');
355-
$phpcsFile->fixer->addNewlineBefore($endLine);
356-
$phpcsFile->fixer->addContent($endLine, '}');
357-
} else {
358-
$phpcsFile->fixer->replaceToken($end, '}');
321+
$addedContent .= $indent.'}';
322+
if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) {
323+
$addedContent .= $phpcsFile->eolChar;
359324
}
325+
326+
$phpcsFile->fixer->addContent($endToken, $addedContent);
360327
}//end if
361328

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

0 commit comments

Comments
 (0)