Skip to content

Commit b7fc828

Browse files
authored
fix(FullyQualifiedNamespace): Fix fixer for new use statement group (#3214374)
1 parent 59f52b8 commit b7fc828

6 files changed

+63
-6
lines changed

coder_sniffer/Drupal/Sniffs/Classes/FullyQualifiedNamespaceSniff.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,20 @@ public function process(File $phpcsFile, $stackPtr)
187187
if ($useStatement !== false && empty($tokens[$useStatement]['conditions']) === true) {
188188
$phpcsFile->fixer->addContentBefore($useStatement, "$use\n");
189189
} else {
190-
// Check if there is an @file comment.
191-
$beginning = 0;
192-
$fileComment = $phpcsFile->findNext(T_WHITESPACE, ($beginning + 1), null, true);
193-
if ($tokens[$fileComment]['code'] === T_DOC_COMMENT_OPEN_TAG) {
194-
$beginning = $tokens[$fileComment]['comment_closer'];
190+
// Check if there is a namespace declaration and add it there.
191+
$namespace = $phpcsFile->findNext(T_NAMESPACE, 0);
192+
if ($namespace !== false) {
193+
$beginning = $phpcsFile->findEndOfStatement($namespace);
195194
$phpcsFile->fixer->addContent($beginning, "\n\n$use\n");
196195
} else {
197-
$phpcsFile->fixer->addContent($beginning, "$use\n");
196+
// Check if there is an @file comment.
197+
$fileComment = $phpcsFile->findNext(T_WHITESPACE, 1, null, true);
198+
if ($tokens[$fileComment]['code'] === T_DOC_COMMENT_OPEN_TAG) {
199+
$beginning = $tokens[$fileComment]['comment_closer'];
200+
$phpcsFile->fixer->addContent($beginning, "\n\n$use\n");
201+
} else {
202+
$phpcsFile->fixer->addContent(0, "\n\n$use\n");
203+
}
198204
}
199205
}
200206
}//end if
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Namespace2;
4+
5+
/**
6+
* Class C's description.
7+
*/
8+
class C extends \Namespace1\D {
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Namespace2;
4+
5+
use Namespace1\D;
6+
7+
/**
8+
* Class C's description.
9+
*/
10+
class C extends D {
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Namespace2;
6+
7+
/**
8+
* Class C's description.
9+
*/
10+
class C extends \Namespace1\D implements \Namespace1\I {
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Namespace2;
6+
7+
use Namespace1\I;
8+
use Namespace1\D;
9+
10+
/**
11+
* Class C's description.
12+
*/
13+
class C extends D implements I {
14+
}

tests/Drupal/Classes/FullyQualifiedNamespaceUnitTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ protected function getErrorList(string $testFile): array
3333
];
3434
case 'FullyQualifiedNamespaceUnitTest.1.inc':
3535
return [16 => 1];
36+
case 'FullyQualifiedNamespaceUnitTest.2.inc':
37+
return [8 => 1];
38+
case 'FullyQualifiedNamespaceUnitTest.3.inc':
39+
return [10 => 2];
3640
}
3741

3842
return [];
@@ -69,6 +73,8 @@ protected function getTestFiles($testFileBase): array
6973
return [
7074
__DIR__.'/FullyQualifiedNamespaceUnitTest.inc',
7175
__DIR__.'/FullyQualifiedNamespaceUnitTest.1.inc',
76+
__DIR__.'/FullyQualifiedNamespaceUnitTest.2.inc',
77+
__DIR__.'/FullyQualifiedNamespaceUnitTest.3.inc',
7278
__DIR__.'/FullyQualifiedNamespaceUnitTest.api.php',
7379
];
7480

0 commit comments

Comments
 (0)