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

keradus / PHP-CS-Fixer / 24023665044

02 Apr 2026 09:33PM UTC coverage: 93.056% (+0.1%) from 92.938%
24023665044

push

github

web-flow
chore: add tests for `BracesPositionFixer` (#9522)

29548 of 31753 relevant lines covered (93.06%)

43.98 hits per line

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

98.41
/src/Fixer/Basic/NoTrailingCommaInSinglelineFixer.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\Basic;
16

17
use PhpCsFixer\AbstractFixer;
18
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
19
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
20
use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
21
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
22
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
23
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
24
use PhpCsFixer\FixerDefinition\CodeSample;
25
use PhpCsFixer\FixerDefinition\FixerDefinition;
26
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
27
use PhpCsFixer\Tokenizer\Analyzer\AttributeAnalyzer;
28
use PhpCsFixer\Tokenizer\CT;
29
use PhpCsFixer\Tokenizer\Tokens;
30

31
/**
32
 * @phpstan-type _AutogeneratedInputConfiguration array{
33
 *  elements?: list<'arguments'|'array'|'array_destructuring'|'group_import'>,
34
 * }
35
 * @phpstan-type _AutogeneratedComputedConfiguration array{
36
 *  elements: list<'arguments'|'array'|'array_destructuring'|'group_import'>,
37
 * }
38
 *
39
 * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
40
 *
41
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
42
 */
43
final class NoTrailingCommaInSinglelineFixer extends AbstractFixer implements ConfigurableFixerInterface
44
{
45
    /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
46
    use ConfigurableFixerTrait;
47

48
    public function getDefinition(): FixerDefinitionInterface
49
    {
50
        return new FixerDefinition(
3✔
51
            'If a list of values separated by a comma is contained on a single line, then the last item MUST NOT have a trailing comma.',
3✔
52
            [
3✔
53
                new CodeSample("<?php\nfoo(\$a,);\n\$foo = array(1,);\n[\$foo, \$bar,] = \$array;\nuse a\\{ClassA, ClassB,};\n"),
3✔
54
                new CodeSample("<?php\nfoo(\$a,);\n[\$foo, \$bar,] = \$array;\n", ['elements' => ['array_destructuring']]),
3✔
55
            ],
3✔
56
        );
3✔
57
    }
58

59
    public function isCandidate(Tokens $tokens): bool
60
    {
61
        return
55✔
62
            $tokens->isTokenKindFound(',')
55✔
63
            && $tokens->isAnyTokenKindsFound([')', CT::T_ARRAY_SQUARE_BRACE_CLOSE, CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE, CT::T_GROUP_IMPORT_BRACE_CLOSE]);
55✔
64
    }
65

66
    /**
67
     * {@inheritdoc}
68
     *
69
     * Must run after MethodArgumentSpaceFixer.
70
     */
71
    public function getPriority(): int
72
    {
73
        return 0;
1✔
74
    }
75

76
    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
77
    {
78
        $elements = ['arguments', 'array', 'array_destructuring', 'group_import'];
64✔
79

80
        return new FixerConfigurationResolver([
64✔
81
            (new FixerOptionBuilder('elements', 'Which elements to fix.'))
64✔
82
                ->setAllowedTypes(['string[]'])
64✔
83
                ->setAllowedValues([new AllowedValueSubset($elements)])
64✔
84
                ->setDefault($elements)
64✔
85
                ->getOption(),
64✔
86
        ]);
64✔
87
    }
88

89
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
90
    {
91
        for ($index = $tokens->count() - 1; $index >= 0; --$index) {
51✔
92
            if (!$tokens[$index]->equals(')') && !$tokens[$index]->isGivenKind([CT::T_ARRAY_SQUARE_BRACE_CLOSE, CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE, CT::T_GROUP_IMPORT_BRACE_CLOSE])) {
51✔
93
                continue;
51✔
94
            }
95

96
            $commaIndex = $tokens->getPrevMeaningfulToken($index);
51✔
97

98
            if (!$tokens[$commaIndex]->equals(',')) {
51✔
99
                continue;
19✔
100
            }
101

102
            $block = Tokens::detectBlockType($tokens[$index]);
51✔
103
            $blockOpenIndex = $tokens->findBlockStart($block['type'], $index);
51✔
104

105
            if ($tokens->isPartialCodeMultiline($blockOpenIndex, $index)) {
51✔
106
                continue;
17✔
107
            }
108

109
            if (!$this->shouldBeCleared($tokens, $blockOpenIndex)) {
37✔
110
                continue;
3✔
111
            }
112

113
            do {
114
                $tokens->clearTokenAndMergeSurroundingWhitespace($commaIndex);
35✔
115
                $commaIndex = $tokens->getPrevMeaningfulToken($commaIndex);
35✔
116
            } while ($tokens[$commaIndex]->equals(','));
35✔
117

118
            $tokens->removeTrailingWhitespace($commaIndex);
35✔
119
        }
120
    }
121

122
    private function shouldBeCleared(Tokens $tokens, int $openIndex): bool
123
    {
124
        $elements = $this->configuration['elements'];
37✔
125

126
        if ($tokens[$openIndex]->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
37✔
127
            return \in_array('array', $elements, true);
7✔
128
        }
129

130
        if ($tokens[$openIndex]->isGivenKind(CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN)) {
31✔
131
            return \in_array('array_destructuring', $elements, true);
3✔
132
        }
133

134
        if ($tokens[$openIndex]->isGivenKind(CT::T_GROUP_IMPORT_BRACE_OPEN)) {
29✔
135
            return \in_array('group_import', $elements, true);
2✔
136
        }
137

138
        if (!$tokens[$openIndex]->equals('(')) {
28✔
139
            return false;
×
140
        }
141

142
        $beforeOpen = $tokens->getPrevMeaningfulToken($openIndex);
28✔
143

144
        if ($tokens[$beforeOpen]->isGivenKind(\T_ARRAY)) {
28✔
145
            return \in_array('array', $elements, true);
6✔
146
        }
147

148
        if ($tokens[$beforeOpen]->isGivenKind(\T_LIST)) {
23✔
149
            return \in_array('array_destructuring', $elements, true);
1✔
150
        }
151

152
        if ($tokens[$beforeOpen]->isGivenKind([\T_UNSET, \T_ISSET, \T_VARIABLE, \T_CLASS])) {
22✔
153
            return \in_array('arguments', $elements, true);
5✔
154
        }
155

156
        if ($tokens[$beforeOpen]->isGivenKind(\T_STRING)) {
18✔
157
            return !AttributeAnalyzer::isAttribute($tokens, $beforeOpen) && \in_array('arguments', $elements, true);
18✔
158
        }
159

160
        if ($tokens[$beforeOpen]->equalsAny([')', ']', [CT::T_DYNAMIC_VAR_BRACE_CLOSE], [CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE]])) {
2✔
161
            $block = Tokens::detectBlockType($tokens[$beforeOpen]);
1✔
162

163
            return
1✔
164
                (
1✔
165
                    Tokens::BLOCK_TYPE_ARRAY_INDEX_CURLY_BRACE === $block['type']
1✔
166
                    || Tokens::BLOCK_TYPE_DYNAMIC_VAR_BRACE === $block['type']
1✔
167
                    || Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE === $block['type']
1✔
168
                    || Tokens::BLOCK_TYPE_PARENTHESIS_BRACE === $block['type']
1✔
169
                ) && \in_array('arguments', $elements, true);
1✔
170
        }
171

172
        return false;
1✔
173
    }
174
}
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