forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug-11679.php
More file actions
40 lines (34 loc) · 1023 Bytes
/
bug-11679.php
File metadata and controls
40 lines (34 loc) · 1023 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace Bug11679;
use function PHPStan\Testing\assertType;
class WorkingExample
{
/** @var array{foo?: bool} */
private array $arr = [];
public function sayHello(): bool
{
assertType('array{foo?: bool}', $this->arr);
if (!isset($this->arr['foo'])) {
$this->arr['foo'] = true;
assertType('array{foo: true}', $this->arr);
}
assertType('array{foo: bool}', $this->arr);
return $this->arr['foo']; // PHPStan realizes optional 'foo' is set
}
}
class NonworkingExample
{
/** @var array<int, array{foo?: bool}> */
private array $arr = [];
public function sayHello(int $index): bool
{
assertType('array<int, array{foo?: bool}>', $this->arr);
if (!isset($this->arr[$index]['foo'])) {
$this->arr[$index]['foo'] = true;
assertType('non-empty-array<int, array{foo: true}>', $this->arr);
assertType('array{foo: true}', $this->arr[$index]);
}
assertType('array<int, array{foo?: bool}>', $this->arr);
return $this->arr[$index]['foo']; // PHPStan does not realize 'foo' is set
}
}