public static fromNodeInContext ( ast\Node $node, |
||
$node | ast\Node | An AST_VAR node |
$context | The context in which the variable is found | |
$code_base | ||
$should_check_type | boolean | |
return | A variable begotten from a node |
/** * @param Node $node * A node to parse * * @return Context * A new or an unchanged context resulting from * parsing the node */ public function visitVar(Node $node) : Context { $variable_name = (new ContextNode($this->code_base, $this->context, $node))->getVariableName(); // Check to see if the variable already exists if ($this->context->getScope()->hasVariableWithName($variable_name)) { $variable = $this->context->getScope()->getVariableWithName($variable_name); // If we're assigning to an array element then we don't // know what the constitutation of the parameter is // outside of the scope of this assignment, so we add to // its union type rather than replace it. if ($this->is_dim_assignment) { $variable->getUnionType()->addUnionType($this->right_type); } else { // If the variable isn't a pass-by-reference paramter // we clone it so as to not disturb its previous types // as we replace it. if ($variable instanceof Parameter) { if ($variable->isPassByReference()) { } else { $variable = clone $variable; } } else { $variable = clone $variable; } $variable->setUnionType($this->right_type); } $this->context->addScopeVariable($variable); return $this->context; } $variable = Variable::fromNodeInContext($this->assignment_node, $this->context, $this->code_base, false); // Set that type on the variable $variable->getUnionType()->addUnionType($this->right_type); // Note that we're not creating a new scope, just // adding variables to the existing scope $this->context->addScopeVariable($variable); return $this->context; }
/** * @return Variable * A variable in scope or a new variable * * @throws NodeException * An exception is thrown if we can't understand the node */ public function getOrCreateVariable() : Variable { try { return $this->getVariable(); } catch (IssueException $exception) { // Swallow it } // Create a new variable $variable = Variable::fromNodeInContext($this->node, $this->context, $this->code_base, false); $this->context->addScopeVariable($variable); return $variable; }
/** * @param Node $node * A node to parse * * @return Context * A new or an unchanged context resulting from * parsing the node */ public function visitStatic(Node $node) : Context { $variable = Variable::fromNodeInContext($node->children['var'], $this->context, $this->code_base, false); // If the element has a default, set its type // on the variable if (isset($node->children['default'])) { $default_type = UnionType::fromNode($this->context, $this->code_base, $node->children['default']); $variable->setUnionType($default_type); } // Note that we're not creating a new scope, just // adding variables to the existing scope $this->context->addScopeVariable($variable); return $this->context; }
/** * @param Node $node * A node that has a reference to a variable * * @param Context $context * The context in which we found the reference * * @param CodeBase $code_base * * @return Variable * A variable in scope or a new variable * * @throws NodeException * An exception is thrown if we can't understand the node */ public static function getOrCreateVariableFromNodeInContext(Node $node, Context $context, CodeBase $code_base) : Variable { // Get the name of the variable $variable_name = self::variableName($node); if (empty($variable_name)) { throw new NodeException($node, "Variable name not found"); } // Check to see if the variable exists in this scope if ($context->getScope()->hasVariableWithName($variable_name)) { return $context->getScope()->getVariableWithName($variable_name); } // Create a new variable $variable = Variable::fromNodeInContext($node, $context, $code_base, false); $context->addScopeVariable($variable); return $variable; }
/** * @param Node $node * A node to parse * * @return Context * A new or an unchanged context resulting from * parsing the node */ public function visitCatch(Node $node) : Context { try { $union_type = UnionTypeVisitor::unionTypeFromClassNode($this->code_base, $this->context, $node->children['class']); $class_list = (new ContextNode($this->code_base, $this->context, $node->children['class']))->getClassList(); } catch (CodeBaseException $exception) { Issue::emit(Issue::UndeclaredClassCatch, $this->context->getFile(), $node->lineno ?? 0, (string) $exception->getFQSEN()); } $variable_name = (new ContextNode($this->code_base, $this->context, $node->children['var']))->getVariableName(); if (!empty($variable_name)) { $variable = Variable::fromNodeInContext($node->children['var'], $this->context, $this->code_base, false); if (!$union_type->isEmpty()) { $variable->setUnionType($union_type); } $this->context->addScopeVariable($variable); } return $this->context; }
/** * @param Node $node * A node to parse * * @return Context * A new or an unchanged context resulting from * parsing the node */ public function visitCatch(Node $node) : Context { try { $union_type = UnionTypeVisitor::unionTypeFromClassNode($this->code_base, $this->context, $node->children['class']); $class_list = AST::classListFromNodeInContext($this->code_base, $this->context, $node->children['class']); } catch (CodeBaseException $exception) { Log::err(Log::EUNDEF, "catching undeclared class {$exception->getFQSEN()}", $this->context->getFile(), $node->lineno); } $variable_name = AST::variableName($node->children['var']); if (!empty($variable_name)) { $variable = Variable::fromNodeInContext($node->children['var'], $this->context, $this->code_base, false); if (!$union_type->isEmpty()) { $variable->setUnionType($union_type); } $this->context->addScopeVariable($variable); } return $this->context; }