Skip to content

Commit f4af289

Browse files
authored
Generic/DisallowShortOpenTag: prevent fixer conflict (#835)
The original fixer conflict which inspired this fix can be reproduced via: ```bash phpcbf -ps ./src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.19.inc --report=full,source --suffix=.fixed --standard=Squiz ``` What it basically comes down to is that this sniff would replace the `<?=` with `<?php echo`, even in case of a parse error, which would then cause problems for other sniffs. Fixed now by ignoring this particular parse error. Includes test. Note: this PR will be most straight-forward to review while ignoring whitespace changes.
1 parent b400aaa commit f4af289

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/Standards/Generic/Sniffs/PHP/DisallowShortOpenTagSniff.php

+14-12
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,20 @@ public function process(File $phpcsFile, $stackPtr)
7474

7575
if ($token['code'] === T_OPEN_TAG_WITH_ECHO) {
7676
$nextVar = $tokens[$phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true)];
77-
$error = 'Short PHP opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."';
78-
$data = [
79-
$nextVar['content'],
80-
$token['content'],
81-
$nextVar['content'],
82-
];
83-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'EchoFound', $data);
84-
if ($fix === true) {
85-
if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
86-
$phpcsFile->fixer->replaceToken($stackPtr, '<?php echo ');
87-
} else {
88-
$phpcsFile->fixer->replaceToken($stackPtr, '<?php echo');
77+
if ($nextVar['code'] !== T_CLOSE_TAG) {
78+
$error = 'Short PHP opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."';
79+
$data = [
80+
$nextVar['content'],
81+
$token['content'],
82+
$nextVar['content'],
83+
];
84+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'EchoFound', $data);
85+
if ($fix === true) {
86+
if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
87+
$phpcsFile->fixer->replaceToken($stackPtr, '<?php echo ');
88+
} else {
89+
$phpcsFile->fixer->replaceToken($stackPtr, '<?php echo');
90+
}
8991
}
9092
}
9193
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Intentional parse error. This should be the only test in this file.
2+
// Making sure that the sniff does not act on this particular parse error (to not make things worse).
3+
4+
<?= ?>

0 commit comments

Comments
 (0)