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

keradus / PHP-CS-Fixer / 15274850899

27 May 2025 11:01AM UTC coverage: 94.849% (-0.02%) from 94.87%
15274850899

push

github

web-flow
DX: introduce `FCT` class for tokens not present in the lowest supported PHP version (#8706)

Co-authored-by: Dariusz Rumiński <dariusz.ruminski@gmail.com>

186 of 192 new or added lines in 52 files covered. (96.88%)

72 existing lines in 9 files now uncovered.

28099 of 29625 relevant lines covered (94.85%)

45.33 hits per line

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

88.0
/src/Fixer/Import/NoUnneededImportAliasFixer.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer\Fixer\Import;
16

17
use PhpCsFixer\AbstractFixer;
18
use PhpCsFixer\FixerDefinition\CodeSample;
19
use PhpCsFixer\FixerDefinition\FixerDefinition;
20
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
21
use PhpCsFixer\Tokenizer\CT;
22
use PhpCsFixer\Tokenizer\Tokens;
23

24
final class NoUnneededImportAliasFixer extends AbstractFixer
25
{
26
    public function getDefinition(): FixerDefinitionInterface
27
    {
28
        return new FixerDefinition(
3✔
29
            'Imports should not be aliased as the same name.',
3✔
30
            [new CodeSample("<?php\nuse A\\B\\Foo as Foo;\n")]
3✔
31
        );
3✔
32
    }
33

34
    public function isCandidate(Tokens $tokens): bool
35
    {
36
        return $tokens->isAllTokenKindsFound([T_USE, T_AS]);
9✔
37
    }
38

39
    /**
40
     * {@inheritdoc}
41
     *
42
     * Must run before NoSinglelineWhitespaceBeforeSemicolonsFixer.
43
     * Must run after PhpUnitNamespacedFixer.
44
     */
45
    public function getPriority(): int
46
    {
47
        return 1;
1✔
48
    }
49

50
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
51
    {
52
        for ($index = \count($tokens) - 1; 0 <= $index; --$index) {
8✔
53
            if (!$tokens[$index]->isGivenKind(T_AS)) {
8✔
54
                continue;
8✔
55
            }
56

57
            $aliasIndex = $tokens->getNextMeaningfulToken($index);
8✔
58

59
            if (!$tokens[$aliasIndex]->isGivenKind(T_STRING)) {
8✔
UNCOV
60
                continue;
×
61
            }
62

63
            $importIndex = $tokens->getPrevMeaningfulToken($index);
8✔
64

65
            if (!$tokens[$importIndex]->isGivenKind(T_STRING)) {
8✔
UNCOV
66
                continue;
×
67
            }
68

69
            if ($tokens[$importIndex]->getContent() !== $tokens[$aliasIndex]->getContent()) {
8✔
70
                continue;
2✔
71
            }
72

73
            do {
74
                $importIndex = $tokens->getPrevMeaningfulToken($importIndex);
8✔
75
            } while ($tokens[$importIndex]->isGivenKind([T_NS_SEPARATOR, T_STRING, T_AS]) || $tokens[$importIndex]->equals(','));
8✔
76

77
            if ($tokens[$importIndex]->isGivenKind([CT::T_FUNCTION_IMPORT, CT::T_CONST_IMPORT])) {
8✔
78
                $importIndex = $tokens->getPrevMeaningfulToken($importIndex);
4✔
79
            }
80

81
            if (!$tokens[$importIndex]->isGivenKind([T_USE, CT::T_GROUP_IMPORT_BRACE_OPEN])) {
8✔
UNCOV
82
                continue;
×
83
            }
84

85
            $tokens->clearTokenAndMergeSurroundingWhitespace($aliasIndex);
8✔
86
            $tokens->clearTokenAndMergeSurroundingWhitespace($index);
8✔
87
        }
88
    }
89
}
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

© 2026 Coveralls, Inc