src/Fixer/Operator/TernaryOperatorSpacesFixer.php
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
    {
        $ternaryLevel = 0;

        foreach ($tokens as $index => $token) {
            if ($token->equals('?')) {
                ++$ternaryLevel;

                $nextNonWhitespaceIndex = $tokens->getNextNonWhitespace($index);
                $nextNonWhitespaceToken = $tokens[$nextNonWhitespaceIndex];

                if ($nextNonWhitespaceToken->equals(':')) {
                    // for `$a ?: $b` remove spaces between `?` and `:`
                    if ($tokens[$index + 1]->isWhitespace()) {
                        $tokens->clearAt($index + 1);
                    }
                } else {
                    // for `$a ? $b : $c` ensure space after `?`
                    $this->ensureWhitespaceExistence($tokens, $index + 1, true);
                }

                // for `$a ? $b : $c` ensure space before `?`
                $this->ensureWhitespaceExistence($tokens, $index - 1, false);

                continue;
            }

            if ($ternaryLevel && $token->equals(':')) {
                // for `$a ? $b : $c` ensure space after `:`
                $this->ensureWhitespaceExistence($tokens, $index + 1, true);

                $prevNonWhitespaceToken = $tokens[$tokens->getPrevNonWhitespace($index)];

                if (!$prevNonWhitespaceToken->equals('?')) {
                    // for `$a ? $b : $c` ensure space before `:`
                    $this->ensureWhitespaceExistence($tokens, $index - 1, false);
                }

                --$ternaryLevel;
            }
        }
    }
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
    {
        $ternaryLevel = 0;

        foreach ($tokens as $index => $token) {
            if ($token->equals('?')) {
                ++$ternaryLevel;

                $nextNonWhitespaceIndex = $tokens->getNextNonWhitespace($index);
                $nextNonWhitespaceToken = $tokens[$nextNonWhitespaceIndex];

                if ($nextNonWhitespaceToken->equals(':')) {
                    // for `$a ?: $b` remove spaces between `?` and `:`
                    if ($tokens[$index + 1]->isWhitespace()) {
                        $tokens->clearAt($index + 1);
                    }
                } else {
                    // for `$a ? $b : $c` ensure space after `?`
                    $this->ensureWhitespaceExistence($tokens, $index + 1, true);
                }

                // for `$a ? $b : $c` ensure space before `?`
                $this->ensureWhitespaceExistence($tokens, $index - 1, false);

                continue;
            }

            if ($ternaryLevel && $token->equals(':')) {
                // for `$a ? $b : $c` ensure space after `:`
                $this->ensureWhitespaceExistence($tokens, $index + 1, true);

                $prevNonWhitespaceToken = $tokens[$tokens->getPrevNonWhitespace($index)];

                if (!$prevNonWhitespaceToken->equals('?')) {
                    // for `$a ? $b : $c` ensure space before `:`
                    $this->ensureWhitespaceExistence($tokens, $index - 1, false);
                }

                --$ternaryLevel;
            }
        }
    }
protected function applyFix ( \ SplFileInfo $ file , Tokens $ tokens ) { $ ternaryLevel = 0 ; foreach ( $ tokens as $ index => $ token ) { if ( $ token -> equals ( '?' ) ) { ++ $ ternaryLevel ; $ nextNonWhitespaceIndex = $ tokens -> getNextNonWhitespace ( $ index ) ; $ nextNonWhitespaceToken = $ tokens [ $ nextNonWhitespaceIndex ] ; if ( $ nextNonWhitespaceToken -> equals ( ':' ) ) { // for `$a ?: $b` remove spaces between `?` and `:` if ( $ tokens [ $ index + 1 ] -> isWhitespace ( ) ) { $ tokens -> clearAt ( $ index + 1 ) ; } } else { // for `$a ? $b : $c` ensure space after `?` $ this -> ensureWhitespaceExistence ( $ tokens , $ index + 1 , true ) ; } // for `$a ? $b : $c` ensure space before `?` $ this -> ensureWhitespaceExistence ( $ tokens , $ index - 1 , false ) ; continue ; } if ( $ ternaryLevel && $ token -> equals ( ':' ) ) { // for `$a ? $b : $c` ensure space after `:` $ this -> ensureWhitespaceExistence ( $ tokens , $ index + 1 , true ) ; $ prevNonWhitespaceToken = $ tokens [ $ tokens -> getPrevNonWhitespace ( $ index ) ] ; if ( ! $ prevNonWhitespaceToken -> equals ( '?' ) ) { // for `$a ? $b : $c` ensure space before `:` $ this -> ensureWhitespaceExistence ( $ tokens , $ index - 1 , false ) ; } -- $ ternaryLevel ; } } }
{@inheritdoc}
{