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

keradus / PHP-CS-Fixer / 17252691116

26 Aug 2025 11:09PM UTC coverage: 94.743% (-0.01%) from 94.755%
17252691116

push

github

keradus
chore: apply phpdoc_tag_no_named_arguments

28313 of 29884 relevant lines covered (94.74%)

45.64 hits per line

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

98.39
/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
    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
67
    {
68
        $elements = ['arguments', 'array', 'array_destructuring', 'group_import'];
64✔
69

70
        return new FixerConfigurationResolver([
64✔
71
            (new FixerOptionBuilder('elements', 'Which elements to fix.'))
64✔
72
                ->setAllowedTypes(['string[]'])
64✔
73
                ->setAllowedValues([new AllowedValueSubset($elements)])
64✔
74
                ->setDefault($elements)
64✔
75
                ->getOption(),
64✔
76
        ]);
64✔
77
    }
78

79
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
80
    {
81
        for ($index = $tokens->count() - 1; $index >= 0; --$index) {
51✔
82
            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✔
83
                continue;
51✔
84
            }
85

86
            $commaIndex = $tokens->getPrevMeaningfulToken($index);
51✔
87

88
            if (!$tokens[$commaIndex]->equals(',')) {
51✔
89
                continue;
19✔
90
            }
91

92
            $block = Tokens::detectBlockType($tokens[$index]);
51✔
93
            $blockOpenIndex = $tokens->findBlockStart($block['type'], $index);
51✔
94

95
            if ($tokens->isPartialCodeMultiline($blockOpenIndex, $index)) {
51✔
96
                continue;
17✔
97
            }
98

99
            if (!$this->shouldBeCleared($tokens, $blockOpenIndex)) {
37✔
100
                continue;
3✔
101
            }
102

103
            do {
104
                $tokens->clearTokenAndMergeSurroundingWhitespace($commaIndex);
35✔
105
                $commaIndex = $tokens->getPrevMeaningfulToken($commaIndex);
35✔
106
            } while ($tokens[$commaIndex]->equals(','));
35✔
107

108
            $tokens->removeTrailingWhitespace($commaIndex);
35✔
109
        }
110
    }
111

112
    private function shouldBeCleared(Tokens $tokens, int $openIndex): bool
113
    {
114
        $elements = $this->configuration['elements'];
37✔
115

116
        if ($tokens[$openIndex]->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
37✔
117
            return \in_array('array', $elements, true);
7✔
118
        }
119

120
        if ($tokens[$openIndex]->isGivenKind(CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN)) {
31✔
121
            return \in_array('array_destructuring', $elements, true);
3✔
122
        }
123

124
        if ($tokens[$openIndex]->isGivenKind(CT::T_GROUP_IMPORT_BRACE_OPEN)) {
29✔
125
            return \in_array('group_import', $elements, true);
2✔
126
        }
127

128
        if (!$tokens[$openIndex]->equals('(')) {
28✔
129
            return false;
×
130
        }
131

132
        $beforeOpen = $tokens->getPrevMeaningfulToken($openIndex);
28✔
133

134
        if ($tokens[$beforeOpen]->isGivenKind(\T_ARRAY)) {
28✔
135
            return \in_array('array', $elements, true);
6✔
136
        }
137

138
        if ($tokens[$beforeOpen]->isGivenKind(\T_LIST)) {
23✔
139
            return \in_array('array_destructuring', $elements, true);
1✔
140
        }
141

142
        if ($tokens[$beforeOpen]->isGivenKind([\T_UNSET, \T_ISSET, \T_VARIABLE, \T_CLASS])) {
22✔
143
            return \in_array('arguments', $elements, true);
5✔
144
        }
145

146
        if ($tokens[$beforeOpen]->isGivenKind(\T_STRING)) {
18✔
147
            return !AttributeAnalyzer::isAttribute($tokens, $beforeOpen) && \in_array('arguments', $elements, true);
18✔
148
        }
149

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

153
            return
1✔
154
                (
1✔
155
                    Tokens::BLOCK_TYPE_ARRAY_INDEX_CURLY_BRACE === $block['type']
1✔
156
                    || Tokens::BLOCK_TYPE_DYNAMIC_VAR_BRACE === $block['type']
1✔
157
                    || Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE === $block['type']
1✔
158
                    || Tokens::BLOCK_TYPE_PARENTHESIS_BRACE === $block['type']
1✔
159
                ) && \in_array('arguments', $elements, true);
1✔
160
        }
161

162
        return false;
1✔
163
    }
164
}
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