Skip to content

Commit a8abeae

Browse files
committed
test not stringable and bcmath
1 parent 61e0eea commit a8abeae

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Type/ObjectType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,14 @@ public function toArrayKey(): Type
707707
public function toCoercedArgumentType(bool $strictTypes): Type
708708
{
709709
if (!$strictTypes) {
710+
$classReflection = $this->getClassReflection();
711+
if (
712+
$classReflection === null
713+
|| !$classReflection->hasNativeMethod('__toString')
714+
) {
715+
return $this;
716+
}
717+
710718
return TypeCombinator::union($this, $this->toString());
711719
}
712720

tests/PHPStan/Analyser/nsrt/bug-12393b.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public function doFoo(): void {
306306
assertType('null', $this->foo);
307307

308308
$this->fooNonNull = $this->returnVoid();
309-
assertType('int|null', $this->foo); // should be *ERROR*
309+
assertType('int|null', $this->foo); // should be *NEVER*
310310
}
311311

312312
public function returnVoid(): void {
@@ -680,11 +680,24 @@ public function doFoo(StringableFoo $foo): void {
680680
$this->foo = $foo;
681681
assertType('string', $this->foo);
682682
}
683+
684+
public function doFoo2(NotStringable $foo): void {
685+
$this->foo = $foo;
686+
assertType('*NEVER*', $this->foo);
687+
}
688+
689+
public function doFoo3(\BcMath\Number $foo): void {
690+
$this->foo = $foo;
691+
assertType('non-empty-string&numeric-string', $this->foo);
692+
}
693+
683694
public function __toString(): string {
684695
return 'Foo';
685696
}
686697
}
687698

699+
final class NotStringable {}
700+
688701
class ObjectWithToStringMethod {
689702
private string $foo;
690703

0 commit comments

Comments
 (0)