Skip to content

Commit

Permalink
fix(FullyQualifiedNamespace): Fix fixer for new use statement group (…
Browse files Browse the repository at this point in the history
…#3214374)
  • Loading branch information
klausi authored Jan 3, 2025
1 parent 59f52b8 commit b7fc828
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,20 @@ public function process(File $phpcsFile, $stackPtr)
if ($useStatement !== false && empty($tokens[$useStatement]['conditions']) === true) {
$phpcsFile->fixer->addContentBefore($useStatement, "$use\n");
} else {
// Check if there is an @file comment.
$beginning = 0;
$fileComment = $phpcsFile->findNext(T_WHITESPACE, ($beginning + 1), null, true);
if ($tokens[$fileComment]['code'] === T_DOC_COMMENT_OPEN_TAG) {
$beginning = $tokens[$fileComment]['comment_closer'];
// Check if there is a namespace declaration and add it there.
$namespace = $phpcsFile->findNext(T_NAMESPACE, 0);
if ($namespace !== false) {
$beginning = $phpcsFile->findEndOfStatement($namespace);
$phpcsFile->fixer->addContent($beginning, "\n\n$use\n");
} else {
$phpcsFile->fixer->addContent($beginning, "$use\n");
// Check if there is an @file comment.
$fileComment = $phpcsFile->findNext(T_WHITESPACE, 1, null, true);
if ($tokens[$fileComment]['code'] === T_DOC_COMMENT_OPEN_TAG) {
$beginning = $tokens[$fileComment]['comment_closer'];
$phpcsFile->fixer->addContent($beginning, "\n\n$use\n");
} else {
$phpcsFile->fixer->addContent(0, "\n\n$use\n");
}
}
}
}//end if
Expand Down
9 changes: 9 additions & 0 deletions tests/Drupal/Classes/FullyQualifiedNamespaceUnitTest.2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Namespace2;

/**
* Class C's description.
*/
class C extends \Namespace1\D {
}
11 changes: 11 additions & 0 deletions tests/Drupal/Classes/FullyQualifiedNamespaceUnitTest.2.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Namespace2;

use Namespace1\D;

/**
* Class C's description.
*/
class C extends D {
}
11 changes: 11 additions & 0 deletions tests/Drupal/Classes/FullyQualifiedNamespaceUnitTest.3.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Namespace2;

/**
* Class C's description.
*/
class C extends \Namespace1\D implements \Namespace1\I {
}
14 changes: 14 additions & 0 deletions tests/Drupal/Classes/FullyQualifiedNamespaceUnitTest.3.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Namespace2;

use Namespace1\I;
use Namespace1\D;

/**
* Class C's description.
*/
class C extends D implements I {
}
6 changes: 6 additions & 0 deletions tests/Drupal/Classes/FullyQualifiedNamespaceUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ protected function getErrorList(string $testFile): array
];
case 'FullyQualifiedNamespaceUnitTest.1.inc':
return [16 => 1];
case 'FullyQualifiedNamespaceUnitTest.2.inc':
return [8 => 1];
case 'FullyQualifiedNamespaceUnitTest.3.inc':
return [10 => 2];
}

return [];
Expand Down Expand Up @@ -69,6 +73,8 @@ protected function getTestFiles($testFileBase): array
return [
__DIR__.'/FullyQualifiedNamespaceUnitTest.inc',
__DIR__.'/FullyQualifiedNamespaceUnitTest.1.inc',
__DIR__.'/FullyQualifiedNamespaceUnitTest.2.inc',
__DIR__.'/FullyQualifiedNamespaceUnitTest.3.inc',
__DIR__.'/FullyQualifiedNamespaceUnitTest.api.php',
];

Expand Down

0 comments on commit b7fc828

Please sign in to comment.