Skip to content

Commit 4b005c8

Browse files
herndlmondrejmirtes
authored andcommitted
Remove incorrect generics support
1 parent cd9b558 commit 4b005c8

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

src/Type/Php/IsAFunctionTypeSpecifyingHelper.php

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,7 @@ public function determineType(
2525
bool $allowSameClass,
2626
): Type
2727
{
28-
$objectOrClassTypeClassNameType = TypeTraverser::map(
29-
$objectOrClassType,
30-
static function (Type $type, callable $traverse): Type {
31-
if ($type instanceof UnionType || $type instanceof IntersectionType) {
32-
return $traverse($type);
33-
}
34-
if ($type instanceof GenericClassStringType) {
35-
return $traverse($type->getGenericType());
36-
}
37-
if ($type instanceof TypeWithClassName) {
38-
return $type;
39-
}
40-
return new ObjectType('');
41-
},
42-
);
43-
$objectOrClassTypeClassName = $objectOrClassTypeClassNameType instanceof TypeWithClassName
44-
? $objectOrClassTypeClassNameType->getClassName()
45-
: null;
28+
$objectOrClassTypeClassName = $this->determineClassNameFromObjectOrClassType($objectOrClassType);
4629

4730
return TypeTraverser::map(
4831
$classType,
@@ -64,18 +47,14 @@ static function (Type $type, callable $traverse) use ($objectOrClassTypeClassNam
6447
return new ObjectType($type->getValue());
6548
}
6649
if ($type instanceof GenericClassStringType) {
67-
$genericType = $type->getGenericType();
68-
if (!$allowSameClass && $genericType instanceof TypeWithClassName && $genericType->getClassName() === $objectOrClassTypeClassName) {
69-
return new NeverType();
70-
}
7150
if ($allowString) {
7251
return TypeCombinator::union(
73-
$genericType,
52+
$type->getGenericType(),
7453
$type,
7554
);
7655
}
7756

78-
return $genericType;
57+
return $type->getGenericType();
7958
}
8059
if ($allowString) {
8160
return TypeCombinator::union(
@@ -89,4 +68,17 @@ static function (Type $type, callable $traverse) use ($objectOrClassTypeClassNam
8968
);
9069
}
9170

71+
private function determineClassNameFromObjectOrClassType(Type $type): ?string
72+
{
73+
if ($type instanceof TypeWithClassName) {
74+
return $type->getClassName();
75+
}
76+
77+
if ($type instanceof ConstantStringType) {
78+
return $type->getValue();
79+
}
80+
81+
return null;
82+
}
83+
9284
}

tests/PHPStan/Analyser/data/is-subclass-of.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function (Bar $a, Bar $b, Bar $c, Bar $d) {
1414
/** @var class-string<Bar> $barClassString */
1515
$barClassString = 'Bar';
1616
if (is_subclass_of($c, $barClassString)) {
17-
\PHPStan\Testing\assertType('*NEVER*', $c);
17+
\PHPStan\Testing\assertType('IsSubclassOf\Bar', $c);
1818
}
1919

2020
/** @var class-string<Foo> $fooClassString */
@@ -27,7 +27,7 @@ function (Bar $a, Bar $b, Bar $c, Bar $d) {
2727
function (string $a, string $b, string $c, string $d) {
2828
/** @var class-string<Bar> $a */
2929
if (is_subclass_of($a, Bar::class)) {
30-
\PHPStan\Testing\assertType('*NEVER*', $a);
30+
\PHPStan\Testing\assertType('class-string<IsSubclassOf\Bar>', $a);
3131
}
3232

3333
/** @var class-string<Bar> $b */
@@ -39,7 +39,7 @@ function (string $a, string $b, string $c, string $d) {
3939
/** @var class-string<Bar> $barClassString */
4040
$barClassString = 'Bar';
4141
if (is_subclass_of($c, $barClassString)) {
42-
\PHPStan\Testing\assertType('*NEVER*', $c);
42+
\PHPStan\Testing\assertType('class-string<IsSubclassOf\Bar>', $c);
4343
}
4444

4545
/** @var class-string<Bar> $d */

0 commit comments

Comments
 (0)