• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHPCSExtra / 15639493787

13 Jun 2025 04:35PM UTC coverage: 97.39% (-2.5%) from 99.85%
15639493787

Pull #367

github

web-flow
Merge 844746ea8 into 10343591c
Pull Request #367: Update for compatibility with PHPCS 4.0

55 of 140 new or added lines in 8 files covered. (39.29%)

1 existing line in 1 file now uncovered.

3358 of 3448 relevant lines covered (97.39%)

3.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.05
/Universal/Sniffs/UseStatements/NoUselessAliasesSniff.php
1
<?php
2
/**
3
 * PHPCSExtra, a collection of sniffs and standards for use with PHP_CodeSniffer.
4
 *
5
 * @package   PHPCSExtra
6
 * @copyright 2023 PHPCSExtra Contributors
7
 * @license   https://opensource.org/licenses/LGPL-3.0 LGPL3
8
 * @link      https://github.com/PHPCSStandards/PHPCSExtra
9
 */
10

11
namespace PHPCSExtra\Universal\Sniffs\UseStatements;
12

13
use PHP_CodeSniffer\Files\File;
14
use PHP_CodeSniffer\Sniffs\Sniff;
15
use PHP_CodeSniffer\Util\Tokens;
16
use PHPCSUtils\Tokens\Collections;
17
use PHPCSUtils\Utils\NamingConventions;
18
use PHPCSUtils\Utils\UseStatements;
19

20
/**
21
 * Detects useless aliases for import use statements.
22
 *
23
 * Aliasing something to the same name as the original construct is considered useless.
24
 * Note: as OO and function names in PHP are case-insensitive, aliasing to the same name,
25
 * using a different case is also considered useless.
26
 *
27
 * @since 1.1.0
28
 */
29
final class NoUselessAliasesSniff implements Sniff
30
{
31

32
    /**
33
     * Returns an array of tokens this test wants to listen for.
34
     *
35
     * @since 1.1.0
36
     *
37
     * @return array<int|string>
38
     */
39
    public function register()
4✔
40
    {
41
        return [\T_USE];
4✔
42
    }
43

44
    /**
45
     * Processes this test, when one of its tokens is encountered.
46
     *
47
     * @since 1.1.0
48
     *
49
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
50
     * @param int                         $stackPtr  The position of the current token
51
     *                                               in the stack passed in $tokens.
52
     *
53
     * @return void
54
     */
55
    public function process(File $phpcsFile, $stackPtr)
4✔
56
    {
57
        if (UseStatements::isImportUse($phpcsFile, $stackPtr) === false) {
4✔
58
            // Closure or trait use statement. Bow out.
59
            return;
4✔
60
        }
61

62
        $endOfStatement = $phpcsFile->findNext([\T_SEMICOLON, \T_CLOSE_TAG], ($stackPtr + 1));
4✔
63
        if ($endOfStatement === false) {
4✔
64
            // Parse error or live coding.
65
            return;
4✔
66
        }
67

68
        $hasAliases = $phpcsFile->findNext(\T_AS, ($stackPtr + 1), $endOfStatement);
4✔
69
        if ($hasAliases === false) {
4✔
70
            // This use import statement does not alias anything, bow out.
71
            return;
4✔
72
        }
73

74
        $useStatements = UseStatements::splitImportUseStatement($phpcsFile, $stackPtr);
4✔
75
        if (\count($useStatements, \COUNT_RECURSIVE) <= 3) {
4✔
76
            // No statements found. Shouldn't be possible, but still. Bow out.
77
            return;
4✔
78
        }
79

80
        $tokens = $phpcsFile->getTokens();
4✔
81

82
        // Collect all places where aliases are used in this use statement.
83
        $aliasPtrs = [];
4✔
84
        $currentAs = $hasAliases;
4✔
85
        do {
86
            $aliasPtr = $phpcsFile->findNext(Tokens::$emptyTokens, ($currentAs + 1), null, true);
4✔
87
            if ($aliasPtr !== false && $tokens[$aliasPtr]['code'] === \T_STRING) {
4✔
88
                $aliasPtrs[$currentAs] = $aliasPtr;
4✔
89
            }
2✔
90

91
            $currentAs = $phpcsFile->findNext(\T_AS, ($currentAs + 1), $endOfStatement);
4✔
92
        } while ($currentAs !== false);
4✔
93

94
        // Now check the names in each use statement for useless aliases.
95
        foreach ($useStatements as $type => $statements) {
4✔
96
            foreach ($statements as $alias => $qualifiedName) {
4✔
97
                $unqualifiedName = \ltrim(\substr($qualifiedName, (int) \strrpos($qualifiedName, '\\')), '\\');
4✔
98

99
                $uselessAlias = false;
4✔
100
                if ($type === 'const') {
4✔
101
                    // Do a case-sensitive comparison for constants.
102
                    if ($unqualifiedName === $alias) {
4✔
103
                        $uselessAlias = true;
4✔
104
                    }
2✔
105
                } elseif (NamingConventions::isEqual($unqualifiedName, $alias)) {
4✔
106
                    $uselessAlias = true;
4✔
107
                }
2✔
108

109
                if ($uselessAlias === false) {
4✔
110
                    continue;
4✔
111
                }
112

113
                // Now check if this is actually used as an alias or just the actual name.
114
                foreach ($aliasPtrs as $asPtr => $aliasPtr) {
4✔
115
                    if ($tokens[$aliasPtr]['content'] !== $alias) {
4✔
116
                        continue;
4✔
117
                    }
118

119
                    // Make sure this is really the right one.
120
                    $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($asPtr - 1), null, true);
4✔
121
                    if (isset(Collections::nameTokens()[$tokens[$prev]['code']]) === false) {
4✔
NEW
122
                        continue;
×
123
                    } elseif ($tokens[$prev]['code'] === \T_STRING
4✔
124
                        && $tokens[$prev]['content'] !== $unqualifiedName
4✔
125
                    ) {
2✔
126
                        continue;
4✔
127
                    } elseif ($tokens[$prev]['code'] === \T_NAME_QUALIFIED
4✔
128
                        && $tokens[$prev]['content'] !== $qualifiedName
4✔
129
                        && \substr($qualifiedName, -(\strlen($tokens[$prev]['content']))) !== $tokens[$prev]['content']
4✔
130
                    ) {
2✔
NEW
131
                        continue;
×
132
                    } elseif ($tokens[$prev]['code'] === \T_NAME_FULLY_QUALIFIED
4✔
133
                        && $tokens[$prev]['content'] !== '\\' . $qualifiedName
4✔
134
                        && \substr($qualifiedName, -(\strlen($tokens[$prev]['content']))) !== $tokens[$prev]['content']
4✔
135
                    ) {
2✔
UNCOV
136
                        continue;
×
137
                    }
138

139
                    $error = 'Useless alias "%s" found for import of "%s"';
4✔
140
                    $code  = 'Found';
4✔
141
                    $data  = [$alias, $qualifiedName];
4✔
142

143
                    // Okay, so this is the one which should be flagged.
144
                    $hasComments = $phpcsFile->findNext(Tokens::$commentTokens, ($prev + 1), $aliasPtr);
4✔
145
                    if ($hasComments !== false) {
4✔
146
                        // Don't auto-fix if there are comments.
147
                        $phpcsFile->addError($error, $aliasPtr, $code, $data);
4✔
148
                        break;
4✔
149
                    }
150

151
                    $fix = $phpcsFile->addFixableError($error, $aliasPtr, $code, $data);
4✔
152

153
                    if ($fix === true) {
4✔
154
                        $phpcsFile->fixer->beginChangeset();
4✔
155

156
                        for ($i = ($prev + 1); $i <= $aliasPtr; $i++) {
4✔
157
                            $phpcsFile->fixer->replaceToken($i, '');
4✔
158
                        }
2✔
159

160
                        $phpcsFile->fixer->endChangeset();
4✔
161
                    }
2✔
162

163
                    break;
4✔
164
                }
2✔
165
            }
2✔
166
        }
2✔
167
    }
2✔
168
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc