/**
* @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;
}