Skip to content

Commit

Permalink
Fix type of return value for WeakMap::offsetGet()
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnymilton committed Jun 18, 2024
1 parent 30f68ba commit 093b35e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions stubs/WeakReference.stub
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ final class WeakReference
*/
final class WeakMap implements \ArrayAccess, \Countable, \IteratorAggregate
{
/**
* @param TKey $offset
* @return TValue
*/
public function offsetGet($offset) {}
}
32 changes: 32 additions & 0 deletions tests/PHPStan/Analyser/nsrt/weakMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace weakMap;

use WeakMap;
use function PHPStan\Testing\assertType;

interface Foo {}
interface Bar {}

/**
* @param WeakMap<Foo, Bar> $weakMap
*/
function weakMapOffsetGetNotNullable(WeakMap $weakMap, Foo $foo): void
{
$bar = $weakMap[$foo];

assertType(Bar::class, $bar);
}


/**
* @param WeakMap<Foo, Bar|null> $weakMap
*/
function weakMapOffsetGetNullable(WeakMap $weakMap, Foo $foo): void
{
$bar = $weakMap[$foo];

assertType( 'weakMap\\Bar|null', $bar);
}

0 comments on commit 093b35e

Please sign in to comment.