Skip to content

Commit 9ac8db3

Browse files
phpstan-botclaude
authored andcommitted
Add pre-PHP 8 and PHP 8.0+ array literal tests for bug-10862
Split tests into four files covering all three PHP version behaviors: - php74: pre-PHP 8.0, negative keys never affect auto-index - php80: PHP 8.0+, array literals with negative keys update auto-index - php82: PHP <= 8.2, imperative assignment does not update for negative keys - php83: PHP 8.3+, negative keys always update auto-index Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e168a11 commit 9ac8db3

File tree

4 files changed

+77
-16
lines changed

4 files changed

+77
-16
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php // lint < 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug10862Php74;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
// Pre-PHP 8.0: negative keys never affect auto-index
10+
11+
// Imperative assignment
12+
function () {
13+
$a = [];
14+
$a[-4] = 1;
15+
$a[] = 2;
16+
17+
assertType('array{-4: 1, 0: 2}', $a);
18+
};
19+
20+
// Array literal
21+
function () {
22+
$a = [-4 => 1];
23+
$a[] = 2;
24+
25+
assertType('array{-4: 1, 0: 2}', $a);
26+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug10862Php80;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
// PHP 8.0+: array literal with negative keys updates auto-index
10+
11+
function () {
12+
$a = [-4 => 1];
13+
$a[] = 2;
14+
15+
assertType('array{-4: 1, -3: 2}', $a);
16+
};
17+
18+
function () {
19+
$a = [-10 => 'a', -5 => 'b'];
20+
$a[] = 'c';
21+
22+
assertType("array{-10: 'a', -5: 'b', -4: 'c'}", $a);
23+
};

tests/PHPStan/Analyser/nsrt/bug-10862-php82.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66

77
use function PHPStan\Testing\assertType;
88

9+
// PHP <= 8.2: imperative assignment with negative keys does not affect auto-index
10+
911
function () {
1012
$a = [];
1113
$a[-4] = 1;
1214
$a[] = 2;
1315

14-
assertType('array{-4: 1, 0: 2}', $a); // PHP <=8.2: next key after -4 is 0
15-
assertType('array{-4, 0}', array_keys($a));
16+
assertType('array{-4: 1, 0: 2}', $a);
1617
};
1718

1819
function () {
1920
$a = [];
2021
$a[-1] = 'x';
2122
$a[] = 'y';
2223

23-
assertType("array{-1: 'x', 0: 'y'}", $a); // PHP <=8.2: next key after -1 is 0
24-
assertType('array{-1, 0}', array_keys($a));
24+
assertType("array{-1: 'x', 0: 'y'}", $a);
2525
};
2626

2727
function () {
@@ -30,8 +30,7 @@ function () {
3030
$a[-5] = 'b';
3131
$a[] = 'c';
3232

33-
assertType("array{-10: 'a', -5: 'b', 0: 'c'}", $a); // PHP <=8.2: next key is 0
34-
assertType('array{-10, -5, 0}', array_keys($a));
33+
assertType("array{-10: 'a', -5: 'b', 0: 'c'}", $a);
3534
};
3635

3736
function () {
@@ -40,6 +39,5 @@ function () {
4039
$a[5] = 'b';
4140
$a[] = 'c';
4241

43-
assertType("array{-3: 'a', 5: 'b', 6: 'c'}", $a); // positive key dominates
44-
assertType('array{-3, 5, 6}', array_keys($a));
42+
assertType("array{-3: 'a', 5: 'b', 6: 'c'}", $a);
4543
};

tests/PHPStan/Analyser/nsrt/bug-10862-php83.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@
66

77
use function PHPStan\Testing\assertType;
88

9+
// PHP 8.3+: negative keys always affect auto-index (both imperative and literal)
10+
11+
// Imperative assignment
912
function () {
1013
$a = [];
1114
$a[-4] = 1;
1215
$a[] = 2;
1316

14-
assertType('array{-4: 1, -3: 2}', $a); // PHP 8.3+: next key after -4 is -3
15-
assertType('array{-4, -3}', array_keys($a));
17+
assertType('array{-4: 1, -3: 2}', $a);
1618
};
1719

1820
function () {
1921
$a = [];
2022
$a[-1] = 'x';
2123
$a[] = 'y';
2224

23-
assertType("array{-1: 'x', 0: 'y'}", $a); // PHP 8.3+: next key after -1 is 0
24-
assertType('array{-1, 0}', array_keys($a));
25+
assertType("array{-1: 'x', 0: 'y'}", $a);
2526
};
2627

2728
function () {
@@ -30,8 +31,7 @@ function () {
3031
$a[-5] = 'b';
3132
$a[] = 'c';
3233

33-
assertType("array{-10: 'a', -5: 'b', -4: 'c'}", $a); // PHP 8.3+: next key after max(-10,-5)=-5 is -4
34-
assertType('array{-10, -5, -4}', array_keys($a));
34+
assertType("array{-10: 'a', -5: 'b', -4: 'c'}", $a);
3535
};
3636

3737
function () {
@@ -40,6 +40,20 @@ function () {
4040
$a[5] = 'b';
4141
$a[] = 'c';
4242

43-
assertType("array{-3: 'a', 5: 'b', 6: 'c'}", $a); // positive key dominates
44-
assertType('array{-3, 5, 6}', array_keys($a));
43+
assertType("array{-3: 'a', 5: 'b', 6: 'c'}", $a);
44+
};
45+
46+
// Array literal
47+
function () {
48+
$a = [-4 => 1];
49+
$a[] = 2;
50+
51+
assertType('array{-4: 1, -3: 2}', $a);
52+
};
53+
54+
function () {
55+
$a = [-10 => 'a', -5 => 'b'];
56+
$a[] = 'c';
57+
58+
assertType("array{-10: 'a', -5: 'b', -4: 'c'}", $a);
4559
};

0 commit comments

Comments
 (0)