/**
* @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 = (string) $this;
$d = (string) $type;
if ($s[0] == '\\') {
$s = substr($s, 1);
}
if ($d[0] == '\\') {
$d = substr($d, 1);
}
if ($s === $d) {
return true;
}
if ($s === 'int' && $d === 'float') {
return true;
// int->float is ok
}
if (($s === 'array' || $s === 'string' || strpos($s, '[]') !== false) && $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') {
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;
}