diff --git a/package.xml b/package.xml index 789e94e86b..e79babcc57 100644 --- a/package.xml +++ b/package.xml @@ -26,8 +26,8 @@ http://pear.php.net/dtd/package-2.0.xsd"> BSD 3-Clause License - - PSR12.Files.FileHeader now has better detection of file comments - -- No longer ignores comments preceding a use, namespace, or declare statement + - PSR12.Files.FileHeader no longer ignores comments preceding a use, namespace, or declare statement + - PSR12.Files.FileHeader now allows a hashbang line at the top of the file - Fixed bug #2615 : Constant visibility false positive on non-class constants - Fixed bug #2616 : PSR12.Files.FileHeader false positive when file only contains docblock - Fixed bug #2619 : PSR12.Files.FileHeader locks up when inline comment is the last content in a file @@ -1066,6 +1066,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + diff --git a/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php b/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php index 8961926e23..cf40ccd2af 100644 --- a/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php +++ b/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php @@ -302,8 +302,19 @@ public function process(File $phpcsFile, $stackPtr) */ if ($stackPtr !== 0) { - $error = 'The file header must be the first content in the file'; - $phpcsFile->addError($error, $stackPtr, 'HeaderPosition'); + // Allow for hashbang lines. + $hashbang = false; + if ($tokens[($stackPtr - 1)]['code'] === T_INLINE_HTML) { + $content = trim($tokens[($stackPtr - 1)]['content']); + if (substr($content, 0, 2) === '#!') { + $hashbang = true; + } + } + + if ($hashbang === false) { + $error = 'The file header must be the first content in the file'; + $phpcsFile->addError($error, $stackPtr, 'HeaderPosition'); + } } return $phpcsFile->numTokens; diff --git a/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.9.inc b/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.9.inc new file mode 100644 index 0000000000..6902dc2a70 --- /dev/null +++ b/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.9.inc @@ -0,0 +1,7 @@ +#!/usr/bin/env php +