Skip to content

Commit

Permalink
PSR12/FileHeader: make "SpacingAfter" and "SpacingInside" errorcodes …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
jrfnl committed Nov 9, 2023
1 parent c719944 commit 73c72b9
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,9 @@ public function processHeaderLines(File $phpcsFile, $headerLines)
// this block.
$next = $phpcsFile->findNext(T_WHITESPACE, ($line['end'] + 1), null, true);
if ($next !== false && $tokens[$next]['line'] !== ($tokens[$line['end']]['line'] + 2)) {
$error = 'Header blocks must be separated by a single blank line';
$fix = $phpcsFile->addFixableError($error, $line['end'], 'SpacingAfterBlock');
$error = 'Header blocks must be separated by a single blank line';
$errorCode = 'SpacingAfter'.str_replace(' ', '', ucwords($line['type'])).'Block';
$fix = $phpcsFile->addFixableError($error, $line['end'], $errorCode);
if ($fix === true) {
if ($tokens[$next]['line'] === $tokens[$line['end']]['line']) {
$phpcsFile->fixer->addContentBefore($next, $phpcsFile->eolChar.$phpcsFile->eolChar);
Expand Down Expand Up @@ -340,8 +341,9 @@ public function processHeaderLines(File $phpcsFile, $headerLines)
// blank line after this statement.
$next = $phpcsFile->findNext(T_WHITESPACE, ($line['end'] + 1), null, true);
if ($tokens[$next]['line'] > ($tokens[$line['end']]['line'] + 1)) {
$error = 'Header blocks must not contain blank lines';
$fix = $phpcsFile->addFixableError($error, $line['end'], 'SpacingInsideBlock');
$error = 'Header blocks must not contain blank lines';
$errorCode = 'SpacingInside'.str_replace(' ', '', ucwords($line['type'])).'Block';
$fix = $phpcsFile->addFixableError($error, $line['end'], $errorCode);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($line['end'] + 1); $i < $next; $i++) {
Expand All @@ -358,7 +360,7 @@ public function processHeaderLines(File $phpcsFile, $headerLines)

$phpcsFile->fixer->endChangeset();
}
}
}//end if
}//end if

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

0 comments on commit 73c72b9

Please sign in to comment.