Skip to content

Commit 451a256

Browse files
committed
PSR12.Files.FileHeader now allows a hashbang line at the top of the file (ref #2617)
1 parent 3fd5bff commit 451a256

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

package.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
2626
</stability>
2727
<license uri="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt">BSD 3-Clause License</license>
2828
<notes>
29-
- PSR12.Files.FileHeader now has better detection of file comments
30-
-- No longer ignores comments preceding a use, namespace, or declare statement
29+
- PSR12.Files.FileHeader no longer ignores comments preceding a use, namespace, or declare statement
30+
- PSR12.Files.FileHeader now allows a hashbang line at the top of the file
3131
- Fixed bug #2615 : Constant visibility false positive on non-class constants
3232
- Fixed bug #2616 : PSR12.Files.FileHeader false positive when file only contains docblock
3333
- 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">
10661066
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.6.inc" role="test" />
10671067
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.7.inc" role="test" />
10681068
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.8.inc" role="test" />
1069+
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.9.inc" role="test" />
10691070
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.php" role="test" />
10701071
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.inc" role="test" />
10711072
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.php" role="test" />

src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,19 @@ public function process(File $phpcsFile, $stackPtr)
302302
*/
303303

304304
if ($stackPtr !== 0) {
305-
$error = 'The file header must be the first content in the file';
306-
$phpcsFile->addError($error, $stackPtr, 'HeaderPosition');
305+
// Allow for hashbang lines.
306+
$hashbang = false;
307+
if ($tokens[($stackPtr - 1)]['code'] === T_INLINE_HTML) {
308+
$content = trim($tokens[($stackPtr - 1)]['content']);
309+
if (substr($content, 0, 2) === '#!') {
310+
$hashbang = true;
311+
}
312+
}
313+
314+
if ($hashbang === false) {
315+
$error = 'The file header must be the first content in the file';
316+
$phpcsFile->addError($error, $stackPtr, 'HeaderPosition');
317+
}
307318
}
308319

309320
return $phpcsFile->numTokens;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
use Psr\Container\ContainerInterface;
5+
6+
/** @var ContainerInterface $container */
7+
$container = require __DIR__ . "/../bootstrap.php";

0 commit comments

Comments
 (0)