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

keradus / PHP-CS-Fixer / 17279562118

27 Aug 2025 09:47PM UTC coverage: 94.693%. Remained the same
17279562118

push

github

keradus
CS

28316 of 29903 relevant lines covered (94.69%)

45.61 hits per line

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

91.89
/src/Fixer/FunctionNotation/NoUselessSprintfFixer.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\FunctionNotation;
16

17
use PhpCsFixer\AbstractFixer;
18
use PhpCsFixer\FixerDefinition\CodeSample;
19
use PhpCsFixer\FixerDefinition\FixerDefinition;
20
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
21
use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
22
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
23
use PhpCsFixer\Tokenizer\Tokens;
24

25
/**
26
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
27
 */
28
final class NoUselessSprintfFixer extends AbstractFixer
29
{
30
    public function getDefinition(): FixerDefinitionInterface
31
    {
32
        return new FixerDefinition(
3✔
33
            'There must be no `sprintf` calls with only the first argument.',
3✔
34
            [
3✔
35
                new CodeSample(
3✔
36
                    "<?php\n\$foo = sprintf('bar');\n"
3✔
37
                ),
3✔
38
            ],
3✔
39
            null,
3✔
40
            'Risky when if the `sprintf` function is overridden.'
3✔
41
        );
3✔
42
    }
43

44
    public function isCandidate(Tokens $tokens): bool
45
    {
46
        return $tokens->isTokenKindFound(\T_STRING);
13✔
47
    }
48

49
    public function isRisky(): bool
50
    {
51
        return true;
1✔
52
    }
53

54
    /**
55
     * {@inheritdoc}
56
     *
57
     * Must run before MethodArgumentSpaceFixer, NativeFunctionCasingFixer, NoEmptyStatementFixer, NoExtraBlankLinesFixer, NoSpacesInsideParenthesisFixer, SpacesInsideParenthesesFixer.
58
     */
59
    public function getPriority(): int
60
    {
61
        return 42;
1✔
62
    }
63

64
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
65
    {
66
        $functionAnalyzer = new FunctionsAnalyzer();
13✔
67
        $argumentsAnalyzer = new ArgumentsAnalyzer();
13✔
68

69
        for ($index = \count($tokens) - 1; $index > 0; --$index) {
13✔
70
            if (!$tokens[$index]->isGivenKind(\T_STRING)) {
13✔
71
                continue;
13✔
72
            }
73

74
            if ('sprintf' !== strtolower($tokens[$index]->getContent())) {
13✔
75
                continue;
4✔
76
            }
77

78
            if (!$functionAnalyzer->isGlobalFunctionCall($tokens, $index)) {
12✔
79
                continue;
3✔
80
            }
81

82
            $openParenthesisIndex = $tokens->getNextTokenOfKind($index, ['(']);
9✔
83

84
            if ($tokens[$tokens->getNextMeaningfulToken($openParenthesisIndex)]->isGivenKind(\T_ELLIPSIS)) {
9✔
85
                continue;
×
86
            }
87

88
            $closeParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesisIndex);
9✔
89

90
            if (1 !== $argumentsAnalyzer->countArguments($tokens, $openParenthesisIndex, $closeParenthesisIndex)) {
9✔
91
                continue;
×
92
            }
93

94
            $tokens->clearTokenAndMergeSurroundingWhitespace($closeParenthesisIndex);
9✔
95

96
            $prevMeaningfulTokenIndex = $tokens->getPrevMeaningfulToken($closeParenthesisIndex);
9✔
97

98
            if ($tokens[$prevMeaningfulTokenIndex]->equals(',')) {
9✔
99
                $tokens->clearTokenAndMergeSurroundingWhitespace($prevMeaningfulTokenIndex);
1✔
100
            }
101

102
            $tokens->clearTokenAndMergeSurroundingWhitespace($openParenthesisIndex);
9✔
103
            $tokens->clearTokenAndMergeSurroundingWhitespace($index);
9✔
104

105
            $prevMeaningfulTokenIndex = $tokens->getPrevMeaningfulToken($index);
9✔
106

107
            if ($tokens[$prevMeaningfulTokenIndex]->isGivenKind(\T_NS_SEPARATOR)) {
9✔
108
                $tokens->clearTokenAndMergeSurroundingWhitespace($prevMeaningfulTokenIndex);
×
109
            }
110
        }
111
    }
112
}
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