src/Fixer/ControlStructure/NoUnneededControlParenthesesFixer.php
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
    {
        // Checks if specific statements are set and uses them in this case.
        $loops = array_intersect_key(self::$loops, array_flip($this->configuration['statements']));

        foreach ($tokens as $index => $token) {
            if (!$token->equalsAny(['(', [CT::T_BRACE_CLASS_INSTANTIATION_OPEN]])) {
                continue;
            }

            $blockStartIndex = $index;
            $index = $tokens->getPrevMeaningfulToken($index);
            $prevToken = $tokens[$index];

            foreach ($loops as $loop) {
                if (!$prevToken->isGivenKind($loop['lookupTokens'])) {
                    continue;
                }

                $blockEndIndex = $tokens->findBlockEnd(
                    $token->equals('(') ? Tokens::BLOCK_TYPE_PARENTHESIS_BRACE : Tokens::BLOCK_TYPE_BRACE_CLASS_INSTANTIATION,
                    $blockStartIndex
                );
                $blockEndNextIndex = $tokens->getNextMeaningfulToken($blockEndIndex);

                if (!$tokens[$blockEndNextIndex]->equalsAny($loop['neededSuccessors'])) {
                    continue;
                }

                if (\array_key_exists('forbiddenContents', $loop)) {
                    $forbiddenTokenIndex = $tokens->getNextTokenOfKind($blockStartIndex, $loop['forbiddenContents']);
                    // A forbidden token is found and is inside the parenthesis.
                    if (null !== $forbiddenTokenIndex && $forbiddenTokenIndex < $blockEndIndex) {
                        continue;
                    }
                }

                if ($tokens[$blockStartIndex - 1]->isWhitespace() || $tokens[$blockStartIndex - 1]->isComment()) {
                    $tokens->clearTokenAndMergeSurroundingWhitespace($blockStartIndex);
                } else {
                    // Adds a space to prevent broken code like `return2`.
                    $tokens[$blockStartIndex] = new Token([T_WHITESPACE, ' ']);
                }

                $tokens->clearTokenAndMergeSurroundingWhitespace($blockEndIndex);
            }
        }
    }
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
    {
        // Checks if specific statements are set and uses them in this case.
        $loops = array_intersect_key(self::$loops, array_flip($this->configuration['statements']));

        foreach ($tokens as $index => $token) {
            if (!$token->equalsAny(['(', [CT::T_BRACE_CLASS_INSTANTIATION_OPEN]])) {
                continue;
            }

            $blockStartIndex = $index;
            $index = $tokens->getPrevMeaningfulToken($index);
            $prevToken = $tokens[$index];

            foreach ($loops as $loop) {
                if (!$prevToken->isGivenKind($loop['lookupTokens'])) {
                    continue;
                }

                $blockEndIndex = $tokens->findBlockEnd(
                    $token->equals('(') ? Tokens::BLOCK_TYPE_PARENTHESIS_BRACE : Tokens::BLOCK_TYPE_BRACE_CLASS_INSTANTIATION,
                    $blockStartIndex
                );
                $blockEndNextIndex = $tokens->getNextMeaningfulToken($blockEndIndex);

                if (!$tokens[$blockEndNextIndex]->equalsAny($loop['neededSuccessors'])) {
                    continue;
                }

                if (\array_key_exists('forbiddenContents', $loop)) {
                    $forbiddenTokenIndex = $tokens->getNextTokenOfKind($blockStartIndex, $loop['forbiddenContents']);
                    // A forbidden token is found and is inside the parenthesis.
                    if (null !== $forbiddenTokenIndex && $forbiddenTokenIndex < $blockEndIndex) {
                        continue;
                    }
                }

                if ($tokens[$blockStartIndex - 1]->isWhitespace() || $tokens[$blockStartIndex - 1]->isComment()) {
                    $tokens->clearTokenAndMergeSurroundingWhitespace($blockStartIndex);
                } else {
                    // Adds a space to prevent broken code like `return2`.
                    $tokens[$blockStartIndex] = new Token([T_WHITESPACE, ' ']);
                }

                $tokens->clearTokenAndMergeSurroundingWhitespace($blockEndIndex);
            }
        }
    }
protected function applyFix ( \ SplFileInfo $ file , Tokens $ tokens ) { // Checks if specific statements are set and uses them in this case. $ loops = array_intersect_key ( self :: $ loops , array_flip ( $ this -> configuration [ 'statements' ] ) ) ; foreach ( $ tokens as $ index => $ token ) { if ( ! $ token -> equalsAny ( [ '(' , [ CT :: T_BRACE_CLASS_INSTANTIATION_OPEN ] ] ) ) { continue ; } $ blockStartIndex = $ index ; $ index = $ tokens -> getPrevMeaningfulToken ( $ index ) ; $ prevToken = $ tokens [ $ index ] ; foreach ( $ loops as $ loop ) { if ( ! $ prevToken -> isGivenKind ( $ loop [ 'lookupTokens' ] ) ) { continue ; } $ blockEndIndex = $ tokens -> findBlockEnd ( $ token -> equals ( '(' ) ? Tokens :: BLOCK_TYPE_PARENTHESIS_BRACE : Tokens :: BLOCK_TYPE_BRACE_CLASS_INSTANTIATION , $ blockStartIndex ) ; $ blockEndNextIndex = $ tokens -> getNextMeaningfulToken ( $ blockEndIndex ) ; if ( ! $ tokens [ $ blockEndNextIndex ] -> equalsAny ( $ loop [ 'neededSuccessors' ] ) ) { continue ; } if ( \ array_key_exists ( 'forbiddenContents' , $ loop ) ) { $ forbiddenTokenIndex = $ tokens -> getNextTokenOfKind ( $ blockStartIndex , $ loop [ 'forbiddenContents' ] ) ; // A forbidden token is found and is inside the parenthesis. if ( null !== $ forbiddenTokenIndex && $ forbiddenTokenIndex < $ blockEndIndex ) { continue ; } } if ( $ tokens [ $ blockStartIndex - 1 ] -> isWhitespace ( ) || $ tokens [ $ blockStartIndex - 1 ] -> isComment ( ) ) { $ tokens -> clearTokenAndMergeSurroundingWhitespace ( $ blockStartIndex ) ; } else { // Adds a space to prevent broken code like `return2`. $ tokens [ $ blockStartIndex ] = new Token ( [ T_WHITESPACE , ' ' ] ) ; } $ tokens -> clearTokenAndMergeSurroundingWhitespace ( $ blockEndIndex ) ; } } }
{@inheritdoc}
{