Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-fix more of PSR12.Files.FileHeader #3764

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.2.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.2.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.3.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.3.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.4.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.4.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.5.inc" role="test" />
Expand Down
55 changes: 52 additions & 3 deletions src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
27 changes: 27 additions & 0 deletions src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.3.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* This file contains an example of coding styles.
*/

declare(strict_types=1);

namespace Vendor\Package;

use Vendor\Package\{ClassA as A, ClassB, ClassC as C};
use Vendor\Package\SomeNamespace\ClassD as D;
use Vendor\Package\AnotherNamespace\ClassE as E;

use function Vendor\Package\{functionA, functionB, functionC};
use function Another\Vendor\functionD;

use const Another\Vendor\CONSTANT_D;
use const Vendor\Package\{CONSTANT_A, CONSTANT_B, CONSTANT_C};

/**
* FooBar is an example class.
*/
class FooBar
{
// ... additional PHP code ...
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Vendor\Package;

declare(strict_types=1);

namespace Vendor\Package;

use Vendor\Package\{ClassA as A, ClassB, ClassC as C};
use Vendor\Package\SomeNamespace\ClassD as D;
use Vendor\Package\AnotherNamespace\ClassE as E;
Expand Down