Phan\Analysis::parseNodeInContext PHP Method

parseNodeInContext() public static method

Parse the given node in the given context populating the code base within the context as a side effect. The returned context is the new context from within the given node.
public static parseNodeInContext ( CodeBase $code_base, Context $context, ast\Node $node ) : Context
$code_base CodeBase The global code base in which we store all state
$context Phan\Language\Context The context in which this node exists
$node ast\Node A node to parse and scan for errors
return Phan\Language\Context The context from within the node is returned
    public static function parseNodeInContext(CodeBase $code_base, Context $context, Node $node) : Context
    {
        // Save a reference to the outer context
        $outer_context = $context;
        // Visit the given node populating the code base
        // with anything we learn and get a new context
        // indicating the state of the world within the
        // given node
        $context = (new ParseVisitor($code_base, $context->withLineNumberStart($node->lineno ?? 0)))($node);
        assert(!empty($context), 'Context cannot be null');
        // Recurse into each child node
        $child_context = $context;
        foreach ($node->children ?? [] as $child_node) {
            // Skip any non Node children.
            if (!$child_node instanceof Node) {
                continue;
            }
            if (!self::shouldVisit($child_node)) {
                $child_context->withLineNumberStart($child_node->lineno ?? 0);
                continue;
            }
            // Step into each child node and get an
            // updated context for the node
            $child_context = self::parseNodeInContext($code_base, $child_context, $child_node);
            assert(!empty($child_context), 'Context cannot be null');
        }
        // For closed context elements (that have an inner scope)
        // return the outer context instead of their inner context
        // after we finish parsing their children.
        if (in_array($node->kind, [\ast\AST_CLASS, \ast\AST_METHOD, \ast\AST_FUNC_DECL, \ast\AST_CLOSURE])) {
            return $outer_context;
        }
        // Pass the context back up to our parent
        return $context;
    }

Same methods

Analysis::parseNodeInContext ( CodeBase $code_base, Context $context, ast\Node $node ) : Context

Usage Example

Example #1
0
 /**
  * Get a Context after parsing the given
  * bit of code.
  */
 private function contextForCode(string $code_stub) : Context
 {
     return Analysis::parseNodeInContext($this->code_base, new Context(), \ast\parse_code('<?php ' . $code_stub, Config::get()->ast_version));
 }