Patchwork\CodeManipulation\Source::siblings PHP Method

siblings() public method

public siblings ( $types, $offset )
    function siblings($types, $offset)
    {
        $level = $this->levels[$offset];
        $begin = Utils\lastNotGreaterThan(Utils\access($this->levelBeginnings, $level, []), $offset);
        $end = Utils\firstGreaterThan(Utils\access($this->levelEndings, $level, []), $offset);
        if ($types === self::ANY) {
            return Utils\allWithinRange($this->tokensByLevel[$level], $begin, $end);
        } else {
            $result = [];
            foreach ((array) $types as $type) {
                $candidates = Utils\access($this->tokensByLevelAndType, [$level, $type], []);
                $result = array_merge(Utils\allWithinRange($candidates, $begin, $end), $result);
            }
            return $result;
        }
    }

Usage Example

Example #1
0
function collectUseDeclarations(Source $s, $begin)
{
    $result = ['class' => [], 'function' => [], 'const' => []];
    # only tokens that are siblings bracket-wise are considered,
    # so trait-use instances are not an issue
    foreach ($s->siblings(T_USE, $begin) as $keyword) {
        # skip if closure-use
        $next = $s->skip(Source::junk(), $keyword);
        if ($s->is(Generic\LEFT_ROUND, $next)) {
            continue;
        }
        parseUseDeclaration($s, $next, $result);
    }
    return $result;
}