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

PHP-CS-Fixer / PHP-CS-Fixer / 13655247317

04 Mar 2025 01:53PM UTC coverage: 94.924% (-0.01%) from 94.938%
13655247317

push

github

web-flow
refactor: add `FullyQualifiedNameAnalyzer` (#8048)

35 of 38 new or added lines in 4 files covered. (92.11%)

3 existing lines in 1 file now uncovered.

28090 of 29592 relevant lines covered (94.92%)

43.06 hits per line

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

96.15
/src/Tokenizer/Analyzer/FullyQualifiedNameAnalyzer.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\Tokenizer\Analyzer;
16

17
use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceAnalysis;
18
use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceUseAnalysis;
19
use PhpCsFixer\Tokenizer\Tokens;
20

21
/**
22
 * @internal
23
 */
24
final class FullyQualifiedNameAnalyzer
25
{
26
    private Tokens $tokens;
27

28
    /**
29
     * @var list<NamespaceAnalysis>
30
     */
31
    private array $namespaceAnalyses = [];
32

33
    /**
34
     * @var array<int, list<NamespaceUseAnalysis>>
35
     */
36
    private array $namespaceUseAnalyses = [];
37

38
    public function __construct(Tokens $tokens)
39
    {
40
        $this->tokens = $tokens;
21✔
41
    }
42

43
    /**
44
     * @param NamespaceUseAnalysis::TYPE_* $importType
45
     */
46
    public function getFullyQualifiedName(string $name, int $indexInNamespace, int $importType): string
47
    {
48
        return ltrim($this->getFullyQualifiedNameWithPossiblyLeadingSlash($name, $indexInNamespace, $importType), '\\');
21✔
49
    }
50

51
    /**
52
     * @param NamespaceUseAnalysis::TYPE_* $importType
53
     */
54
    private function getFullyQualifiedNameWithPossiblyLeadingSlash(string $name, int $indexInNamespace, int $importType): string
55
    {
56
        if ('\\' === $name[0]) {
21✔
57
            return $name;
1✔
58
        }
59

60
        $namespaceAnalysis = $this->getNamespaceAnalysis($indexInNamespace);
20✔
61

62
        $this->namespaceUseAnalyses[$namespaceAnalysis->getStartIndex()] ??= (new NamespaceUsesAnalyzer())->getDeclarationsInNamespace($this->tokens, $namespaceAnalysis);
20✔
63
        \assert(isset($this->namespaceUseAnalyses[$namespaceAnalysis->getStartIndex()]));
20✔
64

65
        $declarations = [];
20✔
66
        foreach ($this->namespaceUseAnalyses[$namespaceAnalysis->getStartIndex()] as $namespaceUseAnalysis) {
20✔
67
            if ($namespaceUseAnalysis->getType() !== $importType) {
15✔
68
                continue;
3✔
69
            }
70
            $declarations[strtolower($namespaceUseAnalysis->getShortName())] = $namespaceUseAnalysis->getFullName();
15✔
71
        }
72

73
        $lowercaseName = strtolower($name);
20✔
74
        foreach ($declarations as $lowercaseShortName => $fullName) {
20✔
75
            if ($lowercaseName === $lowercaseShortName) {
15✔
76
                return $fullName;
11✔
77
            }
78

79
            if (!str_starts_with($lowercaseName, $lowercaseShortName.'\\')) {
5✔
80
                continue;
1✔
81
            }
82

83
            return $fullName.substr($name, \strlen($lowercaseShortName));
4✔
84
        }
85

86
        return $namespaceAnalysis->getFullName().'\\'.$name;
5✔
87
    }
88

89
    private function getNamespaceAnalysis(int $index): NamespaceAnalysis
90
    {
91
        foreach ($this->namespaceAnalyses as $namespace) {
20✔
92
            if ($namespace->getScopeStartIndex() <= $index && $namespace->getScopeEndIndex() >= $index) {
1✔
NEW
93
                return $namespace;
×
94
            }
95
        }
96

97
        $namespace = (new NamespacesAnalyzer())->getNamespaceAt($this->tokens, $index);
20✔
98

99
        $this->namespaceAnalyses[] = $namespace;
20✔
100

101
        return $namespace;
20✔
102
    }
103
}
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