Skip to content

Generic/DisallowShortOpenTag: prevent fixer conflict #835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
26 changes: 14 additions & 12 deletions src/Standards/Generic/Sniffs/PHP/DisallowShortOpenTagSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,20 @@ public function process(File $phpcsFile, $stackPtr)

if ($token['code'] === T_OPEN_TAG_WITH_ECHO) {
$nextVar = $tokens[$phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true)];
$error = 'Short PHP opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."';
$data = [
$nextVar['content'],
$token['content'],
$nextVar['content'],
];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'EchoFound', $data);
if ($fix === true) {
if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
$phpcsFile->fixer->replaceToken($stackPtr, '<?php echo ');
} else {
$phpcsFile->fixer->replaceToken($stackPtr, '<?php echo');
if ($nextVar['code'] !== T_CLOSE_TAG) {
$error = 'Short PHP opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."';
$data = [
$nextVar['content'],
$token['content'],
$nextVar['content'],
];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'EchoFound', $data);
if ($fix === true) {
if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
$phpcsFile->fixer->replaceToken($stackPtr, '<?php echo ');
} else {
$phpcsFile->fixer->replaceToken($stackPtr, '<?php echo');
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Intentional parse error. This should be the only test in this file.
// Making sure that the sniff does not act on this particular parse error (to not make things worse).

<?= ?>