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

keradus / PHP-CS-Fixer / 16241811443

12 Jul 2025 08:36PM UTC coverage: 94.806%. First build
16241811443

push

github

keradus
apply CS

2255 of 2330 new or added lines in 338 files covered. (96.78%)

28257 of 29805 relevant lines covered (94.81%)

45.4 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
final class CleanNamespaceFixer extends AbstractFixer
25
{
26
    public function getDefinition(): FixerDefinitionInterface
27
    {
28
        $samples = [];
3✔
29

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

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

43
    public function isCandidate(Tokens $tokens): bool
44
    {
NEW
45
        return \PHP_VERSION_ID < 8_00_00 && $tokens->isTokenKindFound(\T_NS_SEPARATOR);
×
46
    }
47

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

58
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
59
    {
60
        $count = $tokens->count();
×
61

62
        for ($index = 0; $index < $count; ++$index) {
×
NEW
63
            if ($tokens[$index]->isGivenKind(\T_NS_SEPARATOR)) {
×
64
                $previousIndex = $tokens->getPrevMeaningfulToken($index);
×
65

66
                $index = $this->fixNamespace(
×
67
                    $tokens,
×
NEW
68
                    $tokens[$previousIndex]->isGivenKind(\T_STRING) ? $previousIndex : $index
×
69
                );
×
70
            }
71
        }
72
    }
73

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

81
        // go to the end of the namespace
NEW
82
        while ($tokens[$tillIndex]->isGivenKind([\T_NS_SEPARATOR, \T_STRING])) {
×
83
            $tillIndex = $tokens->getNextMeaningfulToken($tillIndex);
×
84
        }
85

86
        $tillIndex = $tokens->getPrevMeaningfulToken($tillIndex);
×
87

88
        $spaceIndices = [];
×
89

90
        for (; $index <= $tillIndex; ++$index) {
×
NEW
91
            if ($tokens[$index]->isGivenKind(\T_WHITESPACE)) {
×
92
                $spaceIndices[] = $index;
×
93
            } elseif ($tokens[$index]->isComment()) {
×
94
                $tokens->clearAt($index);
×
95
            }
96
        }
97

98
        if ($tokens[$index - 1]->isWhitespace()) {
×
99
            array_pop($spaceIndices);
×
100
        }
101

102
        foreach ($spaceIndices as $i) {
×
103
            $tokens->clearAt($i);
×
104
        }
105

106
        return $index;
×
107
    }
108
}
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