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

UseStatements::splitImportUseStatement(): improve handling of leading backslash #590

Merged
Merged
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
4 changes: 2 additions & 2 deletions PHPCSUtils/Utils/UseStatements.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ public static function splitImportUseStatement(File $phpcsFile, $stackPtr)
case \T_COMMA:
if ($name !== '') {
if ($useGroup === true) {
$statements[$type][$alias] = $baseName . $name;
$statements[$type][$alias] = \ltrim($baseName, '\\') . $name;
} else {
$statements[$type][$alias] = $name;
$statements[$type][$alias] = \ltrim($name, '\\');
}
}

Expand Down
19 changes: 14 additions & 5 deletions Tests/Utils/UseStatements/SplitImportUseStatementTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use MyNamespace\MyClass;
/* testUsePlainAliased */
use MyNamespace \ YourClass as ClassAlias;

/* testUsePlainLeadingBackslash */
use \MyNamespace\TheirClass;

/* testUseMultipleWithComments */
use Vendor\Foo\ClassA as ClassABC,
Vendor \ /*comment*/ Bar \ /*another comment */ InterfaceB,
Expand All @@ -38,7 +41,7 @@ use CONST MyNamespace\MY_CONST;
use const MyNamespace\YOUR_CONST as CONST_ALIAS;

/* testUseConstMultiple */
use const foo\math\PI, foo\math\GOLDEN_RATIO as MATH_GOLDEN;
use const foo\math\PI, \foo\math\GOLDEN_RATIO as MATH_GOLDEN;

/* testGroupUse */
use some\namespacing\{
Expand All @@ -47,6 +50,12 @@ use some\namespacing\{
another\level\SomeClassC as C
};

/* testGroupUseLeadingBackslash */
use \world\namespacing\{
SomeClassA,
deeper\level\SomeClassB
};

/* testGroupUseFunctionTrailingComma */
use function bar\math\{
Msin,
Expand All @@ -70,15 +79,15 @@ use Some\NS\ {
};

/* testUsePlainReservedKeyword */
// Intentional parse error - use of reserved keyword in namespace.
// Intentional parse error for PHP < 8.0 - use of reserved keyword in namespace.
use Vendor\break\ClassName;

/* testUseFunctionPlainReservedKeyword */
// Intentional parse error - use of reserved keyword in namespace.
// Intentional parse error for PHP < 8.0 - use of reserved keyword in namespace.
use function Vendor\YourNamespace\switch\yourFunction;

/* testUseConstPlainReservedKeyword */
// Intentional parse error - use of reserved keyword in namespace.
// Intentional parse error for PHP < 8.0 - use of reserved keyword in namespace.
use const Vendor\YourNamespace\function\yourConst;

/* testUsePlainAliasReservedKeyword */
Expand All @@ -88,7 +97,7 @@ use Vendor\YourNamespace\ClassName as class;
/* testUsePlainAliasReservedKeywordFunction */
// Intentional parse error - use of reserved keyword as alias.
use Vendor\{
YourNamespace\ClassName as function
YourNamespace\ClassName as function
};

/* testUsePlainAliasReservedKeywordConst */
Expand Down
20 changes: 20 additions & 0 deletions Tests/Utils/UseStatements/SplitImportUseStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ public static function dataSplitImportUseStatement()
'const' => [],
],
],
'plain-with-leading-backslash' => [
'testMarker' => '/* testUsePlainLeadingBackslash */',
'expected' => [
'name' => ['TheirClass' => 'MyNamespace\TheirClass'],
'function' => [],
'const' => [],
],
],

'multiple-with-comments' => [
'testMarker' => '/* testUseMultipleWithComments */',
'expected' => [
Expand Down Expand Up @@ -203,6 +212,17 @@ public static function dataSplitImportUseStatement()
'const' => [],
],
],
'group-with-leading-backslash' => [
'testMarker' => '/* testGroupUseLeadingBackslash */',
'expected' => [
'name' => [
'SomeClassA' => 'world\namespacing\SomeClassA',
'SomeClassB' => 'world\namespacing\deeper\level\SomeClassB',
],
'function' => [],
'const' => [],
],
],
'group-function-trailing-comma' => [
'testMarker' => '/* testGroupUseFunctionTrailingComma */',
'expected' => [
Expand Down