From c83c37b227ac192ffc08721f534dc1c563a8df88 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 1 Dec 2019 13:51:28 +0100 Subject: [PATCH] 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 ``` Fixes #3453 --- src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php b/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php index d2b10aef0e..86a600b088 100644 --- a/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php +++ b/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php @@ -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); @@ -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++) { @@ -358,7 +360,7 @@ public function processHeaderLines(File $phpcsFile, $headerLines) $phpcsFile->fixer->endChangeset(); } - } + }//end if }//end if if (isset($found[$line['type']]) === false) {