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

PHP-CS-Fixer / PHP-CS-Fixer / 3721300657

pending completion
3721300657

push

github

GitHub
minor: Follow PSR12 ordered imports in Symfony ruleset (#6712)

9 of 9 new or added lines in 2 files covered. (100.0%)

22674 of 24281 relevant lines covered (93.38%)

39.08 hits per line

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

29.41
/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\AbstractLinesBeforeNamespaceFixer;
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 AbstractLinesBeforeNamespaceFixer
25
{
26
    /**
27
     * {@inheritdoc}
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, 80000 - 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
    /**
47
     * {@inheritdoc}
48
     */
49
    public function isCandidate(Tokens $tokens): bool
50
    {
51
        return \PHP_VERSION_ID < 80000 && $tokens->isTokenKindFound(T_NS_SEPARATOR);
×
52
    }
53

54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
58
    {
59
        $count = $tokens->count();
×
60

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

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

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

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

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

87
        $spaceIndices = [];
×
88

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

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

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

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

© 2025 Coveralls, Inc