Skip to content

Commit 6062437

Browse files
committed
SplObjectStorage's ArrayAccess will never return null
1 parent cc9a674 commit 6062437

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

stubs/SplObjectStorage.stub

+6
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,10 @@ class SplObjectStorage implements Countable, Iterator, Serializable, ArrayAccess
6161
*/
6262
public function setInfo($data): void { }
6363

64+
/**
65+
* @param TObject $offset
66+
* @return TData
67+
*/
68+
public function offsetGet($offset);
69+
6470
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -10767,6 +10767,11 @@ public function dataBug4504(): array
1076710767
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4504.php');
1076810768
}
1076910769

10770+
public function dataBug4436(): array
10771+
{
10772+
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4436.php');
10773+
}
10774+
1077010775
/**
1077110776
* @param string $file
1077210777
* @return array<string, mixed[]>
@@ -10982,6 +10987,7 @@ private function gatherAssertTypes(string $file): array
1098210987
* @dataProvider dataCompact
1098310988
* @dataProvider dataBug4500
1098410989
* @dataProvider dataBug4504
10990+
* @dataProvider dataBug4436
1098510991
* @param string $assertType
1098610992
* @param string $file
1098710993
* @param mixed ...$args
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Bug4436;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
class Bar
8+
{
9+
}
10+
11+
class Foo
12+
{
13+
/** @var \SplObjectStorage<Bar, string> */
14+
private \SplObjectStorage $storage;
15+
16+
public function __construct()
17+
{
18+
$this->storage = new \SplObjectStorage();
19+
}
20+
21+
public function add(Bar $bar, string $value): void
22+
{
23+
$this->storage[$bar] = $value;
24+
}
25+
26+
public function get(Bar $bar): string
27+
{
28+
assertType('string', $this->storage[$bar]);
29+
return $this->storage[$bar];
30+
}
31+
}

0 commit comments

Comments
 (0)