Skip to content

Commit 2f44e16

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). a495c2cc expanded the same approach for else/elseif/if/foreach control structures. After the removal of the `$next !== $end`, the `$next` variable became used allowing to further simplify 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 e9629d8 commit 2f44e16

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
@@ -264,102 +264,69 @@ public function process(File $phpcsFile, $stackPtr)
264264
}
265265

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

301-
if ($tokens[$end]['code'] !== T_SEMICOLON
302-
&& $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET
268+
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
269+
// Account for a comment on the end of the line.
270+
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
271+
if (isset($tokens[($endLine + 1)]) === false
272+
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
303273
) {
304-
$phpcsFile->fixer->addContent($end, '; ');
274+
break;
305275
}
306276
}
307277

308-
$next = $phpcsFile->findNext(T_WHITESPACE, ($endToken + 1), null, true);
309-
if ($next !== false
310-
&& ($tokens[$next]['code'] === T_ELSE
311-
|| $tokens[$next]['code'] === T_ELSEIF)
278+
if (isset(Tokens::$commentTokens[$tokens[$endLine]['code']]) === false
279+
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
280+
|| isset(Tokens::$commentTokens[$tokens[($endLine - 1)]['code']]) === false)
312281
) {
313-
$phpcsFile->fixer->addContentBefore($next, '} ');
314-
} else {
315-
$indent = '';
316-
for ($first = $stackPtr; $first > 0; $first--) {
317-
if ($tokens[$first]['column'] === 1) {
318-
break;
319-
}
320-
}
282+
$endLine = $end;
283+
}
284+
} else {
285+
$endLine = $end;
286+
}
321287

322-
if ($tokens[$first]['code'] === T_WHITESPACE) {
323-
$indent = $tokens[$first]['content'];
324-
} else if ($tokens[$first]['code'] === T_INLINE_HTML
325-
|| $tokens[$first]['code'] === T_OPEN_TAG
326-
) {
327-
$addedContent = '';
328-
}
288+
if ($endLine !== $end) {
289+
$endToken = $endLine;
290+
$addedContent = '';
291+
} else {
292+
$endToken = $end;
293+
$addedContent = $phpcsFile->eolChar;
329294

330-
$addedContent .= $indent.'}';
331-
if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) {
332-
$addedContent .= $phpcsFile->eolChar;
333-
}
295+
if ($tokens[$end]['code'] !== T_SEMICOLON
296+
&& $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET
297+
) {
298+
$phpcsFile->fixer->addContent($end, '; ');
299+
}
300+
}
334301

335-
$phpcsFile->fixer->addContent($endToken, $addedContent);
336-
}//end if
302+
$next = $phpcsFile->findNext(T_WHITESPACE, ($endToken + 1), null, true);
303+
if ($next !== false
304+
&& ($tokens[$next]['code'] === T_ELSE
305+
|| $tokens[$next]['code'] === T_ELSEIF)
306+
) {
307+
$phpcsFile->fixer->addContentBefore($next, '} ');
337308
} else {
338-
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
339-
// Account for a comment on the end of the line.
340-
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
341-
if (isset($tokens[($endLine + 1)]) === false
342-
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
343-
) {
344-
break;
345-
}
309+
$indent = '';
310+
for ($first = $stackPtr; $first > 0; $first--) {
311+
if ($tokens[$first]['column'] === 1) {
312+
break;
346313
}
314+
}
347315

348-
if ($tokens[$endLine]['code'] !== T_COMMENT
349-
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
350-
|| $tokens[($endLine - 1)]['code'] !== T_COMMENT)
351-
) {
352-
$endLine = $end;
353-
}
316+
if ($tokens[$first]['code'] === T_WHITESPACE) {
317+
$indent = $tokens[$first]['content'];
318+
} else if ($tokens[$first]['code'] === T_INLINE_HTML
319+
|| $tokens[$first]['code'] === T_OPEN_TAG
320+
) {
321+
$addedContent = '';
354322
}
355323

356-
if ($endLine !== $end) {
357-
$phpcsFile->fixer->replaceToken($end, '');
358-
$phpcsFile->fixer->addNewlineBefore($endLine);
359-
$phpcsFile->fixer->addContent($endLine, '}');
360-
} else {
361-
$phpcsFile->fixer->replaceToken($end, '}');
324+
$addedContent .= $indent.'}';
325+
if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) {
326+
$addedContent .= $phpcsFile->eolChar;
362327
}
328+
329+
$phpcsFile->fixer->addContent($endToken, $addedContent);
363330
}//end if
364331

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

0 commit comments

Comments
 (0)