Skip to content

Commit 8476760

Browse files
committed
Added regression tests
1 parent 4966dab commit 8476760

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

tests/PHPStan/Analyser/nsrt/bug13674a.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ public function sayHello($arrayA, $listA, int $i): void
1414
{
1515
if (array_key_exists($i, $arrayA)) {
1616
assertType('non-empty-array<int>', $arrayA);
17+
} else {
18+
assertType('array<int>', $arrayA);
1719
}
20+
assertType('array<int>', $arrayA);
1821

1922
if (array_key_exists($i, $listA)) {
2023
assertType('non-empty-list<int>', $listA);
24+
} else {
25+
assertType('list<int>', $listA);
2126
}
27+
assertType('list<int>', $listA);
2228
}
2329
}

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,4 +1113,11 @@ public function testBug12805(): void
11131113
$this->analyse([__DIR__ . '/data/bug-12805.php'], []);
11141114
}
11151115

1116+
public function testBug3795(): void
1117+
{
1118+
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
1119+
1120+
$this->analyse([__DIR__ . '/data/bug-3795.php'], []);
1121+
}
1122+
11161123
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Bug3795;
4+
5+
class User {
6+
private string $id;
7+
private string $name;
8+
9+
public function __construct(string $id, string $name) {
10+
$this->id = $id;
11+
$this->name = $name;
12+
}
13+
14+
public function getId() : string {
15+
return $this->id;
16+
}
17+
18+
public function getName() : string {
19+
return $this->name;
20+
}
21+
}
22+
23+
class Users {
24+
/**
25+
* @param array{id?: string, name?: string} $data
26+
*/
27+
public static function create(array $data) : User {
28+
// The following generates a warning
29+
30+
foreach (['id', 'name'] as $required) {
31+
if (!array_key_exists($required, $data)) {
32+
throw new InvalidArgumentException('Data is missing ' . $required);
33+
}
34+
}
35+
36+
return new User(
37+
(string) $data['id'],
38+
(string) $data['name'],
39+
);
40+
}
41+
}

0 commit comments

Comments
 (0)