Skip to content

Commit

Permalink
PSR12.Files.FileHeader now allows a hashbang line at the top of the f…
Browse files Browse the repository at this point in the history
…ile (ref #2617)
  • Loading branch information
gsherwood committed Oct 3, 2019
1 parent 3fd5bff commit 451a256
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
5 changes: 3 additions & 2 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
</stability>
<license uri="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt">BSD 3-Clause License</license>
<notes>
- 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
Expand Down Expand Up @@ -1066,6 +1066,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.6.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.7.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.8.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.9.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.php" role="test" />
Expand Down
15 changes: 13 additions & 2 deletions src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.9.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env php
<?php

use Psr\Container\ContainerInterface;

/** @var ContainerInterface $container */
$container = require __DIR__ . "/../bootstrap.php";

0 comments on commit 451a256

Please sign in to comment.