Skip to content

Commit e0fc88f

Browse files
committed
fix(FullyQualifiedNamespace): Fix fixer for new use statement group
1 parent 59f52b8 commit e0fc88f

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-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+
}

tests/Drupal/Classes/FullyQualifiedNamespaceUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ 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];
3638
}
3739

3840
return [];
@@ -69,6 +71,7 @@ protected function getTestFiles($testFileBase): array
6971
return [
7072
__DIR__.'/FullyQualifiedNamespaceUnitTest.inc',
7173
__DIR__.'/FullyQualifiedNamespaceUnitTest.1.inc',
74+
__DIR__.'/FullyQualifiedNamespaceUnitTest.2.inc',
7275
__DIR__.'/FullyQualifiedNamespaceUnitTest.api.php',
7376
];
7477

0 commit comments

Comments
 (0)