Skip to content

Commit f66dc72

Browse files
committed
Avoid creating invalid open tags when replacing short open tags
1 parent f4e70f6 commit f66dc72

File tree

4 files changed

+9
-1
lines changed

4 files changed

+9
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
7272
$data = array($token['content']);
7373
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found', $data);
7474
if ($fix === true) {
75-
$phpcsFile->fixer->replaceToken($stackPtr, '<?php');
75+
$correctOpening = '<?php';
76+
if (isset($tokens[$stackPtr + 1]) && $tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
77+
// Avoid creation of invalid open tags like <?phpecho if the original was <?echo
78+
$correctOpening .= ' ';
79+
}
80+
$phpcsFile->fixer->replaceToken($stackPtr, $correctOpening);
7681
}
7782

7883
$phpcsFile->recordMetric($stackPtr, 'PHP short open tag used', 'yes');

CodeSniffer/Standards/Generic/Tests/PHP/DisallowShortOpenTagUnitTest.2.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ Some content <? echo $var ?> Some more content
44
<?
55
echo $var;
66
?>
7+
<?echo $var; ?>
78
</div>

CodeSniffer/Standards/Generic/Tests/PHP/DisallowShortOpenTagUnitTest.2.inc.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ Some content <?php echo $var ?> Some more content
44
<?php
55
echo $var;
66
?>
7+
<?php echo $var; ?>
78
</div>

CodeSniffer/Standards/Generic/Tests/PHP/DisallowShortOpenTagUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public function getErrorList($testFile='')
9292
2 => 1,
9393
3 => 1,
9494
4 => 1,
95+
7 => 1
9596
);
9697
default:
9798
return array();

0 commit comments

Comments
 (0)