Skip to content

Commit

Permalink
Merge branch 'webimpress-hotfix/class-declaration-sniff'
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Feb 28, 2019
2 parents a0d57b8 + b06f50c commit 9342da1
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 12 deletions.
55 changes: 43 additions & 12 deletions src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,32 @@ public function processOpen(File $phpcsFile, $stackPtr)
$fix = $phpcsFile->addFixableError($error, $keyword, ucfirst($keywordType).'Line', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($stackPtr + 1); $i < $keyword; $i++) {
if ($tokens[$i]['line'] !== $tokens[($i + 1)]['line']) {
$phpcsFile->fixer->substrToken($i, 0, (strlen($phpcsFile->eolChar) * -1));
$comments = [];

for ($i = ($stackPtr + 1); $i < $keyword; ++$i) {
if ($tokens[$i]['code'] === T_COMMENT) {
$comments[] = trim($tokens[$i]['content']);
}

if ($tokens[$i]['code'] === T_WHITESPACE
|| $tokens[$i]['code'] === T_COMMENT
) {
$phpcsFile->fixer->replaceToken($i, ' ');
}
}

$phpcsFile->fixer->addContentBefore($keyword, ' ');
$phpcsFile->fixer->addContent($stackPtr, ' ');
if (empty($comments) === false) {
$i = $keyword;
while ($tokens[($i + 1)]['line'] === $tokens[$keyword]['line']) {
++$i;
}

$phpcsFile->fixer->addContentBefore($i, ' '.implode(' ', $comments));
}

$phpcsFile->fixer->endChangeset();
}
}//end if
} else {
// Check the whitespace before. Whitespace after is checked
// later by looking at the whitespace before the first class name
Expand Down Expand Up @@ -231,10 +248,11 @@ public function processOpen(File $phpcsFile, $stackPtr)
$classCount = count($classNames);
$checkingImplements = false;
$implementsToken = null;
foreach ($classNames as $i => $className) {
foreach ($classNames as $n => $className) {
if ($tokens[$className]['code'] === $keywordTokenType) {
$checkingImplements = true;
$implementsToken = $className;

continue;
}

Expand Down Expand Up @@ -344,27 +362,40 @@ public function processOpen(File $phpcsFile, $stackPtr)
$prev = ($className - 1);
}

$spaceBefore = $tokens[$prev]['length'];
if ($spaceBefore !== 1) {
$last = $phpcsFile->findPrevious(T_WHITESPACE, $prev, null, true);
$content = $phpcsFile->getTokensAsString(($last + 1), ($prev - $last));
if ($content !== ' ') {
$found = strlen($content);

$error = 'Expected 1 space before "%s"; %s found';
$data = [
$tokens[$className]['content'],
$spaceBefore,
$found,
];

$fix = $phpcsFile->addFixableError($error, $className, 'SpaceBeforeName', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($prev, ' ');
if ($tokens[$prev]['code'] === T_WHITESPACE) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($prev, ' ');
while ($tokens[--$prev]['code'] === T_WHITESPACE) {
$phpcsFile->fixer->replaceToken($prev, ' ');
}

$phpcsFile->fixer->endChangeset();
} else {
$phpcsFile->fixer->addContent($prev, ' ');
}
}
}
}//end if
}//end if
}//end if

if ($checkingImplements === true
&& $tokens[($className + 1)]['code'] !== T_NS_SEPARATOR
&& $tokens[($className + 1)]['code'] !== T_COMMA
) {
if ($i !== ($classCount - 1)) {
if ($n !== ($classCount - 1)) {
// This is not the last class name, and the comma
// is not where we expect it to be.
if ($tokens[($className + 2)]['code'] !== $keywordTokenType) {
Expand Down
68 changes: 68 additions & 0 deletions src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,71 @@ class C2
{

} // phpcs:ignore Standard.Category.Sniff

interface I1 extends
Foo
{
}

interface I2 extends
Bar
{
}

interface I3 extends
Foo,
Bar
{
}

class C1 extends
Foo
{
}

class C2 extends
Bar
{
}

class C3 extends Foo implements
Bar
{
}

class C4 extends Foo implements
Bar
{
}

class C5 extends Foo implements
Bar,
Baz
{
}

class C6 extends \Foo\Bar implements
\Baz\Bar
{
}

interface I4 extends
\Baz
\Bar
{
}

interface I5 extends /* comment */
\Foo\Bar
{
}

interface I6 extends // comment
\Foo\Bar
{
}

class C7 extends // comment
\Foo\Bar implements \Baz\Bar
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,67 @@ class C2
{

} // phpcs:ignore Standard.Category.Sniff

interface I1 extends
Foo
{
}

interface I2 extends
Bar
{
}

interface I3 extends
Foo,
Bar
{
}

class C1 extends Foo
{
}

class C2 extends Bar
{
}

class C3 extends Foo implements
Bar
{
}

class C4 extends Foo implements
Bar
{
}

class C5 extends Foo implements
Bar,
Baz
{
}

class C6 extends \Foo\Bar implements
\Baz\Bar
{
}

interface I4 extends
\Baz\Bar
{
}

interface I5 extends /* comment */
\Foo\Bar
{
}

interface I6 extends // comment
\Foo\Bar
{
}

class C7 extends \Foo\Bar implements \Baz\Bar // comment
{
}
12 changes: 12 additions & 0 deletions src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ public function getErrorList()
130 => 2,
131 => 1,
158 => 1,
168 => 1,
178 => 1,
179 => 1,
184 => 1,
189 => 1,
194 => 1,
204 => 1,
205 => 1,
210 => 1,
215 => 2,
216 => 1,
231 => 2,
];

}//end getErrorList()
Expand Down

0 comments on commit 9342da1

Please sign in to comment.