Skip to content

Commit cef8120

Browse files
ondrejmirtesclaude
andcommitted
Add regression test(s) for collateral fix(es) #12786 #13075
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CGhnJQpUWRRpg6H5LuWJkA
1 parent 29c49f2 commit cef8120

3 files changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12786;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class A
8+
{
9+
/** @param ?string[] $should_be_array */
10+
public function __construct(
11+
public ?array $should_be_array,
12+
)
13+
{
14+
}
15+
16+
/** @param array{y?: string|string[]} $x */
17+
public static function fromRequest(array $x): self
18+
{
19+
if (isset($x['y']) && is_string($x['y'])) {
20+
$x['y'] = explode(',', $x['y']);
21+
} // $x['y'] can't be string anymore
22+
23+
assertType('array<string>|null', $x['y'] ?? null);
24+
25+
return new self(
26+
should_be_array: $x['y'] ?? null,
27+
);
28+
}
29+
}

‎tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,17 @@ public function testBug6379(): void
13681368
$this->analyse([__DIR__ . '/data/bug-6379.php'], []);
13691369
}
13701370

1371+
public function testBug13075(): void
1372+
{
1373+
$this->reportPossiblyNonexistentConstantArrayOffset = true;
1374+
$this->analyse([__DIR__ . '/data/bug-13075.php'], [
1375+
[
1376+
'Offset \'c\' might not exist on array{c?: string}.',
1377+
40,
1378+
],
1379+
]);
1380+
}
1381+
13711382
public static function dataUnsealedArrayShapes(): iterable
13721383
{
13731384
foreach ([false, true] as $reportPossiblyNonexistentGeneralArrayOffset) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13075;
4+
5+
// In all the examples the caller ensures there's at least one key present in
6+
// the $data parameter and we want to give that information to phpstan.
7+
8+
/**
9+
* @param array{a?: string, b?: string} $data
10+
*/
11+
function works(array $data): string {
12+
if (isset($data['a'])) {
13+
return $data['a'];
14+
}
15+
assert(isset($data['b']));
16+
return $data['b'];
17+
}
18+
19+
/**
20+
* @param array{a?: string, b?: string} $data
21+
*/
22+
function fail1(array $data): string {
23+
assert(isset($data['a']) || isset($data['b']));
24+
return $data['a'] ?? $data['b'];
25+
}
26+
27+
/**
28+
* @param array{a?: string, b?: string} $data
29+
*/
30+
function fail2(array $data): string {
31+
assert(array_key_exists('a', $data) || array_key_exists('b', $data));
32+
return $data['a'] ?? $data['b'];
33+
}
34+
35+
/**
36+
* @param array{a?: string, b?: string, c?: string} $data
37+
*/
38+
function fail3(array $data): string {
39+
assert((bool) array_intersect_key($data, array_flip(['a', 'b', 'c'])));
40+
return $data['a'] ?? $data['b'] ?? $data['c'];
41+
}

0 commit comments

Comments
 (0)