Skip to content

Commit 7b79003

Browse files
committed
PSR12/FileHeader: make "SpacingAfter" and "SpacingInside" errorcodes modular
This changes the error codes for the `SpacingAfterBlock` and `SpacingInsideBlock` errors to modular error codes which include a reference to the type of header block, i.e. `SpacingAfterUseBlock`, `SpacingInsideUseFunctionBlock` etc. This allows for more selective application of the rules. Take for instance the quite common case of the header blocks all being separated by blank lines, except for the open tag and file docblock. ```php <?php /** * File docblock. */ namespace A\B\C; use B\C; ``` The modular errorcodes I'm proposing in this PR will allow for the sniff to be used to safeguard the blank lines between each section without enforcing a blank line between the PHP open tag and the file docblock.
1 parent 0676055 commit 7b79003

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ public function process(File $phpcsFile, $stackPtr)
178178
// this block.
179179
$next = $phpcsFile->findNext(T_WHITESPACE, ($line['end'] + 1), null, true);
180180
if ($next !== false && $tokens[$next]['line'] !== ($tokens[$line['end']]['line'] + 2)) {
181-
$error = 'Header blocks must be separated by a single blank line';
182-
$fix = $phpcsFile->addFixableError($error, $line['end'], 'SpacingAfterBlock');
181+
$error = 'Header blocks must be separated by a single blank line';
182+
$errorCode = 'SpacingAfter'.str_replace(' ', '', ucwords($line['type'])).'Block';
183+
$fix = $phpcsFile->addFixableError($error, $line['end'], $errorCode);
183184
if ($fix === true) {
184185
if ($tokens[$next]['line'] === $tokens[$line['end']]['line']) {
185186
$phpcsFile->fixer->addNewlineBefore($next);
@@ -218,8 +219,9 @@ public function process(File $phpcsFile, $stackPtr)
218219
// blank line after this statement.
219220
$next = $phpcsFile->findNext(T_WHITESPACE, ($line['end'] + 1), null, true);
220221
if ($tokens[$next]['line'] > ($tokens[$line['end']]['line'] + 1)) {
221-
$error = 'Header blocks must not contain blank lines';
222-
$fix = $phpcsFile->addFixableError($error, $line['end'], 'SpacingInsideBlock');
222+
$error = 'Header blocks must not contain blank lines';
223+
$errorCode = 'SpacingInside'.str_replace(' ', '', ucwords($line['type'])).'Block';
224+
$fix = $phpcsFile->addFixableError($error, $line['end'], $errorCode);
223225
if ($fix === true) {
224226
$phpcsFile->fixer->beginChangeset();
225227
for ($i = ($line['end'] + 1); $i < $next; $i++) {

0 commit comments

Comments
 (0)