Skip to content

Commit

Permalink
Fixed bug #2146 : Zend.Files.ClosingTag removes closing tag from end …
Browse files Browse the repository at this point in the history
…of file without inserting a semicolon
  • Loading branch information
gsherwood committed Sep 5, 2018
1 parent cc5c930 commit e64a980
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 6 deletions.
7 changes: 7 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #2143 : PSR2.Namespaces.UseDeclaration does not properly fix "use function" and "use const" statements
-- Thanks to Chris Wilkinson for the patch
- Fixed bug #2144 : Squiz.Arrays.ArrayDeclaration does incorrect align calculation in array with cyrillic keys
- Fixed bug #2146 : Zend.Files.ClosingTag removes closing tag from end of file without inserting a semicolon
- Fixed bug #2151 : XML schema not updated with the new array property syntax
</notes>
<contents>
Expand Down Expand Up @@ -1603,7 +1604,13 @@ http://pear.php.net/dtd/package-2.0.xsd">
</dir>
<dir name="Files">
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.1.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.1.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.2.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.3.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.3.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.4.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.4.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.5.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.php" role="test" />
</dir>
<dir name="NamingConventions">
Expand Down
14 changes: 12 additions & 2 deletions src/Standards/Zend/Sniffs/Files/ClosingTagSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;

class ClosingTagSniff implements Sniff
{
Expand Down Expand Up @@ -51,13 +52,22 @@ public function process(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(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 EOF', 'yes');
} else {
$phpcsFile->recordMetric($stackPtr, 'PHP closing tag at EOF', 'no');
}
}//end if

// Ignore the rest of the file.
return ($phpcsFile->numTokens + 1);
Expand Down
12 changes: 12 additions & 0 deletions src/Standards/Zend/Tests/Files/ClosingTagUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

echo 'hi';

?>

<?php

echo 'bye';



2 changes: 1 addition & 1 deletion src/Standards/Zend/Tests/Files/ClosingTagUnitTest.2.inc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div class="clear"></div>
<?php include('inc.php'); ?>
</div>
</div>
1 change: 1 addition & 0 deletions src/Standards/Zend/Tests/Files/ClosingTagUnitTest.3.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php include($this->add('arg'))?>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php include($this->add('arg'));
1 change: 1 addition & 0 deletions src/Standards/Zend/Tests/Files/ClosingTagUnitTest.4.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php include($this->add('arg')) /* comment */ ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php include($this->add('arg')); /* comment */
1 change: 1 addition & 0 deletions src/Standards/Zend/Tests/Files/ClosingTagUnitTest.5.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php function foo() { include($this->add('arg')); } ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php function foo() { include($this->add('arg')); }
14 changes: 11 additions & 3 deletions src/Standards/Zend/Tests/Files/ClosingTagUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ class ClosingTagUnitTest extends AbstractSniffUnitTest
*/
public function getErrorList($testFile='')
{
if ($testFile !== 'ClosingTagUnitTest.1.inc') {
switch ($testFile) {
case 'ClosingTagUnitTest.1.inc':
return [11 => 1];
break;
case 'ClosingTagUnitTest.3.inc':
case 'ClosingTagUnitTest.4.inc':
case 'ClosingTagUnitTest.5.inc':
return [1 => 1];
break;
default:
return [];
break;
}

return [11 => 1];

}//end getErrorList()


Expand Down

0 comments on commit e64a980

Please sign in to comment.