Latte\PhpWriter::shortTernaryPass PHP Method

shortTernaryPass() public method

Simplified ternary expressions without third part.
public shortTernaryPass ( MacroTokens $tokens ) : MacroTokens
$tokens MacroTokens
return MacroTokens
    public function shortTernaryPass(MacroTokens $tokens)
    {
        $res = new MacroTokens();
        $inTernary = [];
        while ($tokens->nextToken()) {
            if ($tokens->isCurrent('?')) {
                $inTernary[] = $tokens->depth;
            } elseif ($tokens->isCurrent(':')) {
                array_pop($inTernary);
            } elseif ($tokens->isCurrent(',', ')', ']', '|') && end($inTernary) === $tokens->depth + $tokens->isCurrent(')', ']')) {
                $res->append(' : NULL');
                array_pop($inTernary);
            }
            $res->append($tokens->currentToken());
        }
        if ($inTernary) {
            $res->append(' : NULL');
        }
        return $res;
    }