Add stringable

This commit is contained in:
Daniil Gentili 2020-08-14 20:58:05 +02:00
parent 653f76862d
commit 47562e4745
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7

View File

@ -75,6 +75,7 @@ class TypeHintStripper extends Plugin
case 'int': case 'int':
case 'object': case 'object':
case 'string': case 'string':
case 'resource':
case 'null': case 'null':
$stringType = new String_($typeName === 'callable' ? $stringType = new String_($typeName === 'callable' ?
$typeName : $typeName :
@ -98,7 +99,11 @@ class TypeHintStripper extends Plugin
} else { } else {
$noOopTypes = false; $noOopTypes = false;
$stringType = new String_("an instance of ".$type->toString()); $stringType = new String_("an instance of ".$type->toString());
$type = new Instanceof_($var, $type); if ($type->toString() === 'Stringable' || $type->toString() === \Stringable::class) {
$type = Plugin::callPoly("is_stringable", $var);
} else {
$type = new Instanceof_($var, $type);
}
} }
} }
if (\count($typeNames) > 1) { if (\count($typeNames) > 1) {
@ -233,11 +238,15 @@ class TypeHintStripper extends Plugin
{ {
if (\is_object($object)) { if (\is_object($object)) {
$type = \get_class($object); $type = \get_class($object);
return str_starts_with($type, 'class@anonymous') ? 'instance of class@anonymous' : "instance of $type"; return \str_starts_with($type, 'class@anonymous') ? 'instance of class@anonymous' : "instance of $type";
} }
return \gettype($object); return \gettype($object);
} }
public static function is_stringable($string): bool
{
return is_string($string) || (is_object($string) && method_exists($string, '__toString'));
}
public static function runWithAfter(): array public static function runWithAfter(): array
{ {
return [StringConcatOptimizer::class]; return [StringConcatOptimizer::class];