Phan\Language\Type::genericArrayElementType PHP Method

genericArrayElementType() public method

public genericArrayElementType ( ) : Type
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 = strrpos($this->getName(), '[]')) !== false) {
            assert($this->getName() !== '[]' && $this->getName() !== 'array', "Non-generic type requested to be non-generic");
            return Type::make($this->getNamespace(), substr($this->getName(), 0, $pos), $this->template_parameter_type_list);
        }
        return $this;
    }

Usage Example

Example #1
0
File: Type.php Project: etsy/phan
 /**
  * @return bool
  * True if this Type can be cast to the given Type
  * cleanly
  */
 public function canCastToType(Type $type) : bool
 {
     if ($this === $type) {
         return true;
     }
     $s = strtolower((string) $this);
     $d = strtolower((string) $type);
     $s_is_generic_array = $this->isGenericArray();
     $d_is_generic_array = $type->isGenericArray();
     if ($s[0] == '\\') {
         $s = substr($s, 1);
     }
     if ($d[0] == '\\') {
         $d = substr($d, 1);
     }
     if ($s === $d) {
         return true;
     }
     if (Config::get()->scalar_implicit_cast) {
         if ($type->isScalar() && $this->isScalar()) {
             return true;
         }
     }
     if ($s_is_generic_array && $d_is_generic_array) {
         return $this->genericArrayElementType()->canCastToType($type->genericArrayElementType());
     }
     if ($s === 'int' && $d === 'float') {
         return true;
         // int->float is ok
     }
     if (($s === 'array' || $s === 'string' || $s_is_generic_array || $s === 'closure') && $d === 'callable') {
         return true;
     }
     if ($s === 'object' && !$type->isScalar() && $d !== 'array') {
         return true;
     }
     if ($d === 'object' && !$this->isScalar() && $s !== 'array') {
         return true;
     }
     if ($s_is_generic_array && ($d == 'array' || $d == 'arrayaccess')) {
         return true;
     }
     if ($d_is_generic_array && $s === 'array') {
         return true;
     }
     if ($s === 'callable' && $d === 'closure') {
         return true;
     }
     if (($pos = strrpos($d, '\\')) !== false) {
         if ('\\' !== $this->getNamespace()) {
             if (trim($this->getNamespace() . '\\' . $s, '\\') == $d) {
                 return true;
             }
         } else {
             if (substr($d, $pos + 1) === $s) {
                 return true;
                 // Lazy hack, but...
             }
         }
     }
     if (($pos = strrpos($s, '\\')) !== false) {
         if ('\\' !== $type->getNamespace()) {
             if (trim($type->getNamespace() . '\\' . $d, '\\') == $s) {
                 return true;
             }
         } else {
             if (substr($s, $pos + 1) === $d) {
                 return true;
                 // Lazy hack, but...
             }
         }
     }
     return false;
 }