예제 #1
0
파일: rst.php 프로젝트: shabbyrobe/amiss
function iter_lines($h)
{
    // 100 wasn't enough
    $cacheLines = 1000;
    $cache = [];
    $i = 0;
    while (!feof($h) && $i++ < $cacheLines) {
        $line = fgets($h);
        if ($line === false) {
            break;
        }
        $cache[] = $line;
    }
    $indent = indent_detect($cache);
    return [$indent, function () use($cache, $h) {
        foreach ($cache as $line) {
            (yield $line);
        }
        while (!feof($h)) {
            $line = fgets($h);
            if ($line === false) {
                break;
            }
            (yield $line);
        }
    }];
}
예제 #2
0
function comment_rewrite($code)
{
    global $parser;
    global $options;
    $nl = nl_detect($code);
    if ($options->crapExtractor) {
        $tokens = token_get_docblocks_manual($code);
    } else {
        $tokens = token_get_all($code);
    }
    $docComments = [];
    $prev = null;
    foreach ($tokens as $token) {
        $token = (array) $token;
        if (!isset($token[1])) {
            $token = [null, $token[0], null];
        }
        if ($token[0] == T_DOC_COMMENT) {
            $lastWsp = null;
            if ($prev && $prev[0] == T_WHITESPACE) {
                $lastWsp = $prev[1];
            }
            $docComments[] = [$token[1], $lastWsp];
        }
        $prev = $token;
    }
    foreach ($docComments as list($doc, $lastWsp)) {
        list($data, $delete) = $parser->parse($parser->stripDocComment($doc));
        $newDoc = $doc;
        foreach ($delete as $del) {
            $qdel = preg_quote($del, '~');
            $newDoc = preg_replace("~(/\\*\\*\\h*){$qdel}\\h*\n?~", '$1', $newDoc, 1, $aCount);
            $newDoc = preg_replace("~^\\h*\\*\\h*{$qdel}\\h*\n?~m", '', $newDoc, 1, $bCount);
            if ($del && !$aCount && !$bCount) {
                throw new RewriteException($del);
            }
        }
        if (preg_match("~^ /\\*\\* [\\s\\*]* \\*/ \$~x", trim($newDoc))) {
            $newDoc = '';
        }
        $newData = data_rebuild($data);
        if ($newData) {
            $pretty = true;
            if (isset($newData['field']) && count($newData['field']) == 1 && (is_string($newData['field']) || $newData['field'] === true)) {
                // {"field": true}, {"field": "name"}
                $pretty = false;
            } elseif (isset($newData['field']) && count($newData['field']) == 1 && (isset($newData['field']['index']) || isset($newData['field']['primary']))) {
                // {"field": {"index": true}}, {"field": {"primary": true}},
                $pretty = false;
            }
            $newNote = ":amiss = " . json_encode($newData, $pretty ? JSON_PRETTY_PRINT : null) . ";";
            $newNoteLined = " * " . implode("\n * ", explode("\n", $newNote)) . "\n";
            $indent = indent_detect($doc, $lastWsp);
            $newNoteLined = indent($newNoteLined, $indent, $nl);
            if (!$newDoc) {
                $newDoc = "/**\n{$newNoteLined} */";
            } else {
                // preg_replace requires backslashes to be escaped in the replacement pattern
                $newNoteReplace = str_replace("\\", "\\\\", $newNoteLined);
                $newDoc = preg_replace("~(\\*/\\s*)\$~", "*\n{$newNoteReplace} */", $newDoc);
            }
            // preg_replace requires backslashes to be escaped in the replacement pattern
            $newDocReplace = str_replace("\\", "\\\\", $newDoc);
            $code = preg_replace('~' . preg_quote($doc, '~') . '~', $newDocReplace, $code, 1, $count);
            if (!$count) {
                throw new RewriteException();
            }
        }
    }
    return $code;
}