Skip to content

Commit dd1ee58

Browse files
Implements isSuperTypeOf
1 parent 4f086b8 commit dd1ee58

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/Type/StringNeverAcceptingObjectWithToStringType.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@
55
class StringNeverAcceptingObjectWithToStringType extends StringType
66
{
77

8+
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
9+
{
10+
if ($type instanceof CompoundType) {
11+
return $type->isSubTypeOf($this);
12+
}
13+
14+
$result = parent::isSuperTypeOf($type);
15+
if (!$type instanceof self) {
16+
$result = $result->and(IsSuperTypeOfResult::createMaybe());
17+
}
18+
19+
return $result;
20+
}
21+
822
public function accepts(Type $type, bool $strictTypes): AcceptsResult
923
{
1024
return parent::accepts($type, true);

tests/PHPStan/Type/TypeCombinatorTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
use PHPUnit\Framework\Attributes\DataProvider;
5252
use RecursionCallable\Foo;
5353
use stdClass;
54+
use Stringable;
5455
use Test\ClassWithNullableProperty;
5556
use Test\ClassWithToString;
5657
use Test\FirstInterface;
@@ -2141,6 +2142,22 @@ public static function dataUnion(): iterable
21412142
MixedType::class,
21422143
'mixed=implicit',
21432144
],
2145+
[
2146+
[
2147+
new StringNeverAcceptingObjectWithToStringType(),
2148+
new StringType(),
2149+
],
2150+
StringType::class,
2151+
'string',
2152+
],
2153+
[
2154+
[
2155+
new StringNeverAcceptingObjectWithToStringType(),
2156+
new StringAlwaysAcceptingObjectWithToStringType(),
2157+
],
2158+
StringAlwaysAcceptingObjectWithToStringType::class,
2159+
'string',
2160+
],
21442161
];
21452162

21462163
if (PHP_VERSION_ID < 80100) {
@@ -4159,6 +4176,46 @@ public static function dataIntersect(): iterable
41594176
ObjectType::class,
41604177
'stdClass',
41614178
],
4179+
[
4180+
[
4181+
new StringAlwaysAcceptingObjectWithToStringType(),
4182+
new ObjectType(Stringable::class),
4183+
],
4184+
ObjectType::class,
4185+
'Stringable',
4186+
],
4187+
[
4188+
[
4189+
new StringNeverAcceptingObjectWithToStringType(),
4190+
new ObjectType(Stringable::class),
4191+
],
4192+
NeverType::class,
4193+
'*NEVER*=implicit',
4194+
],
4195+
[
4196+
[
4197+
new StringNeverAcceptingObjectWithToStringType(),
4198+
new StringAlwaysAcceptingObjectWithToStringType(),
4199+
],
4200+
StringNeverAcceptingObjectWithToStringType::class,
4201+
'string',
4202+
],
4203+
[
4204+
[
4205+
new StringNeverAcceptingObjectWithToStringType(),
4206+
new StringType(),
4207+
],
4208+
StringNeverAcceptingObjectWithToStringType::class,
4209+
'string',
4210+
],
4211+
[
4212+
[
4213+
new StringAlwaysAcceptingObjectWithToStringType(),
4214+
new StringType(),
4215+
],
4216+
StringType::class,
4217+
'string',
4218+
],
41624219
];
41634220

41644221
if (PHP_VERSION_ID < 80100) {

0 commit comments

Comments
 (0)