• 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

96.55
/src/Fixer/StringNotation/SimpleToComplexStringVariableFixer.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\StringNotation;
16

17
use PhpCsFixer\AbstractFixer;
18
use PhpCsFixer\FixerDefinition\CodeSample;
19
use PhpCsFixer\FixerDefinition\FixerDefinition;
20
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
21
use PhpCsFixer\Tokenizer\CT;
22
use PhpCsFixer\Tokenizer\Token;
23
use PhpCsFixer\Tokenizer\Tokens;
24

25
/**
26
 * @author Dave van der Brugge <dmvdbrugge@gmail.com>
27
 *
28
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
29
 */
30
final class SimpleToComplexStringVariableFixer extends AbstractFixer
31
{
32
    public function getDefinition(): FixerDefinitionInterface
33
    {
34
        return new FixerDefinition(
3✔
35
            'Converts explicit variables in double-quoted strings and heredoc syntax from simple to complex format (`${` to `{$`).',
3✔
36
            [
3✔
37
                new CodeSample(
3✔
38
                    <<<'EOT'
3✔
39
                        <?php
40
                        $name = 'World';
41
                        echo "Hello ${name}!";
42

43
                        EOT
3✔
44
                ),
3✔
45
                new CodeSample(
3✔
46
                    <<<'EOT'
3✔
47
                        <?php
48
                        $name = 'World';
49
                        echo <<<TEST
50
                        Hello ${name}!
51
                        TEST;
52

53
                        EOT
3✔
54
                ),
3✔
55
            ],
3✔
56
            "Doesn't touch implicit variables. Works together nicely with `explicit_string_variable`."
3✔
57
        );
3✔
58
    }
59

60
    /**
61
     * {@inheritdoc}
62
     *
63
     * Must run after ExplicitStringVariableFixer.
64
     */
65
    public function getPriority(): int
66
    {
67
        return -10;
1✔
68
    }
69

70
    public function isCandidate(Tokens $tokens): bool
71
    {
72
        return $tokens->isTokenKindFound(\T_DOLLAR_OPEN_CURLY_BRACES);
10✔
73
    }
74

75
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
76
    {
77
        for ($index = \count($tokens) - 3; $index > 0; --$index) {
6✔
78
            if (!$tokens[$index]->isGivenKind(\T_DOLLAR_OPEN_CURLY_BRACES)) {
6✔
79
                continue;
6✔
80
            }
81
            $varnameToken = $tokens[$index + 1];
6✔
82

83
            if (!$varnameToken->isGivenKind(\T_STRING_VARNAME)) {
6✔
84
                continue;
×
85
            }
86

87
            $dollarCloseToken = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_COMPLEX_STRING_VARIABLE, $index);
6✔
88

89
            $prevTokenContent = $tokens[$index - 1]->getContent();
6✔
90
            if (str_ends_with($prevTokenContent, '$') && !str_ends_with($prevTokenContent, '\$')) {
6✔
91
                $tokens[$index - 1] = new Token([\T_ENCAPSED_AND_WHITESPACE, substr($prevTokenContent, 0, -1).'\$']);
2✔
92
            }
93
            $tokens[$index] = new Token([\T_CURLY_OPEN, '{']);
6✔
94
            $tokens[$index + 1] = new Token([\T_VARIABLE, '$'.$varnameToken->getContent()]);
6✔
95
            $tokens[$dollarCloseToken] = new Token([CT::T_CURLY_CLOSE, '}']);
6✔
96
        }
97
    }
98
}
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