/** * @return Parameter * A parameter built from a node * * @see \Phan\Deprecated\Pass1::node_param * Formerly `function node_param` */ 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 $type = UnionType::fromSimpleNode($context, $node->children['type']); $comment = Comment::fromStringInContext($node->docComment ?? '', $context); // Create the skeleton parameter from what we know so far $parameter = new Parameter($context, (string) $node->children['name'], $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 || $default_node->kind == \ast\AST_CONST || $default_node->kind == \ast\AST_UNARY_OP || $default_node->kind == \ast\AST_ARRAY) { // Set the default value $parameter->setDefaultValue($node->children['default'], UnionType::fromNode($context, $code_base, $node->children['default'])); } else { // Nodes here may be of type \ast\AST_CLASS_CONST // which we can't figure out during the first // parsing pass $parameter->setDefaultValue(null, NullType::instance()->asUnionType()); } } return $parameter; }
/** * @param Method $method * Get a list of methods hydrated with type information * for the given partial method * * @param CodeBase $code_base * The global code base holding all state * * @return Method[] * A list of typed methods based on the given method */ private static function methodListFromMethod(Method $method, CodeBase $code_base) : array { // See if we have any type information for this // internal function $map_list = UnionType::internalFunctionSignatureMapForFQSEN($method->getFQSEN()); if (!$map_list) { return [$method]; } $alternate_id = 0; return array_map(function ($map) use($method, &$alternate_id) : Method { $alternate_method = clone $method; $alternate_method->setFQSEN($alternate_method->getFQSEN()->withAlternateId($alternate_id++)); // Set the return type if one is defined if (!empty($map['return_type'])) { $alternate_method->setUnionType($map['return_type']); } // Load properties if defined foreach ($map['property_name_type_map'] ?? [] as $parameter_name => $parameter_type) { $flags = 0; $is_optional = false; // Check to see if its a pass-by-reference parameter if (strpos($parameter_name, '&') === 0) { $flags |= \ast\flags\PARAM_REF; $parameter_name = substr($parameter_name, 1); } // Check to see if its variadic if (strpos($parameter_name, '...') !== false) { $flags |= \ast\flags\PARAM_VARIADIC; $parameter_name = str_replace('...', '', $parameter_name); } // Check to see if its an optional parameter if (strpos($parameter_name, '=') !== false) { $is_optional = true; $parameter_name = str_replace('=', '', $parameter_name); } $parameter = new Parameter($method->getContext(), $parameter_name, $parameter_type, $flags); if ($is_optional) { $parameter->setDefaultValue(null, NullType::instance()->asUnionType()); } // Add the parameter $alternate_method->parameter_list[] = $parameter; } return $alternate_method; }, $map_list); }
/** * @return Parameter * A parameter built from a node * * @see \Phan\Deprecated\Pass1::node_param * Formerly `function node_param` */ 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; }