Skip to content

Commit 73c72b9

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. `SpacingAfterDeclareBlock`, `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 using: ```xml <rule ref="PSR12.Files.FileHeader"> <exclude name="PSR12.Files.FileHeader.SpacingAfterTagBlock"/> </rule> ``` Fixes #3453
1 parent c719944 commit 73c72b9

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,9 @@ public function processHeaderLines(File $phpcsFile, $headerLines)
301301
// this block.
302302
$next = $phpcsFile->findNext(T_WHITESPACE, ($line['end'] + 1), null, true);
303303
if ($next !== false && $tokens[$next]['line'] !== ($tokens[$line['end']]['line'] + 2)) {
304-
$error = 'Header blocks must be separated by a single blank line';
305-
$fix = $phpcsFile->addFixableError($error, $line['end'], 'SpacingAfterBlock');
304+
$error = 'Header blocks must be separated by a single blank line';
305+
$errorCode = 'SpacingAfter'.str_replace(' ', '', ucwords($line['type'])).'Block';
306+
$fix = $phpcsFile->addFixableError($error, $line['end'], $errorCode);
306307
if ($fix === true) {
307308
if ($tokens[$next]['line'] === $tokens[$line['end']]['line']) {
308309
$phpcsFile->fixer->addContentBefore($next, $phpcsFile->eolChar.$phpcsFile->eolChar);
@@ -340,8 +341,9 @@ public function processHeaderLines(File $phpcsFile, $headerLines)
340341
// blank line after this statement.
341342
$next = $phpcsFile->findNext(T_WHITESPACE, ($line['end'] + 1), null, true);
342343
if ($tokens[$next]['line'] > ($tokens[$line['end']]['line'] + 1)) {
343-
$error = 'Header blocks must not contain blank lines';
344-
$fix = $phpcsFile->addFixableError($error, $line['end'], 'SpacingInsideBlock');
344+
$error = 'Header blocks must not contain blank lines';
345+
$errorCode = 'SpacingInside'.str_replace(' ', '', ucwords($line['type'])).'Block';
346+
$fix = $phpcsFile->addFixableError($error, $line['end'], $errorCode);
345347
if ($fix === true) {
346348
$phpcsFile->fixer->beginChangeset();
347349
for ($i = ($line['end'] + 1); $i < $next; $i++) {
@@ -358,7 +360,7 @@ public function processHeaderLines(File $phpcsFile, $headerLines)
358360

359361
$phpcsFile->fixer->endChangeset();
360362
}
361-
}
363+
}//end if
362364
}//end if
363365

364366
if (isset($found[$line['type']]) === false) {

0 commit comments

Comments
 (0)