Skip to content

Commit ed21cbc

Browse files
committed
Support null templates
1 parent 30f8f1c commit ed21cbc

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/Rules/Generics/TemplateTypeCheck.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use PHPStan\Type\IterableType;
2929
use PHPStan\Type\KeyOfType;
3030
use PHPStan\Type\MixedType;
31+
use PHPStan\Type\NullType;
3132
use PHPStan\Type\ObjectShapeType;
3233
use PHPStan\Type\ObjectType;
3334
use PHPStan\Type\ObjectWithoutClassType;
@@ -132,6 +133,7 @@ public function check(
132133
&& $boundTypeClass !== GenericObjectType::class
133134
&& $boundTypeClass !== KeyOfType::class
134135
&& $boundTypeClass !== IterableType::class
136+
&& $boundTypeClass !== NullType::class
135137
&& !$boundType instanceof UnionType
136138
&& !$boundType instanceof IntersectionType
137139
&& !$boundType instanceof TemplateType

tests/PHPStan/Analyser/nsrt/template-null-bound.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,20 @@ public function doFoo(?int $p): ?int
1717
return $p;
1818
}
1919

20+
/**
21+
* @template T of null
22+
* @return T
23+
*/
24+
public function nullTemplate(): mixed
25+
{
26+
return null;
27+
}
28+
2029
}
2130

2231
function (Foo $f, ?int $i): void {
2332
assertType('null', $f->doFoo(null));
2433
assertType('1', $f->doFoo(1));
2534
assertType('int|null', $f->doFoo($i));
35+
assertType('null', $f->nullTemplate());
2636
};

0 commit comments

Comments
 (0)