File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 3
3
namespace PHPStan \Type ;
4
4
5
5
use PHPStan \Reflection \ReflectionProviderStaticAccessor ;
6
+ use PHPStan \TrinaryLogic ;
6
7
7
8
class StringAlwaysAcceptingObjectWithToStringType extends StringType
8
9
{
9
10
11
+ public function isSuperTypeOf (Type $ type ): TrinaryLogic
12
+ {
13
+ if ($ type instanceof CompoundType) {
14
+ return $ type ->isSubTypeOf ($ this );
15
+ }
16
+
17
+ $ thatClassNames = $ type ->getObjectClassNames ();
18
+ if ($ thatClassNames === []) {
19
+ return parent ::isSuperTypeOf ($ type );
20
+ }
21
+
22
+ $ result = TrinaryLogic::createNo ();
23
+ $ reflectionProvider = ReflectionProviderStaticAccessor::getInstance ();
24
+ foreach ($ thatClassNames as $ thatClassName ) {
25
+ if (!$ reflectionProvider ->hasClass ($ thatClassName )) {
26
+ return TrinaryLogic::createNo ();
27
+ }
28
+
29
+ $ typeClass = $ reflectionProvider ->getClass ($ thatClassName );
30
+ $ result = $ result ->or (TrinaryLogic::createFromBoolean ($ typeClass ->hasNativeMethod ('__toString ' )));
31
+ }
32
+
33
+ return $ result ;
34
+ }
35
+
10
36
public function acceptsWithReason (Type $ type , bool $ strictTypes ): AcceptsResult
11
37
{
12
38
$ thatClassNames = $ type ->getObjectClassNames ();
Original file line number Diff line number Diff line change @@ -86,6 +86,11 @@ public function dataIsSuperTypeOf(): array
86
86
new GenericClassStringType (new ObjectType (stdClass::class)),
87
87
TrinaryLogic::createMaybe (),
88
88
],
89
+ [
90
+ new StringAlwaysAcceptingObjectWithToStringType (),
91
+ new ObjectType (ClassWithToString::class),
92
+ TrinaryLogic::createYes (),
93
+ ],
89
94
];
90
95
}
91
96
You can’t perform that action at this time.
0 commit comments