Skip to content

Commit e64a980

Browse files
committed
Fixed bug #2146 : Zend.Files.ClosingTag removes closing tag from end of file without inserting a semicolon
1 parent cc5c930 commit e64a980

11 files changed

+49
-6
lines changed

package.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
5656
- Fixed bug #2143 : PSR2.Namespaces.UseDeclaration does not properly fix "use function" and "use const" statements
5757
-- Thanks to Chris Wilkinson for the patch
5858
- Fixed bug #2144 : Squiz.Arrays.ArrayDeclaration does incorrect align calculation in array with cyrillic keys
59+
- Fixed bug #2146 : Zend.Files.ClosingTag removes closing tag from end of file without inserting a semicolon
5960
- Fixed bug #2151 : XML schema not updated with the new array property syntax
6061
</notes>
6162
<contents>
@@ -1603,7 +1604,13 @@ http://pear.php.net/dtd/package-2.0.xsd">
16031604
</dir>
16041605
<dir name="Files">
16051606
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.1.inc" role="test" />
1607+
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.1.inc.fixed" role="test" />
16061608
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.2.inc" role="test" />
1609+
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.3.inc" role="test" />
1610+
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.3.inc.fixed" role="test" />
1611+
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.4.inc" role="test" />
1612+
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.4.inc.fixed" role="test" />
1613+
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.5.inc" role="test" />
16071614
<file baseinstalldir="PHP/CodeSniffer" name="ClosingTagUnitTest.php" role="test" />
16081615
</dir>
16091616
<dir name="NamingConventions">

src/Standards/Zend/Sniffs/Files/ClosingTagSniff.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHP_CodeSniffer\Sniffs\Sniff;
1313
use PHP_CodeSniffer\Files\File;
14+
use PHP_CodeSniffer\Util\Tokens;
1415

1516
class ClosingTagSniff implements Sniff
1617
{
@@ -51,13 +52,22 @@ public function process(File $phpcsFile, $stackPtr)
5152
$error = 'A closing tag is not permitted at the end of a PHP file';
5253
$fix = $phpcsFile->addFixableError($error, $last, 'NotAllowed');
5354
if ($fix === true) {
54-
$phpcsFile->fixer->replaceToken($last, '');
55+
$phpcsFile->fixer->beginChangeset();
56+
$phpcsFile->fixer->replaceToken($last, $phpcsFile->eolChar);
57+
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($last - 1), null, true);
58+
if ($tokens[$prev]['code'] !== T_SEMICOLON
59+
&& $tokens[$prev]['code'] !== T_CLOSE_CURLY_BRACKET
60+
) {
61+
$phpcsFile->fixer->addContent($prev, ';');
62+
}
63+
64+
$phpcsFile->fixer->endChangeset();
5565
}
5666

5767
$phpcsFile->recordMetric($stackPtr, 'PHP closing tag at EOF', 'yes');
5868
} else {
5969
$phpcsFile->recordMetric($stackPtr, 'PHP closing tag at EOF', 'no');
60-
}
70+
}//end if
6171

6272
// Ignore the rest of the file.
6373
return ($phpcsFile->numTokens + 1);
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div class="clear"></div>
22
<?php include('inc.php'); ?>
3-
</div>
3+
</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php include($this->add('arg'))?>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php include($this->add('arg'));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php include($this->add('arg')) /* comment */ ?>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php include($this->add('arg')); /* comment */
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php function foo() { include($this->add('arg')); } ?>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php function foo() { include($this->add('arg')); }

0 commit comments

Comments
 (0)