File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed
tests/PHPStan/Analyser/nsrt Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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+
688701class ObjectWithToStringMethod {
689702 private string $ foo ;
690703
You can’t perform that action at this time.
0 commit comments