From e955837e887ca4d5ce27aabab43807e4d427721c Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Mon, 23 Nov 2015 13:05:05 +1100 Subject: [PATCH] Fixed bug #773 : Syntax error when stripping trailing PHP close tag and previous statement has no semicolon --- .../PSR2/Sniffs/Files/ClosingTagSniff.php | 11 ++++++++++- .../Tests/Files/ClosingTagUnitTest.1.inc.fixed | 12 ++++++++++++ .../PSR2/Tests/Files/ClosingTagUnitTest.4.inc | 1 + .../Tests/Files/ClosingTagUnitTest.4.inc.fixed | 1 + .../PSR2/Tests/Files/ClosingTagUnitTest.5.inc | 1 + .../PSR2/Tests/Files/ClosingTagUnitTest.php | 14 +++++++++----- package.xml | 5 +++++ 7 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.1.inc.fixed create mode 100644 CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.4.inc create mode 100644 CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.4.inc.fixed create mode 100644 CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.5.inc diff --git a/CodeSniffer/Standards/PSR2/Sniffs/Files/ClosingTagSniff.php b/CodeSniffer/Standards/PSR2/Sniffs/Files/ClosingTagSniff.php index cbf161603f..efcd4b6875 100644 --- a/CodeSniffer/Standards/PSR2/Sniffs/Files/ClosingTagSniff.php +++ b/CodeSniffer/Standards/PSR2/Sniffs/Files/ClosingTagSniff.php @@ -74,7 +74,16 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $error = 'A closing tag is not permitted at the end of a PHP file'; $fix = $phpcsFile->addFixableError($error, $last, 'NotAllowed'); if ($fix === true) { - $phpcsFile->fixer->replaceToken($last, ''); + $phpcsFile->fixer->beginChangeset(); + $phpcsFile->fixer->replaceToken($last, $phpcsFile->eolChar); + $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($last - 1), null, true); + if ($tokens[$prev]['code'] !== T_SEMICOLON + && $tokens[$prev]['code'] !== T_CLOSE_CURLY_BRACKET + ) { + $phpcsFile->fixer->addContent($prev, ';'); + } + + $phpcsFile->fixer->endChangeset(); } $phpcsFile->recordMetric($stackPtr, 'PHP closing tag at end of PHP-only file', 'yes'); diff --git a/CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.1.inc.fixed b/CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.1.inc.fixed new file mode 100644 index 0000000000..f70b9ebada --- /dev/null +++ b/CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.1.inc.fixed @@ -0,0 +1,12 @@ + + + diff --git a/CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.4.inc.fixed b/CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.4.inc.fixed new file mode 100644 index 0000000000..1058f1f374 --- /dev/null +++ b/CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.4.inc.fixed @@ -0,0 +1 @@ + diff --git a/CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.php b/CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.php index deda740592..3976e7f067 100644 --- a/CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.php +++ b/CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.php @@ -42,14 +42,18 @@ class PSR2_Tests_Files_ClosingTagUnitTest extends AbstractSniffUnitTest */ public function getErrorList($testFile='') { - if ($testFile !== 'ClosingTagUnitTest.1.inc') { + switch ($testFile) { + case 'ClosingTagUnitTest.1.inc': + return array(11 => 1); + + case 'ClosingTagUnitTest.4.inc': + case 'ClosingTagUnitTest.5.inc': + return array(1 => 1); + + default: return array(); } - return array( - 11 => 1, - ); - }//end getErrorList() diff --git a/package.xml b/package.xml index d89fb2c371..0f4ccf3135 100644 --- a/package.xml +++ b/package.xml @@ -74,6 +74,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> - Fixed bug #769 : Incorrect detection of variable reference operator when used with short array syntax -- Thanks to Klaus Purer for the patch - Fixed bug #772 : Syntax error when using PHPCBF on alternative style foreach loops + - Fixed bug #773 : Syntax error when stripping trailing PHP close tag and previous statement has no semicolon - Fixed bug #778 : PHPCBF creates invalid PHP for inline FOREACH containing multiple control structures - Fixed bug #781 : Incorrect checking for PHP7 return types on multi-line function declartions - 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"> + + + +