Phan\Language\Element\Variable::getUnionTypeOfHardcodedGlobalVariableWithName PHP Method

getUnionTypeOfHardcodedGlobalVariableWithName() public static method

public static getUnionTypeOfHardcodedGlobalVariableWithName ( string $name, Context $context ) : UnionType | null
$name string
$context Phan\Language\Context
return Phan\Language\UnionType | null Returns UnionType (Possible with empty set) if and only if isHardcodedGlobalVariableWithName is true. Returns null otherwise.
    public static function getUnionTypeOfHardcodedGlobalVariableWithName(string $name, Context $context)
    {
        if (array_key_exists($name, self::_BUILTIN_SUPERGLOBAL_TYPES)) {
            // More efficient than using context.
            return UnionType::fromFullyQualifiedString(self::_BUILTIN_SUPERGLOBAL_TYPES[$name]);
        }
        if (array_key_exists($name, Config::get()->globals_type_map) || in_array($name, Config::get()->runkit_superglobals)) {
            $type_string = Config::get()->globals_type_map[$name] ?? '';
            return UnionType::fromStringInContext($type_string, $context);
        }
        return null;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Visit a node with kind `\ast\AST_VAR`
  *
  * @param Node $node
  * A node of the type indicated by the method name that we'd
  * like to figure out the type that it produces.
  *
  * @return UnionType
  * The set of types that are possibly produced by the
  * given node
  */
 public function visitVar(Node $node) : UnionType
 {
     // $$var or ${...} (whose idea was that anyway?)
     if ($node->children['name'] instanceof Node && ($node->children['name']->kind == \ast\AST_VAR || $node->children['name']->kind == \ast\AST_BINARY_OP)) {
         return MixedType::instance()->asUnionType();
     }
     // This is nonsense. Give up.
     if ($node->children['name'] instanceof Node) {
         return new UnionType();
     }
     $variable_name = $node->children['name'];
     if (!$this->context->getScope()->hasVariableWithName($variable_name)) {
         if (Variable::isSuperglobalVariableWithName($variable_name)) {
             return Variable::getUnionTypeOfHardcodedGlobalVariableWithName($variable_name, $this->context);
         }
         if (!Config::get()->ignore_undeclared_variables_in_global_scope || !$this->context->isInGlobalScope()) {
             throw new IssueException(Issue::fromType(Issue::UndeclaredVariable)($this->context->getFile(), $node->lineno ?? 0, [$variable_name]));
         }
     } else {
         $variable = $this->context->getScope()->getVariableByName($variable_name);
         return $variable->getUnionType();
     }
     return new UnionType();
 }