Skip to content

Commit 093b35e

Browse files
authored
Fix type of return value for WeakMap::offsetGet()
1 parent 30f68ba commit 093b35e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Diff for: stubs/WeakReference.stub

+5
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ final class WeakReference
2626
*/
2727
final class WeakMap implements \ArrayAccess, \Countable, \IteratorAggregate
2828
{
29+
/**
30+
* @param TKey $offset
31+
* @return TValue
32+
*/
33+
public function offsetGet($offset) {}
2934
}

Diff for: tests/PHPStan/Analyser/nsrt/weakMap.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace weakMap;
6+
7+
use WeakMap;
8+
use function PHPStan\Testing\assertType;
9+
10+
interface Foo {}
11+
interface Bar {}
12+
13+
/**
14+
* @param WeakMap<Foo, Bar> $weakMap
15+
*/
16+
function weakMapOffsetGetNotNullable(WeakMap $weakMap, Foo $foo): void
17+
{
18+
$bar = $weakMap[$foo];
19+
20+
assertType(Bar::class, $bar);
21+
}
22+
23+
24+
/**
25+
* @param WeakMap<Foo, Bar|null> $weakMap
26+
*/
27+
function weakMapOffsetGetNullable(WeakMap $weakMap, Foo $foo): void
28+
{
29+
$bar = $weakMap[$foo];
30+
31+
assertType( 'weakMap\\Bar|null', $bar);
32+
}

0 commit comments

Comments
 (0)