LightnCandy\Validator::spacing PHP Method

spacing() protected static method

Modify $token when spacing rules matched.
protected static spacing ( array &$token, array\arraystring | integer> &$context, boolean $nost = false ) : string | null
$token array
$context array\arraystring | integer>
$nost boolean do not do stand alone logic
return string | null Return compiled code segment for the token
    protected static function spacing(&$token, &$context, $nost = false)
    {
        // left line change detection
        $lsp = preg_match('/^(.*)(\\r?\\n)([ \\t]*?)$/s', $token[Token::POS_LSPACE], $lmatch);
        $ind = $lsp ? $lmatch[3] : $token[Token::POS_LSPACE];
        // right line change detection
        $rsp = preg_match('/^([ \\t]*?)(\\r?\\n)(.*)$/s', $token[Token::POS_RSPACE], $rmatch);
        $st = true;
        // setup ahead flag
        $ahead = $context['tokens']['ahead'];
        $context['tokens']['ahead'] = preg_match('/^[^\\n]*{{/s', $token[Token::POS_RSPACE] . $token[Token::POS_ROTHER]);
        // reset partial indent
        $context['tokens']['partialind'] = '';
        // same tags in the same line , not standalone
        if (!$lsp && $ahead) {
            $st = false;
        }
        if ($nost) {
            $st = false;
        }
        // not standalone because other things in the same line ahead
        if ($token[Token::POS_LOTHER] && !$token[Token::POS_LSPACE]) {
            $st = false;
        }
        // not standalone because other things in the same line behind
        if ($token[Token::POS_ROTHER] && !$token[Token::POS_RSPACE]) {
            $st = false;
        }
        if ($st && ($lsp && $rsp || $rsp && !$token[Token::POS_LOTHER] || $lsp && !$token[Token::POS_ROTHER])) {
            // handle partial
            if ($token[Token::POS_OP] === '>') {
                if (!$context['flags']['noind']) {
                    $context['tokens']['partialind'] = $token[Token::POS_LSPACECTL] ? '' : $ind;
                    $token[Token::POS_LSPACE] = isset($lmatch[2]) ? $lmatch[1] . $lmatch[2] : '';
                }
            } else {
                $token[Token::POS_LSPACE] = isset($lmatch[2]) ? $lmatch[1] . $lmatch[2] : '';
            }
            $token[Token::POS_RSPACE] = isset($rmatch[3]) ? $rmatch[3] : '';
        }
        // Handle space control.
        if ($token[Token::POS_LSPACECTL]) {
            $token[Token::POS_LSPACE] = '';
        }
        if ($token[Token::POS_RSPACECTL]) {
            $token[Token::POS_RSPACE] = '';
        }
    }