Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,25 @@ public function process(File $phpcsFile, $stackPtr)
}
}

if ($tokens[$stackPtr]['code'] === T_WHILE || $tokens[$stackPtr]['code'] === T_FOR) {
// This could be from a DO WHILE, which doesn't have an opening brace or a while/for without body.
if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) {
$afterParensCloser = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($tokens[$stackPtr]['parenthesis_closer'] + 1), null, true);
if ($afterParensCloser === false) {
// Live coding.
return;
}
if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) {
$nextTokenIndex = ($tokens[$stackPtr]['parenthesis_closer'] + 1);
} else if (in_array($tokens[$stackPtr]['code'], [T_ELSE, T_DO], true) === true) {
$nextTokenIndex = ($stackPtr + 1);
}

if ($tokens[$afterParensCloser]['code'] === T_SEMICOLON) {
$phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'no');
return;
}
if (isset($nextTokenIndex) === true) {
$firstNonEmptyToken = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, $nextTokenIndex, null, true);
if ($firstNonEmptyToken === false) {
// Live coding.
return;
}
}//end if

if ($tokens[$firstNonEmptyToken]['code'] === T_SEMICOLON) {
// This is a control structure without a body. Bow out.
$phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'no');
return;
}
}

if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false
&& $tokens[$stackPtr]['code'] !== T_ELSE
Expand Down Expand Up @@ -151,9 +155,7 @@ public function process(File $phpcsFile, $stackPtr)
$closer = $stackPtr;
}

if ($tokens[($closer + 1)]['code'] === T_WHITESPACE
|| $tokens[($closer + 1)]['code'] === T_SEMICOLON
) {
if ($tokens[($closer + 1)]['code'] === T_WHITESPACE) {
$phpcsFile->fixer->addContent($closer, ' {');
} else {
$phpcsFile->fixer->addContent($closer, ' { ');
Expand Down Expand Up @@ -249,102 +251,69 @@ public function process(File $phpcsFile, $stackPtr)
}

$nextContent = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($end + 1), null, true);
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
// Looks for completely empty statements.
$next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), ($end + 1), true);
} else {
$next = ($end + 1);
$endLine = $end;
}

if ($next !== $end) {
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
// Account for a comment on the end of the line.
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
if (isset($tokens[($endLine + 1)]) === false
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
) {
break;
}
}

if (isset(Tokens::COMMENT_TOKENS[$tokens[$endLine]['code']]) === false
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
|| isset(Tokens::COMMENT_TOKENS[$tokens[($endLine - 1)]['code']]) === false)
) {
$endLine = $end;
}
}

if ($endLine !== $end) {
$endToken = $endLine;
$addedContent = '';
} else {
$endToken = $end;
$addedContent = $phpcsFile->eolChar;

if ($tokens[$end]['code'] !== T_SEMICOLON
&& $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
// Account for a comment on the end of the line.
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
if (isset($tokens[($endLine + 1)]) === false
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
) {
$phpcsFile->fixer->addContent($end, '; ');
break;
}
}

$next = $phpcsFile->findNext(T_WHITESPACE, ($endToken + 1), null, true);
if ($next !== false
&& ($tokens[$next]['code'] === T_ELSE
|| $tokens[$next]['code'] === T_ELSEIF)
if (isset(Tokens::COMMENT_TOKENS[$tokens[$endLine]['code']]) === false
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
|| isset(Tokens::COMMENT_TOKENS[$tokens[($endLine - 1)]['code']]) === false)
) {
$phpcsFile->fixer->addContentBefore($next, '} ');
} else {
$indent = '';
for ($first = $stackPtr; $first > 0; $first--) {
if ($tokens[$first]['column'] === 1) {
break;
}
}
$endLine = $end;
}
} else {
$endLine = $end;
}

if ($tokens[$first]['code'] === T_WHITESPACE) {
$indent = $tokens[$first]['content'];
} else if ($tokens[$first]['code'] === T_INLINE_HTML
|| $tokens[$first]['code'] === T_OPEN_TAG
) {
$addedContent = '';
}
if ($endLine !== $end) {
$endToken = $endLine;
$addedContent = '';
} else {
$endToken = $end;
$addedContent = $phpcsFile->eolChar;

$addedContent .= $indent.'}';
if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) {
$addedContent .= $phpcsFile->eolChar;
}
if ($tokens[$end]['code'] !== T_SEMICOLON
&& $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET
) {
$phpcsFile->fixer->addContent($end, '; ');
}
}

$phpcsFile->fixer->addContent($endToken, $addedContent);
}//end if
$next = $phpcsFile->findNext(T_WHITESPACE, ($endToken + 1), null, true);
if ($next !== false
&& ($tokens[$next]['code'] === T_ELSE
|| $tokens[$next]['code'] === T_ELSEIF)
) {
$phpcsFile->fixer->addContentBefore($next, '} ');
} else {
if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
// Account for a comment on the end of the line.
for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
if (isset($tokens[($endLine + 1)]) === false
|| $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
) {
break;
}
$indent = '';
for ($first = $stackPtr; $first > 0; $first--) {
if ($tokens[$first]['column'] === 1) {
break;
}
}

if ($tokens[$endLine]['code'] !== T_COMMENT
&& ($tokens[$endLine]['code'] !== T_WHITESPACE
|| $tokens[($endLine - 1)]['code'] !== T_COMMENT)
) {
$endLine = $end;
}
if ($tokens[$first]['code'] === T_WHITESPACE) {
$indent = $tokens[$first]['content'];
} else if ($tokens[$first]['code'] === T_INLINE_HTML
|| $tokens[$first]['code'] === T_OPEN_TAG
) {
$addedContent = '';
}

if ($endLine !== $end) {
$phpcsFile->fixer->replaceToken($end, '');
$phpcsFile->fixer->addNewlineBefore($endLine);
$phpcsFile->fixer->addContent($endLine, '}');
} else {
$phpcsFile->fixer->replaceToken($end, '}');
$addedContent .= $indent.'}';
if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) {
$addedContent .= $phpcsFile->eolChar;
}

$phpcsFile->fixer->addContent($endToken, $addedContent);
}//end if

$phpcsFile->fixer->endChangeset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,24 @@ function testFinally()
if ($something) {
echo 'hello';
} else /* comment */ if ($somethingElse) echo 'hi';

if ($sniffShouldBailEarly);

if (false) {
} elseif ($sniffShouldBailEarly);

if (false) {
} else if ($sniffShouldBailEarly);

if (false) {
} else ($sniffShouldGenerateError);

if (false) {
} else; // Sniff should bail early.

foreach ($array as $sniffShouldBailEarly);

foreach ($array as $sniffShouldBailEarly)
/* some comment */;

do ; while ($sniffShouldBailEarly > 5);
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,25 @@ if ($something) {
echo 'hello';
} else /* comment */ if ($somethingElse) { echo 'hi';
}

if ($sniffShouldBailEarly);

if (false) {
} elseif ($sniffShouldBailEarly);

if (false) {
} else if ($sniffShouldBailEarly);

if (false) {
} else { ($sniffShouldGenerateError);
}

if (false) {
} else; // Sniff should bail early.

foreach ($array as $sniffShouldBailEarly);

foreach ($array as $sniffShouldBailEarly)
/* some comment */;

do ; while ($sniffShouldBailEarly > 5);
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function getErrorList($testFile='')
260 => 1,
269 => 1,
278 => 1,
289 => 1,
];

default:
Expand Down