src/Fixer/Import/OrderedImportsFixer.php
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
    {
        $tokensAnalyzer = new TokensAnalyzer($tokens);
        $namespacesImports = $tokensAnalyzer->getImportUseIndexes(true);

        if (0 === \count($namespacesImports)) {
            return;
        }

        $usesOrder = [];
        foreach ($namespacesImports as $uses) {
            $usesOrder[] = $this->getNewOrder(array_reverse($uses), $tokens);
        }
        $usesOrder = array_replace(...$usesOrder);

        $usesOrder = array_reverse($usesOrder, true);
        $mapStartToEnd = [];

        foreach ($usesOrder as $use) {
            $mapStartToEnd[$use['startIndex']] = $use['endIndex'];
        }

        // Now insert the new tokens, starting from the end
        foreach ($usesOrder as $index => $use) {
            $declarationTokens = Tokens::fromCode(
                sprintf(
                    '<?php use %s%s;',
                    self::IMPORT_TYPE_CLASS === $use['importType'] ? '' : ' '.$use['importType'].' ',
                    $use['namespace']
                )
            );

            $declarationTokens->clearRange(0, 2); // clear `<?php use `
            $declarationTokens->clearAt(\count($declarationTokens) - 1); // clear `;`
            $declarationTokens->clearEmptyTokens();

            $tokens->overrideRange($index, $mapStartToEnd[$index], $declarationTokens);
            if ($use['group']) {
                // a group import must start with `use` and cannot be part of comma separated import list
                $prev = $tokens->getPrevMeaningfulToken($index);
                if ($tokens[$prev]->equals(',')) {
                    $tokens[$prev] = new Token(';');
                    $tokens->insertAt($prev + 1, new Token([T_USE, 'use']));

                    if (!$tokens[$prev + 2]->isWhitespace()) {
                        $tokens->insertAt($prev + 2, new Token([T_WHITESPACE, ' ']));
                    }
                }
            }
        }
    }
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
    {
        $tokensAnalyzer = new TokensAnalyzer($tokens);
        $namespacesImports = $tokensAnalyzer->getImportUseIndexes(true);

        if (0 === \count($namespacesImports)) {
            return;
        }

        $usesOrder = [];
        foreach ($namespacesImports as $uses) {
            $usesOrder[] = $this->getNewOrder(array_reverse($uses), $tokens);
        }
        $usesOrder = array_replace(...$usesOrder);

        $usesOrder = array_reverse($usesOrder, true);
        $mapStartToEnd = [];

        foreach ($usesOrder as $use) {
            $mapStartToEnd[$use['startIndex']] = $use['endIndex'];
        }

        // Now insert the new tokens, starting from the end
        foreach ($usesOrder as $index => $use) {
            $declarationTokens = Tokens::fromCode(
                sprintf(
                    '<?php use %s%s;',
                    self::IMPORT_TYPE_CLASS === $use['importType'] ? '' : ' '.$use['importType'].' ',
                    $use['namespace']
                )
            );

            $declarationTokens->clearRange(0, 2); // clear `<?php use `
            $declarationTokens->clearAt(\count($declarationTokens) - 1); // clear `;`
            $declarationTokens->clearEmptyTokens();

            $tokens->overrideRange($index, $mapStartToEnd[$index], $declarationTokens);
            if ($use['group']) {
                // a group import must start with `use` and cannot be part of comma separated import list
                $prev = $tokens->getPrevMeaningfulToken($index);
                if ($tokens[$prev]->equals(',')) {
                    $tokens[$prev] = new Token(';');
                    $tokens->insertAt($prev + 1, new Token([T_USE, 'use']));

                    if (!$tokens[$prev + 2]->isWhitespace()) {
                        $tokens->insertAt($prev + 2, new Token([T_WHITESPACE, ' ']));
                    }
                }
            }
        }
    }
protected function applyFix ( \ SplFileInfo $ file , Tokens $ tokens ) { $ tokensAnalyzer = new TokensAnalyzer ( $ tokens ) ; $ namespacesImports = $ tokensAnalyzer -> getImportUseIndexes ( true ) ; if ( 0 === \ count ( $ namespacesImports ) ) { return ; } $ usesOrder = [ ] ; foreach ( $ namespacesImports as $ uses ) { $ usesOrder [ ] = $ this -> getNewOrder ( array_reverse ( $ uses ) , $ tokens ) ; } $ usesOrder = array_replace ( ... $ usesOrder ) ; $ usesOrder = array_reverse ( $ usesOrder , true ) ; $ mapStartToEnd = [ ] ; foreach ( $ usesOrder as $ use ) { $ mapStartToEnd [ $ use [ 'startIndex' ] ] = $ use [ 'endIndex' ] ; } // Now insert the new tokens, starting from the end foreach ( $ usesOrder as $ index => $ use ) { $ declarationTokens = Tokens :: fromCode ( sprintf ( '<?php use %s%s;' , self :: IMPORT_TYPE_CLASS === $ use [ 'importType' ] ? '' : ' ' . $ use [ 'importType' ] . ' ' , $ use [ 'namespace' ] ) ) ; $ declarationTokens -> clearRange ( 0 , 2 ) ; // clear `<?php use ` $ declarationTokens -> clearAt ( \ count ( $ declarationTokens ) - 1 ) ; // clear `;` $ declarationTokens -> clearEmptyTokens ( ) ; $ tokens -> overrideRange ( $ index , $ mapStartToEnd [ $ index ] , $ declarationTokens ) ; if ( $ use [ 'group' ] ) { // a group import must start with `use` and cannot be part of comma separated import list $ prev = $ tokens -> getPrevMeaningfulToken ( $ index ) ; if ( $ tokens [ $ prev ] -> equals ( ',' ) ) { $ tokens [ $ prev ] = new Token ( ';' ) ; $ tokens -> insertAt ( $ prev + 1 , new Token ( [ T_USE , 'use' ] ) ) ; if ( ! $ tokens [ $ prev + 2 ] -> isWhitespace ( ) ) { $ tokens -> insertAt ( $ prev + 2 , new Token ( [ T_WHITESPACE , ' ' ] ) ) ; } } } } }
{@inheritdoc}
{