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

keradus / PHP-CS-Fixer / 17319949156

29 Aug 2025 09:20AM UTC coverage: 94.696% (-0.05%) from 94.744%
17319949156

push

github

keradus
CS

28333 of 29920 relevant lines covered (94.7%)

45.63 hits per line

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

81.25
/src/Tokenizer/Analyzer/NamespacesAnalyzer.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
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
25
 */
26
final class NamespacesAnalyzer
27
{
28
    /**
29
     * @return list<NamespaceAnalysis>
30
     */
31
    public function getDeclarations(Tokens $tokens): array
32
    {
33
        $namespaces = [];
9✔
34

35
        for ($index = 1, $count = \count($tokens); $index < $count; ++$index) {
9✔
36
            $token = $tokens[$index];
8✔
37

38
            if (!$token->isGivenKind(\T_NAMESPACE)) {
8✔
39
                continue;
6✔
40
            }
41

42
            $declarationEndIndex = $tokens->getNextTokenOfKind($index, [';', '{']);
5✔
43
            $namespace = trim($tokens->generatePartialCode($index + 1, $declarationEndIndex - 1));
5✔
44
            $declarationParts = explode('\\', $namespace);
5✔
45
            $shortName = end($declarationParts);
5✔
46

47
            if ($tokens[$declarationEndIndex]->equals('{')) {
5✔
48
                $scopeEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $declarationEndIndex);
3✔
49
            } else {
50
                $scopeEndIndex = $tokens->getNextTokenOfKind($declarationEndIndex, [[\T_NAMESPACE]]);
2✔
51
                if (null === $scopeEndIndex) {
2✔
52
                    $scopeEndIndex = \count($tokens);
2✔
53
                }
54
                --$scopeEndIndex;
2✔
55
            }
56

57
            $namespaces[] = new NamespaceAnalysis(
5✔
58
                $namespace,
5✔
59
                $shortName,
5✔
60
                $index,
5✔
61
                $declarationEndIndex,
5✔
62
                $index,
5✔
63
                $scopeEndIndex
5✔
64
            );
5✔
65

66
            // Continue the analysis after the end of this namespace to find the next one
67
            $index = $scopeEndIndex;
5✔
68
        }
69

70
        if (0 === \count($namespaces) && $tokens->isTokenKindFound(\T_OPEN_TAG)) {
9✔
71
            $namespaces[] = new NamespaceAnalysis(
3✔
72
                '',
3✔
73
                '',
3✔
74
                $openTagIndex = $tokens[0]->isGivenKind(\T_INLINE_HTML) ? 1 : 0,
3✔
75
                $openTagIndex,
3✔
76
                $openTagIndex,
3✔
77
                \count($tokens) - 1,
3✔
78
            );
3✔
79
        }
80

81
        return $namespaces;
9✔
82
    }
83

84
    public function getNamespaceAt(Tokens $tokens, int $index): NamespaceAnalysis
85
    {
86
        if (!$tokens->offsetExists($index)) {
5✔
87
            throw new \InvalidArgumentException(\sprintf('Token index %d does not exist.', $index));
1✔
88
        }
89

90
        foreach ($this->getDeclarations($tokens) as $namespace) {
4✔
91
            if ($namespace->getScopeStartIndex() <= $index && $namespace->getScopeEndIndex() >= $index) {
4✔
92
                return $namespace;
4✔
93
            }
94
        }
95

96
        throw new \LogicException(\sprintf('Unable to get the namespace at index %d.', $index));
×
97
    }
98

99
    /**
100
     * @return array{NamespaceAnalysis, array<string, NamespaceUseAnalysis>}
101
     */
102
    public static function collectNamespaceAnalysis(Tokens $tokens, int $startIndex): array
103
    {
104
        $namespaceAnalysis = (new self())->getNamespaceAt($tokens, $startIndex);
×
105
        $namespaceUseAnalyses = (new NamespaceUsesAnalyzer())->getDeclarationsInNamespace($tokens, $namespaceAnalysis);
×
106

107
        $uses = [];
×
108
        foreach ($namespaceUseAnalyses as $use) {
×
109
            if (!$use->isClass()) {
×
110
                continue;
×
111
            }
112

113
            $uses[$use->getShortName()] = $use;
×
114
        }
115

116
        return [$namespaceAnalysis, $uses];
×
117
    }
118
}
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