diff --git a/package.xml b/package.xml index 9338b46e79..89ef8a4581 100644 --- a/package.xml +++ b/package.xml @@ -1214,6 +1214,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + diff --git a/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php b/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php index 8a8255cf47..6fd2edcc15 100644 --- a/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php +++ b/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php @@ -332,8 +332,25 @@ public function processHeaderLines(File $phpcsFile, $headerLines) $headerLines[($i + 1)]['type'], $tokens[$found[$headerLines[($i + 1)]['type']]['start']]['line'], ]; - $phpcsFile->addError($error, $headerLines[($i + 1)]['start'], 'IncorrectGrouping', $data); - } + + $fix = $phpcsFile->addFixableError($error, $headerLines[($i + 1)]['start'], 'IncorrectGrouping', $data); + + if ($fix === true) { + $phpcsFile->fixer->beginChangeset(); + + $nextLine = $headerLines[($i + 1)]; + $type = $nextLine['type']; + + $nextLineContent = ''; + for ($j = $nextLine['start']; $j <= ($nextLine['end'] + 1); $j++) { + $nextLineContent .= $tokens[$j]['content']; + $phpcsFile->fixer->replaceToken($j, ''); + } + + $phpcsFile->fixer->addContent(($found[$type]['end'] + 1), $nextLineContent); + $phpcsFile->fixer->endChangeset(); + } + }//end if } else if ($headerLines[($i + 1)]['type'] === $line['type']) { // Still in the same block, so make sure there is no // blank line after this statement. @@ -418,8 +435,40 @@ public function processHeaderLines(File $phpcsFile, $headerLines) $blockOrder[$type], $blockOrder[$prevValidType], ]; - $phpcsFile->addError($error, $found[$type]['start'], 'IncorrectOrder', $data); + + if (isset($previousType) === false) { + $phpcsFile->addError($error, $found[$type]['start'], 'IncorrectOrder', $data); + } else { + $fix = $phpcsFile->addFixableError($error, $found[$type]['start'], 'IncorrectOrder', $data); + + if ($fix === true) { + $phpcsFile->fixer->beginChangeset(); + + $previousTypeContent = ''; + for ($i = $found[$previousType]['start']; $i <= $found[$previousType]['end']; $i++) { + $previousTypeContent .= $tokens[$i]['content']; + if ($i > $found[$previousType]['start']) { + $phpcsFile->fixer->replaceToken($i, ''); + } + } + + $typeContent = ''; + for ($i = $found[$type]['start']; $i <= $found[$type]['end']; $i++) { + $typeContent .= $tokens[$i]['content']; + if ($i > $found[$type]['start']) { + $phpcsFile->fixer->replaceToken($i, ''); + } + } + + $phpcsFile->fixer->replaceToken($found[$type]['start'], $previousTypeContent); + $phpcsFile->fixer->replaceToken($found[$previousType]['start'], $typeContent); + + $phpcsFile->fixer->endChangeset(); + }//end if + }//end if }//end if + + $previousType = $type; }//end foreach }//end processHeaderLines() diff --git a/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.3.inc.fixed b/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.3.inc.fixed new file mode 100644 index 0000000000..5f5eb54d22 --- /dev/null +++ b/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.3.inc.fixed @@ -0,0 +1,27 @@ +