Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3191,7 +3191,7 @@ public function describe(VerbosityLevel $level): string
$describeValue = function (bool $truncate) use ($level, $arrayName): string {
$items = [];
$values = [];
$exportValuesOnly = true;
$exportValuesOnly = !$this->shouldNameItsKeys();
foreach ($this->keyTypes as $i => $keyType) {
$valueType = $this->valueTypes[$i];
if ($keyType->getValue() !== $i) {
Expand Down Expand Up @@ -3274,6 +3274,12 @@ function () use ($typeOnlyArrayName, $level): string {
);
}

/** With bleeding edge, array{X, Y} is a list, so a shape that might not be one names its keys. */
private function shouldNameItsKeys(): bool
{
return count($this->keyTypes) > 1 && !$this->isList->yes() && BleedingEdgeToggle::isBleedingEdge();
}

/** @param bool $namingKeys whether the description names the keys, as a shape does */
private function shouldBeDescribedAsAList(bool $namingKeys = true): bool
{
Expand Down Expand Up @@ -4331,7 +4337,7 @@ public function toPhpDocNode(): TypeNode
{
$items = [];
$values = [];
$exportValuesOnly = true;
$exportValuesOnly = !$this->shouldNameItsKeys();
foreach ($this->keyTypes as $i => $keyType) {
if ($keyType->getValue() !== $i) {
$exportValuesOnly = false;
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/data/param-out.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function foo15() {
$manifest,
"fooCompare"
);
assertType('array{1, 2, 3}', $manifest);
assertType('array{0: 1, 1: 2, 2: 3}', $manifest);
}

function fooSpaceship (string $a, string $b): int {
Expand All @@ -234,7 +234,7 @@ function foo16() {
$array,
"fooSpaceship"
);
assertType('array{1, 2}', $array);
assertType('array{0: 1, 1: 2}', $array);
}

function fooShuffle() {
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/array-is-list-unset.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function () {
assertType('array{1, 2, 3}', $a);
assertType('true', array_is_list($a));
unset($a[2]);
assertType('array{1, 2}', $a);
assertType('array{0: 1, 1: 2}', $a);
assertType('false', array_is_list($a));
$a[] = 4;
assertType('array{0: 1, 1: 2, 3: 4}', $a);
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/array-replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function arrayReplaceArrayShapeAndGeneralArray($array1, $array2, $array3)
*/
public function arrayReplaceNumericKeys($array1, $array2): void
{
assertType("array{1, 3, 4}", array_replace($array1, $array2));
assertType("array{0: 1, 1: 3, 2: 4}", array_replace($array1, $array2));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function singleArgument(): void
// the haystack is array_search()'s second argument, so a call without one cannot be the
// list-preserving idiom - the call itself is reported as invalid
$list[array_search($list)] = 4;
assertType('array{1|4, 2|4, 3|4, ...<int<min, -1>|int<3, max>|string, 4>}', $list);
assertType('array{0: 1|4, 1: 2|4, 2: 3|4, ...<int<min, -1>|int<3, max>|string, 4>}', $list);
}

/**
Expand All @@ -20,5 +20,5 @@ function unpackedArguments(array $args): void
{
$list = [1, 2, 3];
$list[array_search(...$args)] = 4;
assertType('array{1|4, 2|4, 3|4, ...<int<min, -1>|int<3, max>|string, 4>}', $list);
assertType('array{0: 1|4, 1: 2|4, 2: 3|4, ...<int<min, -1>|int<3, max>|string, 4>}', $list);
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/array-shape-key-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function positionsOfIntegerKeysInAnyOrder(bool $b): void
assertType("array{'a'|'b', 'a'|'b'}", array_values($x));
assertType("array{'a'|'b'}", array_slice($x, 0, 1));
assertType("array{'a'|'b', 'a'|'b'}", array_reverse($x));
assertType("array{'b', 'a'}", array_reverse($x, true));
assertType("array{0: 'b', 1: 'a'}", array_reverse($x, true));

$shifted = $x;
array_shift($shifted);
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/nsrt/array_map_multiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public function arrayMapNull(array $array, array $other): void
{
assertType('array{}', array_map(null, []));
assertType('array{foo: true}', array_map(null, ['foo' => true]));
assertType('non-empty-list<array{1|2|3, 4|5|6}>', array_map(null, [1, 2, 3], [4, 5, 6]));
assertType('non-empty-list<array{0: 1|2|3, 1: 4|5|6}>', array_map(null, [1, 2, 3], [4, 5, 6]));

assertType('non-empty-array<string, int>', array_map(null, $array));
assertType('non-empty-list<array{int, int}>', array_map(null, $array, $array));
assertType('non-empty-list<array{int, int, int}>', array_map(null, $array, $array, $array));
assertType('non-empty-list<array{int|null, bool|null}>', array_map(null, $array, $other));
assertType('non-empty-list<array{0: int, 1: int}>', array_map(null, $array, $array));
assertType('non-empty-list<array{0: int, 1: int, 2: int}>', array_map(null, $array, $array, $array));
assertType('non-empty-list<array{0: int|null, 1: bool|null}>', array_map(null, $array, $other));

assertType('array{1}|array{true}', array_map(null, rand() ? [1] : [true]));
assertType('array{1}|array{true, false}', array_map(null, rand() ? [1] : [true, false]));
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-10254.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function (): void {
$value = Option::some(1)
->zip(Option::some(2));

assertType('Bug10254\\Option<array{1, 2}>', $value);
assertType('Bug10254\\Option<array{0: 1, 1: 2}>', $value);

$value1 = $value->map(function ($value) {
assertType('1', $value[0]);
Expand Down
16 changes: 8 additions & 8 deletions tests/PHPStan/Analyser/nsrt/bug-10627.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@ public function sayHello(): void
{
$list = ['A', 'C', 'B'];
natcasesort($list);
assertType("array{'A', 'C', 'B'}", $list);
assertType("array{0: 'A', 1: 'C', 2: 'B'}", $list);
assertType('bool', array_is_list($list));
}

public function sayHello2(): void
{
$list = ['A', 'C', 'B'];
natsort($list);
assertType("array{'A', 'C', 'B'}", $list);
assertType("array{0: 'A', 1: 'C', 2: 'B'}", $list);
assertType('bool', array_is_list($list));
}

public function sayHello3(): void
{
$list = ['A', 'C', 'B'];
arsort($list);
assertType("array{'A', 'C', 'B'}", $list);
assertType("array{0: 'A', 1: 'C', 2: 'B'}", $list);
assertType('bool', array_is_list($list));
}

public function sayHello4(): void
{
$list = ['A', 'C', 'B'];
asort($list);
assertType("array{'A', 'C', 'B'}", $list);
assertType("array{0: 'A', 1: 'C', 2: 'B'}", $list);
assertType('bool', array_is_list($list));
}

public function sayHello5(): void
{
$list = ['A', 'C', 'B'];
ksort($list);
assertType("array{'A', 'C', 'B'}", $list);
assertType("array{0: 'A', 1: 'C', 2: 'B'}", $list);
assertType('bool', array_is_list($list));
}

Expand All @@ -53,7 +53,7 @@ public function sayHello6(): void
uasort($list, function () {

});
assertType("array{'A', 'C', 'B'}", $list);
assertType("array{0: 'A', 1: 'C', 2: 'B'}", $list);
assertType('bool', array_is_list($list));
}

Expand All @@ -63,15 +63,15 @@ public function sayHello7(): void
uksort($list, function () {

});
assertType("array{'A', 'C', 'B'}", $list);
assertType("array{0: 'A', 1: 'C', 2: 'B'}", $list);
assertType('bool', array_is_list($list));
}

public function sayHello8(): void
{
$list = ['A', 'C', 'B'];
krsort($list);
assertType("array{'A', 'C', 'B'}", $list);
assertType("array{0: 'A', 1: 'C', 2: 'B'}", $list);
assertType('bool', array_is_list($list));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-14314.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testOptionalKeysWithEmpty(array $arr): void
{
assertType('array{}|array{0: string, 1: string, 2?: string}', $arr);
if (count($arr) === 3) {
assertType('array{string, string, string}', $arr);
assertType('array{0: string, 1: string, 2: string}', $arr);
return;
}
// Fallback keeps full array{0: string, 1: string, 2?: string} since its size
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-15679.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
function f(bool $b): void
{
$x = $b ? [1 => 0, 0 => 0] : [0 => 1, 1 => 0];
assertType('array{0|1, 0}', $x);
assertType('array{0: 0|1, 1: 0}', $x);
assertType('bool', array_is_list($x));

$y = $b ? [1 => 'a', 0 => 'b'] : [0 => 'c', 1 => 'a'];
Expand All @@ -21,7 +21,7 @@ function g(bool $b): void

$y = $b ? [1 => 'a', 0 => 'b'] : [0 => 'c', 1 => 'a'];
if (!array_is_list($y)) {
assertType("array{'b'|'c', 'a'}", $y);
assertType("array{0: 'b'|'c', 1: 'a'}", $y);
}

if ($b) {
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-3013.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testArrayKeyExists($foo): void
assertType("1", $bar);

if (array_key_exists($bar, $foo) === true) {
assertType("array{1, 2}", $foo);
assertType("array{0: 1, 1: 2}", $foo);
assertType("1", $bar);
return;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/nsrt/constant-array-type-set.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function doFoo(int $i)
{
$a = [1, 2, 3];
$a[$i] = 4;
assertType('array{1|4, 2|4, 3|4, ...<int<min, -1>|int<3, max>, 4>}', $a);
assertType('array{0: 1|4, 1: 2|4, 2: 3|4, ...<int<min, -1>|int<3, max>, 4>}', $a);

$b = [1, 2, 3];
$b[3] = 4;
Expand Down Expand Up @@ -72,7 +72,7 @@ public function doBar3(int $offset): void
{
$a = [false, false, false, false, false];
$a[$offset] = true;
assertType('array{bool, bool, bool, bool, bool, ...<int<5, max>, true>}', $a);
assertType('array{0: bool, 1: bool, 2: bool, 3: bool, 4: bool, ...<int<5, max>, true>}', $a);
}

/**
Expand All @@ -83,7 +83,7 @@ public function doBar4(int $offset): void
{
$a = [false, false, false, false, false];
$a[$offset] = true;
assertType('array{bool, false, false, false, false, ...<int<min, -1>, true>}', $a);
assertType('array{0: bool, 1: false, 2: false, 3: false, 4: false, ...<int<min, -1>, true>}', $a);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/count-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ public function doBar(array $arr): void
}

assertType('2', count($arr));
assertType('array{string, string}', $arr);
assertType('array{0: string, 1: string}', $arr);
}

/** @param array{0: string, 1?: string} $arr */
public function doBaz(array $arr): void
{
if (count($arr) > 1) {
assertType('2', count($arr));
assertType('array{string, string}', $arr);
assertType('array{0: string, 1: string}', $arr);
}

assertType('1|2', count($arr));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function optionalKeyIsDropped(array $a, array $b): void
public function keysTheArrayCanHave(array $a, array $b): void
{
if ($a === $b) {
assertType('array{int, int}', $a);
assertType('array{0: int, 1: int}', $a);
}
}

Expand Down
12 changes: 6 additions & 6 deletions tests/PHPStan/Analyser/nsrt/list-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ protected function testOptionalKeys($row): void
if (count($row) === 1) {
assertType('array{mixed}', $row);
} else {
assertType('array{mixed, string|null}', $row);
assertType('array{0: mixed, 1: string|null}', $row);
}

if (count($row) === 2) {
assertType('array{mixed, string|null}', $row);
assertType('array{0: mixed, 1: string|null}', $row);
} else {
assertType('array{mixed}', $row);
}
Expand All @@ -263,11 +263,11 @@ protected function testOptionalKeysInUnion($row): void
if (count($row) === 1) {
assertType('array{mixed}', $row);
} else {
assertType('array{mixed, string|null}', $row);
assertType('array{0: mixed, 1: string|null}', $row);
}

if (count($row) === 2) {
assertType('array{mixed, string|null}', $row);
assertType('array{0: mixed, 1: string|null}', $row);
} else {
assertType('array{mixed}', $row);
}
Expand All @@ -293,11 +293,11 @@ protected function testOptionalKeysInListsOfTaggedUnion($row): void
if (count($row) === 1) {
assertType('array{int}|array{string}', $row);
} else {
assertType('array{int, string|null}', $row);
assertType('array{0: int, 1: string|null}', $row);
}

if (count($row) === 2) {
assertType('array{int, string|null}', $row);
assertType('array{0: int, 1: string|null}', $row);
} else {
assertType('array{int}|array{string}', $row);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/nsrt/localeconv.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ function (): void {
};

function (int $timestamp, bool $assoc): void {
assertType('array{int<0, 59>, int<0, 59>, int<0, 23>, int<1, 31>, int<0, 11>, int, int<0, 6>, int<0, 365>, int}', localtime());
assertType('array{int<0, 59>, int<0, 59>, int<0, 23>, int<1, 31>, int<0, 11>, int, int<0, 6>, int<0, 365>, int}', localtime($timestamp));
assertType('array{int<0, 59>, int<0, 59>, int<0, 23>, int<1, 31>, int<0, 11>, int, int<0, 6>, int<0, 365>, int}', localtime($timestamp, false));
assertType('array{0: int<0, 59>, 1: int<0, 59>, 2: int<0, 23>, 3: int<1, 31>, 4: int<0, 11>, 5: int, 6: int<0, 6>, 7: int<0, 365>, 8: int}', localtime());
assertType('array{0: int<0, 59>, 1: int<0, 59>, 2: int<0, 23>, 3: int<1, 31>, 4: int<0, 11>, 5: int, 6: int<0, 6>, 7: int<0, 365>, 8: int}', localtime($timestamp));
assertType('array{0: int<0, 59>, 1: int<0, 59>, 2: int<0, 23>, 3: int<1, 31>, 4: int<0, 11>, 5: int, 6: int<0, 6>, 7: int<0, 365>, 8: int}', localtime($timestamp, false));
assertType('array{tm_sec: int<0, 59>, tm_min: int<0, 59>, tm_hour: int<0, 23>, tm_mday: int<1, 31>, tm_mon: int<0, 11>, tm_year: int, tm_wday: int<0, 6>, tm_yday: int<0, 365>, tm_isdst: int}', localtime($timestamp, true));
assertType('array{int<0, 59>, int<0, 59>, int<0, 23>, int<1, 31>, int<0, 11>, int, int<0, 6>, int<0, 365>, int}|array{tm_sec: int<0, 59>, tm_min: int<0, 59>, tm_hour: int<0, 23>, tm_mday: int<1, 31>, tm_mon: int<0, 11>, tm_year: int, tm_wday: int<0, 6>, tm_yday: int<0, 365>, tm_isdst: int}', localtime($timestamp, $assoc));
assertType('array{0: int<0, 59>, 1: int<0, 59>, 2: int<0, 23>, 3: int<1, 31>, 4: int<0, 11>, 5: int, 6: int<0, 6>, 7: int<0, 365>, 8: int}|array{tm_sec: int<0, 59>, tm_min: int<0, 59>, tm_hour: int<0, 23>, tm_mday: int<1, 31>, tm_mon: int<0, 11>, tm_year: int, tm_wday: int<0, 6>, tm_yday: int<0, 365>, tm_isdst: int}', localtime($timestamp, $assoc));
};
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Arrays/ArrayDestructuringRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testBug8075(): void
36,
],
[
'Offset 2 does not exist on array{string, int}.',
'Offset 2 does not exist on array{0: string, 1: int}.',
48,
],
[
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Generators/YieldFromTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testRule(): void
41,
],
[
'Generator expects value type array{DateTime, DateTime, stdClass, DateTimeImmutable}, array{0: DateTime, 1: DateTime, 2: stdClass, 4: DateTimeImmutable} given.',
'Generator expects value type array{0: DateTime, 1: DateTime, 2: stdClass, 3: DateTimeImmutable}, array{0: DateTime, 1: DateTime, 2: stdClass, 4: DateTimeImmutable} given.',
74,
'Array does not have offset 3.',
],
Expand Down
Loading
Loading