src/Fixer/StringNotation/ExplicitStringVariableFixer.php
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
    {
        $backtickStarted = false;
        for ($index = \count($tokens) - 1; $index > 0; --$index) {
            $token = $tokens[$index];
            if ($token->equals('`')) {
                $backtickStarted = !$backtickStarted;

                continue;
            }

            if ($backtickStarted || !$token->isGivenKind(T_VARIABLE)) {
                continue;
            }

            $prevToken = $tokens[$index - 1];
            if (!$this->isStringPartToken($prevToken)) {
                continue;
            }

            $distinctVariableIndex = $index;
            $variableTokens = [
                $distinctVariableIndex => [
                    'tokens' => [$index => $token],
                    'firstVariableTokenIndex' => $index,
                    'lastVariableTokenIndex' => $index,
                ],
            ];

            $nextIndex = $index + 1;
            $squareBracketCount = 0;
            while (!$this->isStringPartToken($tokens[$nextIndex])) {
                if ($tokens[$nextIndex]->isGivenKind(T_VARIABLE) && 1 !== $squareBracketCount) {
                    $distinctVariableIndex = $nextIndex;
                    $variableTokens[$distinctVariableIndex] = [
                        'tokens' => [$nextIndex => $tokens[$nextIndex]],
                        'firstVariableTokenIndex' => $nextIndex,
                        'lastVariableTokenIndex' => $nextIndex,
                    ];
                } else {
                    $variableTokens[$distinctVariableIndex]['tokens'][$nextIndex] = $tokens[$nextIndex];
                    $variableTokens[$distinctVariableIndex]['lastVariableTokenIndex'] = $nextIndex;

                    if ($tokens[$nextIndex]->equalsAny(['[', ']'])) {
                        ++$squareBracketCount;
                    }
                }

                ++$nextIndex;
            }
            krsort($variableTokens, \SORT_NUMERIC);

            foreach ($variableTokens as $distinctVariableSet) {
                if (1 === \count($distinctVariableSet['tokens'])) {
                    $singleVariableIndex = key($distinctVariableSet['tokens']);
                    $singleVariableToken = current($distinctVariableSet['tokens']);
                    $tokens->overrideRange($singleVariableIndex, $singleVariableIndex, [
                        new Token([T_DOLLAR_OPEN_CURLY_BRACES, '${']),
                        new Token([T_STRING_VARNAME, substr($singleVariableToken->getContent(), 1)]),
                        new Token([CT::T_DOLLAR_CLOSE_CURLY_BRACES, '}']),
                    ]);
                } else {
                    foreach ($distinctVariableSet['tokens'] as $variablePartIndex => $variablePartToken) {
                        if ($variablePartToken->isGivenKind(T_NUM_STRING)) {
                            $tokens[$variablePartIndex] = new Token([T_LNUMBER, $variablePartToken->getContent()]);

                            continue;
                        }

                        if ($variablePartToken->isGivenKind(T_STRING) && $tokens[$variablePartIndex + 1]->equals(']')) {
                            $tokens[$variablePartIndex] = new Token([T_CONSTANT_ENCAPSED_STRING, "'".$variablePartToken->getContent()."'"]);
                        }
                    }

                    $tokens->insertAt($distinctVariableSet['lastVariableTokenIndex'] + 1, new Token([CT::T_CURLY_CLOSE, '}']));
                    $tokens->insertAt($distinctVariableSet['firstVariableTokenIndex'], new Token([T_CURLY_OPEN, '{']));
                }
            }
        }
    }
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
    {
        $backtickStarted = false;
        for ($index = \count($tokens) - 1; $index > 0; --$index) {
            $token = $tokens[$index];
            if ($token->equals('`')) {
                $backtickStarted = !$backtickStarted;

                continue;
            }

            if ($backtickStarted || !$token->isGivenKind(T_VARIABLE)) {
                continue;
            }

            $prevToken = $tokens[$index - 1];
            if (!$this->isStringPartToken($prevToken)) {
                continue;
            }

            $distinctVariableIndex = $index;
            $variableTokens = [
                $distinctVariableIndex => [
                    'tokens' => [$index => $token],
                    'firstVariableTokenIndex' => $index,
                    'lastVariableTokenIndex' => $index,
                ],
            ];

            $nextIndex = $index + 1;
            $squareBracketCount = 0;
            while (!$this->isStringPartToken($tokens[$nextIndex])) {
                if ($tokens[$nextIndex]->isGivenKind(T_VARIABLE) && 1 !== $squareBracketCount) {
                    $distinctVariableIndex = $nextIndex;
                    $variableTokens[$distinctVariableIndex] = [
                        'tokens' => [$nextIndex => $tokens[$nextIndex]],
                        'firstVariableTokenIndex' => $nextIndex,
                        'lastVariableTokenIndex' => $nextIndex,
                    ];
                } else {
                    $variableTokens[$distinctVariableIndex]['tokens'][$nextIndex] = $tokens[$nextIndex];
                    $variableTokens[$distinctVariableIndex]['lastVariableTokenIndex'] = $nextIndex;

                    if ($tokens[$nextIndex]->equalsAny(['[', ']'])) {
                        ++$squareBracketCount;
                    }
                }

                ++$nextIndex;
            }
            krsort($variableTokens, \SORT_NUMERIC);

            foreach ($variableTokens as $distinctVariableSet) {
                if (1 === \count($distinctVariableSet['tokens'])) {
                    $singleVariableIndex = key($distinctVariableSet['tokens']);
                    $singleVariableToken = current($distinctVariableSet['tokens']);
                    $tokens->overrideRange($singleVariableIndex, $singleVariableIndex, [
                        new Token([T_DOLLAR_OPEN_CURLY_BRACES, '${']),
                        new Token([T_STRING_VARNAME, substr($singleVariableToken->getContent(), 1)]),
                        new Token([CT::T_DOLLAR_CLOSE_CURLY_BRACES, '}']),
                    ]);
                } else {
                    foreach ($distinctVariableSet['tokens'] as $variablePartIndex => $variablePartToken) {
                        if ($variablePartToken->isGivenKind(T_NUM_STRING)) {
                            $tokens[$variablePartIndex] = new Token([T_LNUMBER, $variablePartToken->getContent()]);

                            continue;
                        }

                        if ($variablePartToken->isGivenKind(T_STRING) && $tokens[$variablePartIndex + 1]->equals(']')) {
                            $tokens[$variablePartIndex] = new Token([T_CONSTANT_ENCAPSED_STRING, "'".$variablePartToken->getContent()."'"]);
                        }
                    }

                    $tokens->insertAt($distinctVariableSet['lastVariableTokenIndex'] + 1, new Token([CT::T_CURLY_CLOSE, '}']));
                    $tokens->insertAt($distinctVariableSet['firstVariableTokenIndex'], new Token([T_CURLY_OPEN, '{']));
                }
            }
        }
    }
protected function applyFix ( \ SplFileInfo $ file , Tokens $ tokens ) { $ backtickStarted = false ; for ( $ index = \ count ( $ tokens ) - 1 ; $ index > 0 ; -- $ index ) { $ token = $ tokens [ $ index ] ; if ( $ token -> equals ( '`' ) ) { $ backtickStarted = ! $ backtickStarted ; continue ; } if ( $ backtickStarted || ! $ token -> isGivenKind ( T_VARIABLE ) ) { continue ; } $ prevToken = $ tokens [ $ index - 1 ] ; if ( ! $ this -> isStringPartToken ( $ prevToken ) ) { continue ; } $ distinctVariableIndex = $ index ; $ variableTokens = [ $ distinctVariableIndex => [ 'tokens' => [ $ index => $ token ] , 'firstVariableTokenIndex' => $ index , 'lastVariableTokenIndex' => $ index , ] , ] ; $ nextIndex = $ index + 1 ; $ squareBracketCount = 0 ; while ( ! $ this -> isStringPartToken ( $ tokens [ $ nextIndex ] ) ) { if ( $ tokens [ $ nextIndex ] -> isGivenKind ( T_VARIABLE ) && 1 !== $ squareBracketCount ) { $ distinctVariableIndex = $ nextIndex ; $ variableTokens [ $ distinctVariableIndex ] = [ 'tokens' => [ $ nextIndex => $ tokens [ $ nextIndex ] ] , 'firstVariableTokenIndex' => $ nextIndex , 'lastVariableTokenIndex' => $ nextIndex , ] ; } else { $ variableTokens [ $ distinctVariableIndex ] [ 'tokens' ] [ $ nextIndex ] = $ tokens [ $ nextIndex ] ; $ variableTokens [ $ distinctVariableIndex ] [ 'lastVariableTokenIndex' ] = $ nextIndex ; if ( $ tokens [ $ nextIndex ] -> equalsAny ( [ '[' , ']' ] ) ) { ++ $ squareBracketCount ; } } ++ $ nextIndex ; } krsort ( $ variableTokens , \ SORT_NUMERIC ) ; foreach ( $ variableTokens as $ distinctVariableSet ) { if ( 1 === \ count ( $ distinctVariableSet [ 'tokens' ] ) ) { $ singleVariableIndex = key ( $ distinctVariableSet [ 'tokens' ] ) ; $ singleVariableToken = current ( $ distinctVariableSet [ 'tokens' ] ) ; $ tokens -> overrideRange ( $ singleVariableIndex , $ singleVariableIndex , [ new Token ( [ T_DOLLAR_OPEN_CURLY_BRACES , '${' ] ) , new Token ( [ T_STRING_VARNAME , substr ( $ singleVariableToken -> getContent ( ) , 1 ) ] ) , new Token ( [ CT :: T_DOLLAR_CLOSE_CURLY_BRACES , '}' ] ) , ] ) ; } else { foreach ( $ distinctVariableSet [ 'tokens' ] as $ variablePartIndex => $ variablePartToken ) { if ( $ variablePartToken -> isGivenKind ( T_NUM_STRING ) ) { $ tokens [ $ variablePartIndex ] = new Token ( [ T_LNUMBER , $ variablePartToken -> getContent ( ) ] ) ; continue ; } if ( $ variablePartToken -> isGivenKind ( T_STRING ) && $ tokens [ $ variablePartIndex + 1 ] -> equals ( ']' ) ) { $ tokens [ $ variablePartIndex ] = new Token ( [ T_CONSTANT_ENCAPSED_STRING , "'" . $ variablePartToken -> getContent ( ) . "'" ] ) ; } } $ tokens -> insertAt ( $ distinctVariableSet [ 'lastVariableTokenIndex' ] + 1 , new Token ( [ CT :: T_CURLY_CLOSE , '}' ] ) ) ; $ tokens -> insertAt ( $ distinctVariableSet [ 'firstVariableTokenIndex' ] , new Token ( [ T_CURLY_OPEN , '{' ] ) ) ; } } } }
{@inheritdoc}
{