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

keradus / PHP-CS-Fixer / 17678835382

12 Sep 2025 03:24PM UTC coverage: 94.69% (-0.06%) from 94.75%
17678835382

push

github

keradus
fix typo

1 of 1 new or added line in 1 file covered. (100.0%)

1042 existing lines in 177 files now uncovered.

28424 of 30018 relevant lines covered (94.69%)

45.5 hits per line

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

31.43
/src/Fixer/NamespaceNotation/CleanNamespaceFixer.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\NamespaceNotation;
16

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

24
/**
25
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
26
 */
27
final class CleanNamespaceFixer extends AbstractFixer
28
{
29
    public function getDefinition(): FixerDefinitionInterface
30
    {
31
        $samples = [];
3✔
32

33
        foreach (['namespace Foo \ Bar;', 'echo foo /* comment */ \ bar();'] as $sample) {
3✔
34
            $samples[] = new VersionSpecificCodeSample(
3✔
35
                "<?php\n".$sample."\n",
3✔
36
                new VersionSpecification(null, 8_00_00 - 1)
3✔
37
            );
3✔
38
        }
39

40
        return new FixerDefinition(
3✔
41
            'Namespace must not contain spacing, comments or PHPDoc.',
3✔
42
            $samples
3✔
43
        );
3✔
44
    }
45

46
    public function isCandidate(Tokens $tokens): bool
47
    {
UNCOV
48
        return \PHP_VERSION_ID < 8_00_00 && $tokens->isTokenKindFound(\T_NS_SEPARATOR);
×
49
    }
50

51
    /**
52
     * {@inheritdoc}
53
     *
54
     * Must run before PhpUnitDataProviderReturnTypeFixer.
55
     */
56
    public function getPriority(): int
57
    {
58
        return 10;
1✔
59
    }
60

61
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
62
    {
63
        $count = $tokens->count();
×
64

UNCOV
65
        for ($index = 0; $index < $count; ++$index) {
×
66
            if ($tokens[$index]->isGivenKind(\T_NS_SEPARATOR)) {
×
67
                $previousIndex = $tokens->getPrevMeaningfulToken($index);
×
68

69
                $index = $this->fixNamespace(
×
UNCOV
70
                    $tokens,
×
UNCOV
71
                    $tokens[$previousIndex]->isGivenKind(\T_STRING) ? $previousIndex : $index
×
UNCOV
72
                );
×
73
            }
74
        }
75
    }
76

77
    /**
78
     * @param int $index start of namespace
79
     */
80
    private function fixNamespace(Tokens $tokens, int $index): int
81
    {
82
        $tillIndex = $index;
×
83

84
        // go to the end of the namespace
UNCOV
85
        while ($tokens[$tillIndex]->isGivenKind([\T_NS_SEPARATOR, \T_STRING])) {
×
86
            $tillIndex = $tokens->getNextMeaningfulToken($tillIndex);
×
87
        }
88

UNCOV
89
        $tillIndex = $tokens->getPrevMeaningfulToken($tillIndex);
×
90

91
        $spaceIndices = [];
×
92

93
        for (; $index <= $tillIndex; ++$index) {
×
94
            if ($tokens[$index]->isGivenKind(\T_WHITESPACE)) {
×
UNCOV
95
                $spaceIndices[] = $index;
×
UNCOV
96
            } elseif ($tokens[$index]->isComment()) {
×
UNCOV
97
                $tokens->clearAt($index);
×
98
            }
99
        }
100

UNCOV
101
        if ($tokens[$index - 1]->isWhitespace()) {
×
102
            array_pop($spaceIndices);
×
103
        }
104

UNCOV
105
        foreach ($spaceIndices as $i) {
×
106
            $tokens->clearAt($i);
×
107
        }
108

UNCOV
109
        return $index;
×
110
    }
111
}
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