src/Fixer/Import/SingleLineAfterImportsFixer.php
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
    {
        $ending = $this->whitespacesConfig->getLineEnding();
        $tokensAnalyzer = new TokensAnalyzer($tokens);

        $added = 0;
        foreach ($tokensAnalyzer->getImportUseIndexes() as $index) {
            $index += $added;
            $indent = '';

            // if previous line ends with comment and current line starts with whitespace, use current indent
            if ($tokens[$index - 1]->isWhitespace(" \t") && $tokens[$index - 2]->isGivenKind(T_COMMENT)) {
                $indent = $tokens[$index - 1]->getContent();
            } elseif ($tokens[$index - 1]->isWhitespace()) {
                $indent = Utils::calculateTrailingWhitespaceIndent($tokens[$index - 1]);
            }

            $semicolonIndex = $tokens->getNextTokenOfKind($index, [';', [T_CLOSE_TAG]]); // Handle insert index for inline T_COMMENT with whitespace after semicolon
            $insertIndex = $semicolonIndex;

            if ($tokens[$semicolonIndex]->isGivenKind(T_CLOSE_TAG)) {
                if ($tokens[$insertIndex - 1]->isWhitespace()) {
                    --$insertIndex;
                }

                $tokens->insertAt($insertIndex, new Token(';'));
                ++$added;
            }

            if ($semicolonIndex === \count($tokens) - 1) {
                $tokens->insertAt($insertIndex + 1, new Token([T_WHITESPACE, $ending.$ending.$indent]));
                ++$added;
            } else {
                $newline = $ending;
                $tokens[$semicolonIndex]->isGivenKind(T_CLOSE_TAG) ? --$insertIndex : ++$insertIndex;
                if ($tokens[$insertIndex]->isWhitespace(" \t") && $tokens[$insertIndex + 1]->isComment()) {
                    ++$insertIndex;
                }

                // Increment insert index for inline T_COMMENT or T_DOC_COMMENT
                if ($tokens[$insertIndex]->isComment()) {
                    ++$insertIndex;
                }

                $afterSemicolon = $tokens->getNextMeaningfulToken($semicolonIndex);
                if (null === $afterSemicolon || !$tokens[$afterSemicolon]->isGivenKind(T_USE)) {
                    $newline .= $ending;
                }

                if ($tokens[$insertIndex]->isWhitespace()) {
                    $nextToken = $tokens[$insertIndex];
                    $nextMeaningfulAfterUseIndex = $tokens->getNextMeaningfulToken($insertIndex);
                    if (null !== $nextMeaningfulAfterUseIndex && $tokens[$nextMeaningfulAfterUseIndex]->isGivenKind(T_USE)) {
                        if (substr_count($nextToken->getContent(), "\n") < 2) {
                            $tokens[$insertIndex] = new Token([T_WHITESPACE, $newline.$indent.ltrim($nextToken->getContent())]);
                        }
                    } else {
                        $tokens[$insertIndex] = new Token([T_WHITESPACE, $newline.$indent.ltrim($nextToken->getContent())]);
                    }
                } else {
                    $tokens->insertAt($insertIndex, new Token([T_WHITESPACE, $newline.$indent]));
                    ++$added;
                }
            }
        }
    }
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
    {
        $ending = $this->whitespacesConfig->getLineEnding();
        $tokensAnalyzer = new TokensAnalyzer($tokens);

        $added = 0;
        foreach ($tokensAnalyzer->getImportUseIndexes() as $index) {
            $index += $added;
            $indent = '';

            // if previous line ends with comment and current line starts with whitespace, use current indent
            if ($tokens[$index - 1]->isWhitespace(" \t") && $tokens[$index - 2]->isGivenKind(T_COMMENT)) {
                $indent = $tokens[$index - 1]->getContent();
            } elseif ($tokens[$index - 1]->isWhitespace()) {
                $indent = Utils::calculateTrailingWhitespaceIndent($tokens[$index - 1]);
            }

            $semicolonIndex = $tokens->getNextTokenOfKind($index, [';', [T_CLOSE_TAG]]); // Handle insert index for inline T_COMMENT with whitespace after semicolon
            $insertIndex = $semicolonIndex;

            if ($tokens[$semicolonIndex]->isGivenKind(T_CLOSE_TAG)) {
                if ($tokens[$insertIndex - 1]->isWhitespace()) {
                    --$insertIndex;
                }

                $tokens->insertAt($insertIndex, new Token(';'));
                ++$added;
            }

            if ($semicolonIndex === \count($tokens) - 1) {
                $tokens->insertAt($insertIndex + 1, new Token([T_WHITESPACE, $ending.$ending.$indent]));
                ++$added;
            } else {
                $newline = $ending;
                $tokens[$semicolonIndex]->isGivenKind(T_CLOSE_TAG) ? --$insertIndex : ++$insertIndex;
                if ($tokens[$insertIndex]->isWhitespace(" \t") && $tokens[$insertIndex + 1]->isComment()) {
                    ++$insertIndex;
                }

                // Increment insert index for inline T_COMMENT or T_DOC_COMMENT
                if ($tokens[$insertIndex]->isComment()) {
                    ++$insertIndex;
                }

                $afterSemicolon = $tokens->getNextMeaningfulToken($semicolonIndex);
                if (null === $afterSemicolon || !$tokens[$afterSemicolon]->isGivenKind(T_USE)) {
                    $newline .= $ending;
                }

                if ($tokens[$insertIndex]->isWhitespace()) {
                    $nextToken = $tokens[$insertIndex];
                    $nextMeaningfulAfterUseIndex = $tokens->getNextMeaningfulToken($insertIndex);
                    if (null !== $nextMeaningfulAfterUseIndex && $tokens[$nextMeaningfulAfterUseIndex]->isGivenKind(T_USE)) {
                        if (substr_count($nextToken->getContent(), "\n") < 2) {
                            $tokens[$insertIndex] = new Token([T_WHITESPACE, $newline.$indent.ltrim($nextToken->getContent())]);
                        }
                    } else {
                        $tokens[$insertIndex] = new Token([T_WHITESPACE, $newline.$indent.ltrim($nextToken->getContent())]);
                    }
                } else {
                    $tokens->insertAt($insertIndex, new Token([T_WHITESPACE, $newline.$indent]));
                    ++$added;
                }
            }
        }
    }
protected function applyFix ( \ SplFileInfo $ file , Tokens $ tokens ) { $ ending = $ this -> whitespacesConfig -> getLineEnding ( ) ; $ tokensAnalyzer = new TokensAnalyzer ( $ tokens ) ; $ added = 0 ; foreach ( $ tokensAnalyzer -> getImportUseIndexes ( ) as $ index ) { $ index += $ added ; $ indent = '' ; // if previous line ends with comment and current line starts with whitespace, use current indent if ( $ tokens [ $ index - 1 ] -> isWhitespace ( " \t" ) && $ tokens [ $ index - 2 ] -> isGivenKind ( T_COMMENT ) ) { $ indent = $ tokens [ $ index - 1 ] -> getContent ( ) ; } elseif ( $ tokens [ $ index - 1 ] -> isWhitespace ( ) ) { $ indent = Utils :: calculateTrailingWhitespaceIndent ( $ tokens [ $ index - 1 ] ) ; } $ semicolonIndex = $ tokens -> getNextTokenOfKind ( $ index , [ ';' , [ T_CLOSE_TAG ] ] ) ; // Handle insert index for inline T_COMMENT with whitespace after semicolon $ insertIndex = $ semicolonIndex ; if ( $ tokens [ $ semicolonIndex ] -> isGivenKind ( T_CLOSE_TAG ) ) { if ( $ tokens [ $ insertIndex - 1 ] -> isWhitespace ( ) ) { -- $ insertIndex ; } $ tokens -> insertAt ( $ insertIndex , new Token ( ';' ) ) ; ++ $ added ; } if ( $ semicolonIndex === \ count ( $ tokens ) - 1 ) { $ tokens -> insertAt ( $ insertIndex + 1 , new Token ( [ T_WHITESPACE , $ ending . $ ending . $ indent ] ) ) ; ++ $ added ; } else { $ newline = $ ending ; $ tokens [ $ semicolonIndex ] -> isGivenKind ( T_CLOSE_TAG ) ? -- $ insertIndex : ++ $ insertIndex ; if ( $ tokens [ $ insertIndex ] -> isWhitespace ( " \t" ) && $ tokens [ $ insertIndex + 1 ] -> isComment ( ) ) { ++ $ insertIndex ; } // Increment insert index for inline T_COMMENT or T_DOC_COMMENT if ( $ tokens [ $ insertIndex ] -> isComment ( ) ) { ++ $ insertIndex ; } $ afterSemicolon = $ tokens -> getNextMeaningfulToken ( $ semicolonIndex ) ; if ( null === $ afterSemicolon || ! $ tokens [ $ afterSemicolon ] -> isGivenKind ( T_USE ) ) { $ newline .= $ ending ; } if ( $ tokens [ $ insertIndex ] -> isWhitespace ( ) ) { $ nextToken = $ tokens [ $ insertIndex ] ; $ nextMeaningfulAfterUseIndex = $ tokens -> getNextMeaningfulToken ( $ insertIndex ) ; if ( null !== $ nextMeaningfulAfterUseIndex && $ tokens [ $ nextMeaningfulAfterUseIndex ] -> isGivenKind ( T_USE ) ) { if ( substr_count ( $ nextToken -> getContent ( ) , "\n" ) < 2 ) { $ tokens [ $ insertIndex ] = new Token ( [ T_WHITESPACE , $ newline . $ indent . ltrim ( $ nextToken -> getContent ( ) ) ] ) ; } } else { $ tokens [ $ insertIndex ] = new Token ( [ T_WHITESPACE , $ newline . $ indent . ltrim ( $ nextToken -> getContent ( ) ) ] ) ; } } else { $ tokens -> insertAt ( $ insertIndex , new Token ( [ T_WHITESPACE , $ newline . $ indent ] ) ) ; ++ $ added ; } } } }
{@inheritdoc}
{