Skip to content

Commit b2252f7

Browse files
committed
Universal/NoUselessAliases: update for PHPCS 4.0
The tokenization of (namespaced) "names" has changed in PHP 8.0 and this new tokenization will come into effect for PHP_CodeSniffer as of version 4.0.0. This commit adds handling for this new tokenization to this sniff. Includes some additional tests.
1 parent d4762a8 commit b2252f7

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

Universal/Sniffs/UseStatements/NoUselessAliasesSniff.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHP_CodeSniffer\Files\File;
1414
use PHP_CodeSniffer\Sniffs\Sniff;
1515
use PHP_CodeSniffer\Util\Tokens;
16+
use PHPCSUtils\Tokens\Collections;
1617
use PHPCSUtils\Utils\NamingConventions;
1718
use PHPCSUtils\Utils\UseStatements;
1819

@@ -117,8 +118,21 @@ public function process(File $phpcsFile, $stackPtr)
117118

118119
// Make sure this is really the right one.
119120
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($asPtr - 1), null, true);
120-
if ($tokens[$prev]['code'] !== \T_STRING
121-
|| $tokens[$prev]['content'] !== $unqualifiedName
121+
if (isset(Collections::nameTokens()[$tokens[$prev]['code']]) === false) {
122+
// Shouldn't be possible.
123+
continue; // @codeCoverageIgnore
124+
} elseif ($tokens[$prev]['code'] === \T_STRING
125+
&& $tokens[$prev]['content'] !== $unqualifiedName
126+
) {
127+
continue;
128+
} elseif ($tokens[$prev]['code'] === \T_NAME_QUALIFIED
129+
&& $tokens[$prev]['content'] !== $qualifiedName
130+
&& \substr($qualifiedName, -(\strlen($tokens[$prev]['content']))) !== $tokens[$prev]['content']
131+
) {
132+
continue;
133+
} elseif ($tokens[$prev]['code'] === \T_NAME_FULLY_QUALIFIED
134+
&& $tokens[$prev]['content'] !== '\\' . $qualifiedName
135+
&& \substr($qualifiedName, -(\strlen($tokens[$prev]['content']))) !== $tokens[$prev]['content']
122136
) {
123137
continue;
124138
}

Universal/Tests/UseStatements/NoUselessAliasesUnitTest.inc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use Some\NS\ {
6464

6565
// Verify handling of non-ascii names.
6666
use Vendor\Package\Déjàvü as Dejavu; // OK.
67-
use Vendor\Package\Déjàvü as DÉJÀVÜ; // Ok.
67+
use Vendor\Package\Déjàvü as DÉJÀVÜ; // OK.
6868
use Vendor\Package\Déjàvü as Déjàvü; // Error.
6969
use Vendor\Package\Déjàvü as déJàVü; // Error.
7070

@@ -75,6 +75,23 @@ use function 💩💩 as 💩💩; // Error.
7575
use function foo\math\sin as Cos, // OK.
7676
foo\math\cos as Cos; // Error.
7777

78+
// Duplicate aliases, but that's not our concern - this test is about flagging the correct alias pointer.
79+
use function foo,
80+
bar as baz,
81+
baz as baz; // Error.
82+
83+
use function partial\foo,
84+
partial\bar as baz,
85+
partial\baz as baz; // Error.
86+
87+
use function \foo,
88+
\bar as baz,
89+
\baz as baz; // Error.
90+
91+
use function \full\foo,
92+
\full\bar as baz,
93+
\full\baz as baz; // Error.
94+
7895
// Intentional parse error.
7996
use function as ;
8097

Universal/Tests/UseStatements/NoUselessAliasesUnitTest.inc.fixed

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use Some\NS\ {
6262

6363
// Verify handling of non-ascii names.
6464
use Vendor\Package\Déjàvü as Dejavu; // OK.
65-
use Vendor\Package\Déjàvü as DÉJÀVÜ; // Ok.
65+
use Vendor\Package\Déjàvü as DÉJÀVÜ; // OK.
6666
use Vendor\Package\Déjàvü; // Error.
6767
use Vendor\Package\Déjàvü; // Error.
6868

@@ -73,6 +73,23 @@ use function 💩💩; // Error.
7373
use function foo\math\sin as Cos, // OK.
7474
foo\math\cos; // Error.
7575

76+
// Duplicate aliases, but that's not our concern - this test is about flagging the correct alias pointer.
77+
use function foo,
78+
bar as baz,
79+
baz; // Error.
80+
81+
use function partial\foo,
82+
partial\bar as baz,
83+
partial\baz; // Error.
84+
85+
use function \foo,
86+
\bar as baz,
87+
\baz; // Error.
88+
89+
use function \full\foo,
90+
\full\bar as baz,
91+
\full\baz; // Error.
92+
7693
// Intentional parse error.
7794
use function as ;
7895

Universal/Tests/UseStatements/NoUselessAliasesUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function getErrorList()
4848
69 => 1,
4949
72 => 1,
5050
76 => 1,
51+
81 => 1,
52+
85 => 1,
53+
89 => 1,
54+
93 => 1,
5155
];
5256
}
5357

0 commit comments

Comments
 (0)