Skip to content

Commit

Permalink
SplObjectStorage's ArrayAccess will never return null
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 11, 2021
1 parent cc9a674 commit 6062437
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions stubs/SplObjectStorage.stub
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,10 @@ class SplObjectStorage implements Countable, Iterator, Serializable, ArrayAccess
*/
public function setInfo($data): void { }

/**
* @param TObject $offset
* @return TData
*/
public function offsetGet($offset);

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10767,6 +10767,11 @@ public function dataBug4504(): array
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4504.php');
}

public function dataBug4436(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4436.php');
}

/**
* @param string $file
* @return array<string, mixed[]>
Expand Down Expand Up @@ -10982,6 +10987,7 @@ private function gatherAssertTypes(string $file): array
* @dataProvider dataCompact
* @dataProvider dataBug4500
* @dataProvider dataBug4504
* @dataProvider dataBug4436
* @param string $assertType
* @param string $file
* @param mixed ...$args
Expand Down
31 changes: 31 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4436.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Bug4436;

use function PHPStan\Analyser\assertType;

class Bar
{
}

class Foo
{
/** @var \SplObjectStorage<Bar, string> */
private \SplObjectStorage $storage;

public function __construct()
{
$this->storage = new \SplObjectStorage();
}

public function add(Bar $bar, string $value): void
{
$this->storage[$bar] = $value;
}

public function get(Bar $bar): string
{
assertType('string', $this->storage[$bar]);
return $this->storage[$bar];
}
}

0 comments on commit 6062437

Please sign in to comment.