public function nonPrimitive() { Primitive::forName('lang.Object'); }
/** * Gets a type for a given name * * Checks for: * - Primitive types (string, int, double, boolean, resource) * - Array and map notations (array, string[], string*, [:string]) * - Any type (var) * - Void type (void) * - Function types * - Generic notations (util.collections.HashTable<lang.types.String, lang.Generic>) * - Anything else will be passed to XPClass::forName() * * @param string $type * @return lang.Type * @throws lang.IllegalStateException if type is empty */ public static function forName($type) { static $primitives = ['string' => 'string', 'int' => 'int', 'integer' => 'int', 'double' => 'double', 'float' => 'double', 'bool' => 'bool', 'boolean' => 'bool', 'HH\\int' => 'int', 'HH\\string' => 'string', 'HH\\float' => 'double', 'HH\\bool' => 'bool']; if (0 === strlen($type)) { throw new IllegalStateException('Empty type'); } // Map well-known primitives, var and void, handle rest syntactically: // * T[] is an array // * [:T] is a map // * T* is a vararg // * T<K, V> is a generic // * D<K, V> is a generic type definition D with K and V components // except if any of K, V contains a ?, in which case it's a wild // card type. // * T1|T2 is a type union // * Anything else is a qualified or unqualified class name if (isset($primitives[$type])) { return Primitive::forName($primitives[$type]); } else { if ('var' === $type || 'resource' === $type || 'HH\\mixed' === $type) { return self::$VAR; } else { if ('void' === $type || 'HH\\void' === $type || 'HH\\noreturn' === $type) { return self::$VOID; } else { if ('array' === $type) { return self::$ARRAY; } else { if ('object' === $type) { return self::$OBJECT; } else { if ('callable' === $type) { return self::$CALLABLE; } else { if ('iterable' === $type) { return self::$ITERABLE; } else { if (0 === substr_compare($type, 'function(', 0, 9)) { return FunctionType::forName($type); } else { if (0 === substr_compare($type, '[]', -2)) { return new ArrayType(self::forName(substr($type, 0, -2))); } else { if (0 === substr_compare($type, '[:', 0, 2)) { return new MapType(self::forName(substr($type, 2, -1))); } else { if ('?' === $type[0] || '@' === $type[0]) { return self::forName(substr($type, 1)); } else { if ('(' === $type[0]) { return self::forName(substr($type, 1, -1)); } else { if (0 === substr_compare($type, '*', -1)) { return new ArrayType(self::forName(substr($type, 0, -1))); } else { if (strstr($type, '|')) { return TypeUnion::forName($type); } else { if ('HH\\num' === $type) { return new TypeUnion([Primitive::$INT, Primitive::$DOUBLE]); } else { if ('HH\\arraykey' === $type) { return new TypeUnion([Primitive::$INT, Primitive::$STRING]); } else { if (false === ($p = strpos($type, '<'))) { return XPClass::forName($type); } else { if (strstr($type, '?')) { return WildcardType::forName($type); } else { $base = substr($type, 0, $p); $components = self::forNames(substr($type, $p + 1, -1)); if ('array' === $base) { return 1 === sizeof($components) ? new ArrayType($components[0]) : new MapType($components[1]); } else { return XPClass::forName($base)->newGenericType($components); } } } } } } } } } } } } } } } } } } } }
public function cannot_cast_maps_to_primitives($name) { Primitive::forName($name)->cast(['one' => 'two']); }
/** * Gets a type for a given name * * Checks for: * - Primitive types (string, int, double, boolean, resource) * - Array and map notations (array, string[], string*, [:string]) * - Any type (var) * - Void type (void) * - Function types * - Generic notations (util.collections.HashTable<lang.types.String, lang.Generic>) * - Anything else will be passed to XPClass::forName() * * @param string $type * @return lang.Type * @throws lang.IllegalStateException if type is empty */ public static function forName($type) { static $primitives = ['string' => 'string', 'int' => 'int', 'integer' => 'int', 'double' => 'double', 'float' => 'double', 'bool' => 'bool', 'boolean' => 'bool']; if (0 === strlen($type)) { throw new IllegalStateException('Empty type'); } // Map well-known primitives, var and void, handle rest syntactically: // * T[] is an array // * [:T] is a map // * T* is a vararg // * T<K, V> is a generic // * D<K, V> is a generic type definition D with K and V components // except if any of K, V contains a ?, in which case it's a wild // card type. // * T1|T2 is a type union // * Anything else is a qualified or unqualified class name if (isset($primitives[$type])) { return Primitive::forName($primitives[$type]); } else { if ('var' === $type || 'resource' === $type) { return self::$VAR; } else { if ('void' === $type) { return self::$VOID; } else { if ('array' === $type) { return self::$ARRAY; } else { if ('callable' === $type) { return self::$CALLABLE; } else { if (0 === substr_compare($type, 'function(', 0, 9)) { return FunctionType::forName($type); } else { if (0 === substr_compare($type, '[]', -2)) { return new ArrayType(self::forName(substr($type, 0, -2))); } else { if (0 === substr_compare($type, '[:', 0, 2)) { return new MapType(self::forName(substr($type, 2, -1))); } else { if ('(' === $type[0]) { return self::forName(substr($type, 1, -1)); } else { if (0 === substr_compare($type, '*', -1)) { return new ArrayType(self::forName(substr($type, 0, -1))); } else { if (strstr($type, '|')) { return TypeUnion::forName($type); } else { if (false === ($p = strpos($type, '<'))) { $normalized = strtr($type, '\\', '.'); return strstr($normalized, '.') ? XPClass::forName($normalized) : new XPClass($normalized); } else { if (strstr($type, '?')) { return WildcardType::forName($type); } else { $base = substr($type, 0, $p); $components = self::forNames(substr($type, $p + 1, -1)); return cast(self::forName($base), 'lang.XPClass')->newGenericType($components); } } } } } } } } } } } } } }
/** * Gets a type for a given name * * Checks for: * <ul> * <li>Primitive types (string, integer, double, boolean, array)</li> * <li>Array notations (string[] or string*)</li> * <li>Resources</li> * <li>Any type (var or *)</li> * <li>Generic notations (util.collections.HashTable<lang.types.String, lang.Generic>)</li> * <li>Anything else will be passed to XPClass::forName()</li> * </ul> * * @param string name * @return lang.Type */ public static function forName($name) { static $deprecated = array('char' => 'string', 'integer' => 'int', 'boolean' => 'bool', 'float' => 'double', 'mixed' => 'var', '*' => 'var', 'array' => 'var[]', 'resource' => 'var'); static $primitives = array('string' => TRUE, 'int' => TRUE, 'double' => TRUE, 'bool' => TRUE); // Map deprecated type names $type = isset($deprecated[$name]) ? $deprecated[$name] : $name; // Map well-known primitives, var and void, handle rest syntactically: // * T[] is an array // * [:T] is a map // * T* is a vararg // * T<K, V> is a generic // * Anything else is a qualified or unqualified class name if (isset($primitives[$type])) { return Primitive::forName($type); } else { if ('var' === $type) { return self::$VAR; } else { if ('void' === $type) { return self::$VOID; return $type; } else { if ('[]' === substr($type, -2)) { return ArrayType::forName($type); } else { if ('[:' === substr($type, 0, 2)) { return MapType::forName($type); } else { if ('*' === substr($type, -1)) { return ArrayType::forName(substr($type, 0, -1) . '[]'); } else { if (FALSE === ($p = strpos($type, '<'))) { return strstr($type, '.') ? XPClass::forName($type) : new XPClass($type); } } } } } } } // Generics // * D<K, V> is a generic type definition D with K and V componenty // * Deprecated: array<T> is T[], array<K, V> is [:T] $base = substr($type, 0, $p); $components = self::forNames(substr($type, $p + 1, -1)); if ('array' !== $base) { return cast(self::forName($base), 'lang.XPClass')->newGenericType($components); } $s = sizeof($components); if (2 === $s) { return MapType::forName('[:' . $components[1]->name . ']'); } else { if (1 === $s) { return ArrayType::forName($components[0]->name . '[]'); } } throw new IllegalArgumentException('Unparseable name ' . $name); }