Skip to content

Commit

Permalink
Fixed bug #773 : Syntax error when stripping trailing PHP close tag a…
Browse files Browse the repository at this point in the history
…nd previous statement has no semicolon
  • Loading branch information
gsherwood committed Nov 23, 2015
1 parent 58b328b commit e955837
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
11 changes: 10 additions & 1 deletion CodeSniffer/Standards/PSR2/Sniffs/Files/ClosingTagSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

echo 'hi';

?>

<?php

echo 'bye';



Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php echo $foo //hello ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php echo $foo; //hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php $foo = true; if ($foo) { echo 'hi'; } //hello ?>
14 changes: 9 additions & 5 deletions CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
5 changes: 5 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1310,8 +1311,12 @@ http://pear.php.net/dtd/package-2.0.xsd">
</dir>
<dir name="Files">
<file baseinstalldir="PHP" name="ClosingTagUnitTest.1.inc" role="test" />
<file baseinstalldir="PHP" name="ClosingTagUnitTest.1.inc.fixed" role="test" />
<file baseinstalldir="PHP" name="ClosingTagUnitTest.2.inc" role="test" />
<file baseinstalldir="PHP" name="ClosingTagUnitTest.3.inc" role="test" />
<file baseinstalldir="PHP" name="ClosingTagUnitTest.4.inc" role="test" />
<file baseinstalldir="PHP" name="ClosingTagUnitTest.4.inc.fixed" role="test" />
<file baseinstalldir="PHP" name="ClosingTagUnitTest.5.inc" role="test" />
<file baseinstalldir="PHP" name="ClosingTagUnitTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
Expand Down

0 comments on commit e955837

Please sign in to comment.