Phan\Language\Type::make PHP Method

make() protected static method

protected static make ( string $namespace, string $type_name, UnionType[] $template_parameter_type_list ) : Type
$namespace string The (optional) namespace of the type such as '\' or '\Phan\Language'.
$type_name string
$template_parameter_type_list UnionType[] A (possibly empty) list of template parameter types
return Type A single canonical instance of the given type.
    protected static function make(string $namespace, string $type_name, $template_parameter_type_list) : Type
    {
        $namespace = trim($namespace);
        if ('\\' === $namespace) {
            $type_name = self::canonicalNameFromName($type_name);
        }
        // If this looks like a generic type string, explicitly
        // make it as such
        if (self::isGenericArrayString($type_name) && ($pos = strrpos($type_name, '[]')) !== false) {
            return GenericArrayType::fromElementType(Type::make($namespace, substr($type_name, 0, $pos), $template_parameter_type_list));
        }
        assert($namespace && 0 === strpos($namespace, '\\'), "Namespace must be fully qualified");
        assert(!empty($namespace), "Namespace cannot be empty");
        assert('\\' === $namespace[0], "Namespace must be fully qualified");
        assert(!empty($type_name), "Type name cannot be empty");
        assert(false === strpos($type_name, '|'), "Type name may not contain a pipe.");
        // Create a canonical representation of the
        // namespace and name
        $namespace = $namespace ?: '\\';
        if ('\\' === $namespace) {
            $type_name = self::canonicalNameFromName($type_name);
        }
        // Make sure we only ever create exactly one
        // object for any unique type
        static $canonical_object_map = [];
        $key = $namespace . '\\' . $type_name;
        if ($template_parameter_type_list) {
            $key .= '<' . implode(',', array_map(function (UnionType $union_type) {
                return (string) $union_type;
            }, $template_parameter_type_list)) . '>';
        }
        $key = strtolower($key);
        if (empty($canonical_object_map[$key])) {
            $canonical_object_map[$key] = new static($namespace, $type_name, $template_parameter_type_list);
        }
        return $canonical_object_map[$key];
    }

Same methods

Type::make ( string $namespace, string $type_name, UnionType[] $template_parameter_type_list ) : Type

Usage Example

Example #1
0
 /**
  * @return Type
  * A variation of this type that is not generic.
  * i.e. 'int[]' becomes 'int'.
  */
 public function genericArrayElementType() : Type
 {
     assert($this->isGenericArray(), "Cannot call genericArrayElementType on non-generic array");
     if (($pos = strpos($this->name, '[]')) !== false) {
         assert($this->name !== '[]' && $this->name !== 'array', "Non-generic type '{$this->name}' requested to be non-generic");
         return Type::make($this->getNamespace(), substr($this->name, 0, $pos));
     }
     return $this;
 }
All Usage Examples Of Phan\Language\Type::make