Skip to content

Commit

Permalink
Added check to make sure the file header is the first thing in the fi…
Browse files Browse the repository at this point in the history
…le (ref squizlabs#750)
  • Loading branch information
gsherwood committed Aug 30, 2019
1 parent c573001 commit a10963b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.3.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.4.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.4.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.5.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
21 changes: 20 additions & 1 deletion src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ public function process(File $phpcsFile, $stackPtr)
$next = $phpcsFile->findNext(T_WHITESPACE, ($next + 1), null, true);
} while ($next !== false);

if (count($headerLines) === 1) {
// This is only an open tag and doesn't contain the file header.
// We need to keep checking for one in case they put it further
// down in the file.
return;
}

/*
Next, check the spacing and grouping of the statements
inside each header block.
Expand Down Expand Up @@ -213,7 +220,7 @@ public function process(File $phpcsFile, $stackPtr)
}//end foreach

/*
Finally, check that the order of the header blocks
Next, check that the order of the header blocks
is correct:
Opening php tag.
File-level docblock.
Expand Down Expand Up @@ -269,6 +276,18 @@ public function process(File $phpcsFile, $stackPtr)
}//end if
}//end foreach

/*
Finally, make sure this header block is at the very
top of the file.
*/

if ($stackPtr !== 0) {
$error = 'The file header must be the first content in the file';
$phpcsFile->addError($error, $stackPtr, 'HeaderPosition');
}

return $phpcsFile->numTokens;

}//end process()


Expand Down
18 changes: 18 additions & 0 deletions src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.5.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
echo 'Some content';
?>
<?php

/**
* The header is not the first thing in the file.
*/

namespace Vendor\Package;

/**
* FooBar is an example class.
*/
class FooBar
{
// ... additional PHP code ...
}
2 changes: 2 additions & 0 deletions src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function getErrorList($testFile='')
3 => 1,
7 => 1,
];
case 'FileHeaderUnitTest.5.inc':
return [4 => 1];
default:
return [];
}//end switch
Expand Down

0 comments on commit a10963b

Please sign in to comment.