Skip to content

Commit bafc809

Browse files
committed
added regression tests
1 parent 2c21ad1 commit bafc809

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ private static function findTestFiles(): iterable
208208
yield __DIR__ . '/../Rules/Classes/data/bug-11591-property-tag.php';
209209
yield __DIR__ . '/../Rules/Classes/data/mixin-trait-use.php';
210210

211+
yield __DIR__ . '/../Rules/Arrays/data/bug-11679.php';
211212
yield __DIR__ . '/../Rules/Methods/data/bug-4801.php';
212213
}
213214

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,13 @@ public function testBug12406(): void
856856
$this->analyse([__DIR__ . '/data/bug-12406.php'], []);
857857
}
858858

859+
public function testBug11679(): void
860+
{
861+
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
862+
863+
$this->analyse([__DIR__ . '/data/bug-11679.php'], []);
864+
}
865+
859866
public function testBug8649(): void
860867
{
861868
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Bug11679;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class WorkingExample
8+
{
9+
/** @var array{foo?: bool} */
10+
private array $arr = [];
11+
12+
public function sayHello(): bool
13+
{
14+
assertType('array{foo?: bool}', $this->arr);
15+
if (!isset($this->arr['foo'])) {
16+
$this->arr['foo'] = true;
17+
assertType('array{foo: true}', $this->arr);
18+
}
19+
assertType('array{foo: bool}', $this->arr);
20+
return $this->arr['foo']; // PHPStan realizes optional 'foo' is set
21+
}
22+
}
23+
24+
class NonworkingExample
25+
{
26+
/** @var array<int, array{foo?: bool}> */
27+
private array $arr = [];
28+
29+
public function sayHello(int $index): bool
30+
{
31+
assertType('array<int, array{foo?: bool}>', $this->arr);
32+
if (!isset($this->arr[$index]['foo'])) {
33+
$this->arr[$index]['foo'] = true;
34+
assertType('non-empty-array<int, array{foo: true}>', $this->arr);
35+
}
36+
assertType('array<int, array{foo?: bool}>', $this->arr);
37+
return $this->arr[$index]['foo']; // PHPStan does not realize 'foo' is set
38+
}
39+
}

0 commit comments

Comments
 (0)