Phan\Language\Type::canCastToType PHP Method

canCastToType() public method

public canCastToType ( Type $type ) : boolean
$type Type
return boolean 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);
        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 === 'int' && $d === 'float') {
            return true;
            // int->float is ok
        }
        if (($s === 'array' || $s === 'string' || strpos($s, '[]') !== false || $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 (strpos($s, '[]') !== false && ($d == 'array' || $d == '\\ArrayAccess')) {
            return true;
        }
        if (strpos($d, '[]') !== false && $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;
    }