Skip to content

Commit a10963b

Browse files
committed
Added check to make sure the file header is the first thing in the file (ref #750)
1 parent c573001 commit a10963b

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
11551155
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.3.inc" role="test" />
11561156
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.4.inc" role="test" />
11571157
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.4.inc.fixed" role="test" />
1158+
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.5.inc" role="test" />
11581159
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.php" role="test" />
11591160
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.inc" role="test" />
11601161
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.php" role="test" />

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ public function process(File $phpcsFile, $stackPtr)
130130
$next = $phpcsFile->findNext(T_WHITESPACE, ($next + 1), null, true);
131131
} while ($next !== false);
132132

133+
if (count($headerLines) === 1) {
134+
// This is only an open tag and doesn't contain the file header.
135+
// We need to keep checking for one in case they put it further
136+
// down in the file.
137+
return;
138+
}
139+
133140
/*
134141
Next, check the spacing and grouping of the statements
135142
inside each header block.
@@ -213,7 +220,7 @@ public function process(File $phpcsFile, $stackPtr)
213220
}//end foreach
214221

215222
/*
216-
Finally, check that the order of the header blocks
223+
Next, check that the order of the header blocks
217224
is correct:
218225
Opening php tag.
219226
File-level docblock.
@@ -269,6 +276,18 @@ public function process(File $phpcsFile, $stackPtr)
269276
}//end if
270277
}//end foreach
271278

279+
/*
280+
Finally, make sure this header block is at the very
281+
top of the file.
282+
*/
283+
284+
if ($stackPtr !== 0) {
285+
$error = 'The file header must be the first content in the file';
286+
$phpcsFile->addError($error, $stackPtr, 'HeaderPosition');
287+
}
288+
289+
return $phpcsFile->numTokens;
290+
272291
}//end process()
273292

274293

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
echo 'Some content';
3+
?>
4+
<?php
5+
6+
/**
7+
* The header is not the first thing in the file.
8+
*/
9+
10+
namespace Vendor\Package;
11+
12+
/**
13+
* FooBar is an example class.
14+
*/
15+
class FooBar
16+
{
17+
// ... additional PHP code ...
18+
}

src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public function getErrorList($testFile='')
4848
3 => 1,
4949
7 => 1,
5050
];
51+
case 'FileHeaderUnitTest.5.inc':
52+
return [4 => 1];
5153
default:
5254
return [];
5355
}//end switch

0 commit comments

Comments
 (0)