|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of PHP CS Fixer. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * Dariusz Rumiński <dariusz.ruminski@gmail.com> |
| 8 | + * |
| 9 | + * This source file is subject to the MIT license that is bundled |
| 10 | + * with this source code in the file LICENSE. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace PhpCsFixer\Fixer\StringNotation; |
| 14 | + |
| 15 | +use PhpCsFixer\AbstractFixer; |
| 16 | +use PhpCsFixer\FixerDefinition\CodeSample; |
| 17 | +use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 18 | +use PhpCsFixer\Tokenizer\CT; |
| 19 | +use PhpCsFixer\Tokenizer\Token; |
| 20 | +use PhpCsFixer\Tokenizer\Tokens; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Dave van der Brugge <dmvdbrugge@gmail.com> |
| 24 | + */ |
| 25 | +final class SimpleToComplexStringVariableFixer extends AbstractFixer |
| 26 | +{ |
| 27 | + /** |
| 28 | + * {@inheritdoc} |
| 29 | + */ |
| 30 | + public function getDefinition() |
| 31 | + { |
| 32 | + return new FixerDefinition( |
| 33 | + 'Converts explicit variables in double-quoted strings and heredoc syntax from simple to complex format (`${` to `{$`).', |
| 34 | + [ |
| 35 | + new CodeSample( |
| 36 | + <<<'EOT' |
| 37 | +<?php |
| 38 | +$name = 'World'; |
| 39 | +echo "Hello ${name}!"; |
| 40 | + |
| 41 | +EOT |
| 42 | + ), |
| 43 | + new CodeSample( |
| 44 | + <<<'EOT' |
| 45 | +<?php |
| 46 | +$name = 'World'; |
| 47 | +echo <<<TEST |
| 48 | +Hello ${name}! |
| 49 | +TEST; |
| 50 | + |
| 51 | +EOT |
| 52 | + ), |
| 53 | + ], |
| 54 | + "Doesn't touch implicit variables. Works together nicely with `explicit_string_variable`." |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritdoc} |
| 60 | + */ |
| 61 | + public function getPriority() |
| 62 | + { |
| 63 | + // should run after ExplicitStringVariableFixer |
| 64 | + return -10; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * {@inheritdoc} |
| 69 | + */ |
| 70 | + public function isCandidate(Tokens $tokens) |
| 71 | + { |
| 72 | + return $tokens->isTokenKindFound(T_DOLLAR_OPEN_CURLY_BRACES); |
| 73 | + } |
| 74 | + |
| 75 | + protected function applyFix(\SplFileInfo $file, Tokens $tokens) |
| 76 | + { |
| 77 | + for ($index = \count($tokens) - 3; $index > 0; --$index) { |
| 78 | + $token = $tokens[$index]; |
| 79 | + |
| 80 | + if (!$token->isGivenKind(T_DOLLAR_OPEN_CURLY_BRACES)) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + $varnameToken = $tokens[$index + 1]; |
| 85 | + |
| 86 | + if (!$varnameToken->isGivenKind(T_STRING_VARNAME)) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + $dollarCloseToken = $tokens[$index + 2]; |
| 91 | + |
| 92 | + if (!$dollarCloseToken->isGivenKind(CT::T_DOLLAR_CLOSE_CURLY_BRACES)) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + $tokens->overrideRange($index, $index + 2, [ |
| 97 | + new Token([T_CURLY_OPEN, '{']), |
| 98 | + new Token([T_VARIABLE, '$'.$varnameToken->getContent()]), |
| 99 | + new Token([CT::T_CURLY_CLOSE, '}']), |
| 100 | + ]); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments