Phan\Language\Element\Parameter::fromNode PHP Метод

fromNode() публичный статический Метод

См. также: Phan\Deprecated\Pass1::node_param Formerly `function node_param`
public static fromNode ( Context $context, CodeBase $code_base, ast\Node $node ) : Parameter
$context Phan\Language\Context
$code_base Phan\CodeBase
$node ast\Node
Результат Parameter A parameter built from a node
    public static function fromNode(Context $context, CodeBase $code_base, Node $node) : Parameter
    {
        assert($node instanceof Node, "node was not an \\ast\\Node");
        // Get the type of the parameter
        $union_type = UnionType::fromNode($context, $code_base, $node->children['type']);
        // Create the skeleton parameter from what we know so far
        $parameter = new Parameter($context, (string) $node->children['name'], $union_type, $node->flags ?? 0);
        // If there is a default value, store it and its type
        if (($default_node = $node->children['default']) !== null) {
            // We can't figure out default values during the
            // parsing phase, unfortunately
            if (!$default_node instanceof Node) {
                // Get the type of the default
                $union_type = UnionType::fromNode($context, $code_base, $default_node);
                // Set the default value
                $parameter->setDefaultValueType($union_type);
                // Set the actual value of the default
                $parameter->setDefaultValue($default_node);
            } else {
                try {
                    // Get the type of the default
                    $union_type = UnionType::fromNode($context, $code_base, $default_node, false);
                } catch (IssueException $exception) {
                    if ($default_node instanceof Node && $default_node->kind === \ast\AST_ARRAY) {
                        $union_type = new UnionType([ArrayType::instance()]);
                    } else {
                        // If we're in the parsing phase and we
                        // depend on a constant that isn't yet
                        // defined, give up and set it to
                        // bool|float|int|string to avoid having
                        // to handle a future type.
                        $union_type = new UnionType([BoolType::instance(), FloatType::instance(), IntType::instance(), StringType::instance()]);
                    }
                }
                // Set the default value
                $parameter->setDefaultValueType($union_type);
                // Set the actual value of the default
                $parameter->setDefaultValue($default_node);
            }
        }
        return $parameter;
    }

Usage Example

Пример #1
0
 /**
  * @return Parameter[]
  * A list of parameters from an AST node.
  *
  * @see \Phan\Deprecated\Pass1::node_paramlist
  * Formerly `function node_paramlist`
  */
 public static function listFromNode(Context $context, CodeBase $code_base, Node $node) : array
 {
     assert($node instanceof Node, "node was not an \\ast\\Node");
     $parameter_list = [];
     $is_optional_seen = false;
     foreach ($node->children ?? [] as $i => $child_node) {
         $parameter = Parameter::fromNode($context, $code_base, $child_node);
         if (!$parameter->isOptional() && $is_optional_seen) {
             Issue::maybeEmit($code_base, $context, Issue::ParamReqAfterOpt, $node->lineno ?? 0);
         } elseif ($parameter->isOptional() && !$is_optional_seen && $parameter->getVariadicElementUnionType()->isEmpty()) {
             $is_optional_seen = true;
         }
         $parameter_list[] = $parameter;
     }
     return $parameter_list;
 }
All Usage Examples Of Phan\Language\Element\Parameter::fromNode