Phan\Language\UnionType::internalFunctionSignatureMapForFQSEN PHP Method

internalFunctionSignatureMapForFQSEN() public static method

A list of types for parameters associated with the given builtin function with the given name
See also: internal_varargs_check Formerly `function internal_varargs_check`
public static internalFunctionSignatureMapForFQSEN ( FullyQualifiedMethodName | FullyQualifiedFunctionName $function_fqsen ) : array
$function_fqsen Phan\Language\FQSEN\FullyQualifiedMethodName | Phan\Language\FQSEN\FullyQualifiedFunctionName
return array
    public static function internalFunctionSignatureMapForFQSEN($function_fqsen) : array
    {
        $context = new Context();
        $map = self::internalFunctionSignatureMap();
        if ($function_fqsen instanceof FullyQualifiedMethodName) {
            $class_fqsen = $function_fqsen->getFullyQualifiedClassName();
            $class_name = $class_fqsen->getName();
            $function_name = $class_name . '::' . $function_fqsen->getName();
        } else {
            $function_name = $function_fqsen->getName();
        }
        $function_name = strtolower($function_name);
        $function_name_original = $function_name;
        $alternate_id = 0;
        $configurations = [];
        while (isset($map[$function_name])) {
            // Get some static data about the function
            $type_name_struct = $map[$function_name];
            if (empty($type_name_struct)) {
                continue;
            }
            // Figure out the return type
            $return_type_name = array_shift($type_name_struct);
            $return_type = $return_type_name ? UnionType::fromStringInContext($return_type_name, $context) : null;
            $name_type_name_map = $type_name_struct;
            $property_name_type_map = [];
            foreach ($name_type_name_map as $name => $type_name) {
                $property_name_type_map[$name] = empty($type_name) ? new UnionType() : UnionType::fromStringInContext($type_name, $context);
            }
            $configurations[] = ['return_type' => $return_type, 'property_name_type_map' => $property_name_type_map];
            $function_name = $function_name_original . '\'' . ++$alternate_id;
        }
        return $configurations;
    }

Usage Example

Esempio n. 1
0
 /**
  * @param FunctionInterface $function
  * 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 functionListFromFunction(FunctionInterface $function, CodeBase $code_base) : array
 {
     // See if we have any type information for this
     // internal function
     $map_list = UnionType::internalFunctionSignatureMapForFQSEN($function->getFQSEN());
     if (!$map_list) {
         return [$function];
     }
     $alternate_id = 0;
     return array_map(function ($map) use($function, &$alternate_id) : FunctionInterface {
         $alternate_function = clone $function;
         $alternate_function->setFQSEN($alternate_function->getFQSEN()->withAlternateId($alternate_id++));
         // Set the return type if one is defined
         if (!empty($map['return_type'])) {
             $alternate_function->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($function->getContext(), $parameter_name, $parameter_type, $flags);
             if ($is_optional) {
                 $parameter->setDefaultValueType(NullType::instance()->asUnionType());
             }
             // Add the parameter
             $alternate_function->appendParameter($parameter);
         }
         $alternate_function->setNumberOfRequiredParameters(array_reduce($alternate_function->getParameterList(), function (int $carry, Parameter $parameter) : int {
             return $carry + ($parameter->isOptional() ? 0 : 1);
         }, 0));
         $alternate_function->setNumberOfOptionalParameters(count($alternate_function->getParameterList()) - $alternate_function->getNumberOfRequiredParameters());
         if ($alternate_function instanceof Method) {
             if ($alternate_function->getIsMagicCall() || $alternate_function->getIsMagicCallStatic()) {
                 $alternate_function->setNumberOfOptionalParameters(999);
                 $alternate_function->setNumberOfRequiredParameters(0);
             }
         }
         return $alternate_function;
     }, $map_list);
 }
All Usage Examples Of Phan\Language\UnionType::internalFunctionSignatureMapForFQSEN