Skip to content

Commit e955837

Browse files
committed
Fixed bug #773 : Syntax error when stripping trailing PHP close tag and previous statement has no semicolon
1 parent 58b328b commit e955837

File tree

7 files changed

+39
-6
lines changed

7 files changed

+39
-6
lines changed

CodeSniffer/Standards/PSR2/Sniffs/Files/ClosingTagSniff.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,16 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
7474
$error = 'A closing tag is not permitted at the end of a PHP file';
7575
$fix = $phpcsFile->addFixableError($error, $last, 'NotAllowed');
7676
if ($fix === true) {
77-
$phpcsFile->fixer->replaceToken($last, '');
77+
$phpcsFile->fixer->beginChangeset();
78+
$phpcsFile->fixer->replaceToken($last, $phpcsFile->eolChar);
79+
$prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($last - 1), null, true);
80+
if ($tokens[$prev]['code'] !== T_SEMICOLON
81+
&& $tokens[$prev]['code'] !== T_CLOSE_CURLY_BRACKET
82+
) {
83+
$phpcsFile->fixer->addContent($prev, ';');
84+
}
85+
86+
$phpcsFile->fixer->endChangeset();
7887
}
7988

8089
$phpcsFile->recordMetric($stackPtr, 'PHP closing tag at end of PHP-only file', 'yes');
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
echo 'hi';
4+
5+
?>
6+
7+
<?php
8+
9+
echo 'bye';
10+
11+
12+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php echo $foo //hello ?>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php echo $foo; //hello
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php $foo = true; if ($foo) { echo 'hi'; } //hello ?>

CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,18 @@ class PSR2_Tests_Files_ClosingTagUnitTest extends AbstractSniffUnitTest
4242
*/
4343
public function getErrorList($testFile='')
4444
{
45-
if ($testFile !== 'ClosingTagUnitTest.1.inc') {
45+
switch ($testFile) {
46+
case 'ClosingTagUnitTest.1.inc':
47+
return array(11 => 1);
48+
49+
case 'ClosingTagUnitTest.4.inc':
50+
case 'ClosingTagUnitTest.5.inc':
51+
return array(1 => 1);
52+
53+
default:
4654
return array();
4755
}
4856

49-
return array(
50-
11 => 1,
51-
);
52-
5357
}//end getErrorList()
5458

5559

package.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
7474
- Fixed bug #769 : Incorrect detection of variable reference operator when used with short array syntax
7575
-- Thanks to Klaus Purer for the patch
7676
- Fixed bug #772 : Syntax error when using PHPCBF on alternative style foreach loops
77+
- Fixed bug #773 : Syntax error when stripping trailing PHP close tag and previous statement has no semicolon
7778
- Fixed bug #778 : PHPCBF creates invalid PHP for inline FOREACH containing multiple control structures
7879
- Fixed bug #781 : Incorrect checking for PHP7 return types on multi-line function declartions
7980
- Fixed bug #782 : Conditional function declarations cause fixing conflicts in Squiz standard
@@ -1310,8 +1311,12 @@ http://pear.php.net/dtd/package-2.0.xsd">
13101311
</dir>
13111312
<dir name="Files">
13121313
<file baseinstalldir="PHP" name="ClosingTagUnitTest.1.inc" role="test" />
1314+
<file baseinstalldir="PHP" name="ClosingTagUnitTest.1.inc.fixed" role="test" />
13131315
<file baseinstalldir="PHP" name="ClosingTagUnitTest.2.inc" role="test" />
13141316
<file baseinstalldir="PHP" name="ClosingTagUnitTest.3.inc" role="test" />
1317+
<file baseinstalldir="PHP" name="ClosingTagUnitTest.4.inc" role="test" />
1318+
<file baseinstalldir="PHP" name="ClosingTagUnitTest.4.inc.fixed" role="test" />
1319+
<file baseinstalldir="PHP" name="ClosingTagUnitTest.5.inc" role="test" />
13151320
<file baseinstalldir="PHP" name="ClosingTagUnitTest.php" role="test">
13161321
<tasks:replace from="@package_version@" to="version" type="package-info" />
13171322
</file>

0 commit comments

Comments
 (0)