Skip to content
Open
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
22 changes: 20 additions & 2 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,13 +521,31 @@ public function spliceArray(Type $offsetType, Type $lengthType, Type $replacemen
return new ConstantArrayType([], []);
}

$isIntegerKey = $this->keyType->isInteger();
if ($isIntegerKey->yes()) {
$keyType = IntegerRangeType::createAllGreaterThanOrEqualTo(0);
} elseif ($isIntegerKey->maybe()) {
$keyType = TypeCombinator::union(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all the existing 'non-empty-array' are transformed into non-empty-array<int<0, max>|string, mixed> because of now benevolent union (array key) is transformed into a real union.

I dunno if we want to keep a benevolent union here...

TypeCombinator::remove($this->getIterableKeyType(), new IntegerType()),
IntegerRangeType::createAllGreaterThanOrEqualTo(0),
);
} else {
$keyType = TypeCombinator::union($this->getIterableKeyType(), $replacementArrayType->getKeysArray()->getIterableKeyType());
}
$arrayType = new self(
TypeCombinator::union($this->getIterableKeyType(), $replacementArrayType->getKeysArray()->getIterableKeyType()),
$keyType,
TypeCombinator::union($this->getIterableValueType(), $replacementArrayType->getIterableValueType()),
);

$accessories = [];
if ($replacementArrayTypeIsIterableAtLeastOnce->yes()) {
$arrayType = new IntersectionType([$arrayType, new NonEmptyArrayType()]);
$accessories[] = new NonEmptyArrayType();
}
if ($isIntegerKey->yes()) {
$accessories[] = new AccessoryArrayListType();
}
if ($accessories !== []) {
$arrayType = new IntersectionType([$arrayType, ...$accessories]);
}

return $arrayType;
Expand Down
2 changes: 1 addition & 1 deletion src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,7 @@ public static function intersect(Type ...$types): Type
return $types[0];
}

return new IntersectionType(array_values($types));
return new IntersectionType($types);
}

public static function removeFalsey(Type $type): Type
Expand Down
28 changes: 14 additions & 14 deletions tests/PHPStan/Analyser/nsrt/array_splice.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,52 @@ function insertViaArraySplice(array $arr): void
{
$brr = $arr;
$extract = array_splice($brr, 0, 0, 1);
assertType('non-empty-array<int, int>', $brr);
assertType('non-empty-list<int>', $brr);
assertType('array{}', $extract);

$brr = $arr;
$extract = array_splice($brr, 0, 0, [1]);
assertType('non-empty-array<int, int>', $brr);
assertType('non-empty-list<int>', $brr);
assertType('array{}', $extract);

$brr = $arr;
$extract = array_splice($brr, 0, 0, '');
assertType('non-empty-array<int, \'\'|int>', $brr);
assertType('non-empty-list<\'\'|int>', $brr);
assertType('array{}', $extract);

$brr = $arr;
$extract = array_splice($brr, 0, 0, ['']);
assertType('non-empty-array<int, \'\'|int>', $brr);
assertType('non-empty-list<\'\'|int>', $brr);
assertType('array{}', $extract);

$brr = $arr;
$extract = array_splice($brr, 0, 0, null);
assertType('array<int, int>', $brr);
assertType('list<int>', $brr);
assertType('array{}', $extract);

$brr = $arr;
$extract = array_splice($brr, 0, 0, [null]);
assertType('non-empty-array<int, int|null>', $brr);
assertType('non-empty-list<int|null>', $brr);
assertType('array{}', $extract);

$brr = $arr;
$extract = array_splice($brr, 0, 0, new Foo());
assertType('non-empty-array<int, bool|int|string>', $brr);
assertType('non-empty-list<bool|int|string>', $brr);
assertType('array{}', $extract);

$brr = $arr;
$extract = array_splice($brr, 0, 0, [new \stdClass()]);
assertType('non-empty-array<int, int|stdClass>', $brr);
assertType('non-empty-list<int|stdClass>', $brr);
assertType('array{}', $extract);

$brr = $arr;
$extract = array_splice($brr, 0, 0, false);
assertType('non-empty-array<int, int|false>', $brr);
assertType('non-empty-list<int|false>', $brr);
assertType('array{}', $extract);

$brr = $arr;
$extract = array_splice($brr, 0, 0, [false]);
assertType('non-empty-array<int, int|false>', $brr);
assertType('non-empty-list<int|false>', $brr);
assertType('array{}', $extract);

$brr = $arr;
Expand Down Expand Up @@ -323,25 +323,25 @@ function offsets(array $arr): void
{
if (array_key_exists(1, $arr)) {
$extract = array_splice($arr, 0, 1, 'hello');
assertType('non-empty-array', $arr);
assertType('non-empty-array<int<0, max>|string, mixed>', $arr);
assertType('array', $extract);
}

if (array_key_exists(1, $arr)) {
$extract = array_splice($arr, 0, 0, 'hello');
assertType('non-empty-array&hasOffset(1)', $arr);
assertType('non-empty-array<int<0, max>|string, mixed>&hasOffset(1)', $arr);
assertType('array{}', $extract);
}

if (array_key_exists(1, $arr) && $arr[1] === 'foo') {
$extract = array_splice($arr, 0, 1, 'hello');
assertType('non-empty-array', $arr);
assertType('non-empty-array<int<0, max>|string, mixed>', $arr);
assertType('array', $extract);
}

if (array_key_exists(1, $arr) && $arr[1] === 'foo') {
$extract = array_splice($arr, 0, 0, 'hello');
assertType('non-empty-array&hasOffsetValue(1, \'foo\')', $arr);
assertType('non-empty-array<int<0, max>|string, mixed>&hasOffsetValue(1, \'foo\')', $arr);
assertType('array{}', $extract);
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14037.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Bug14037;

use function PHPStan\Testing\assertType;

/**
* @param array<10|20|30|'a', mixed> $a
*/
function splice(array $a): void {
array_splice($a, 0, 0);
assertType("array<'a'|int<0, max>, mixed>", $a);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 'a'|int<0, 2>

You're doing $keyType = IntegerRangeType::createAllGreaterThanOrEqualTo(0);

but maybe you should check getConstantScalarTypes.
and if if (count($constantScalars) > 0), you're doing $keyType = IntegerRangeType::fromInterval(0, $count -1); ?

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-4743.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function splice(int $offset, int $length): void
{
$newNodes = array_splice($this->nodes, $offset, $length);

assertType('array<T of Bug4743\\Node (class Bug4743\\NodeList, argument)>', $this->nodes);
assertType('array<int<0, max>|string, T of Bug4743\\Node (class Bug4743\\NodeList, argument)>', $this->nodes);
assertType('array<T of Bug4743\\Node (class Bug4743\\NodeList, argument)>', $newNodes);
}
}
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-5017.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function doBar($items)
while ($items) {
assertType('non-empty-array<int>', $items);
$batch = array_splice($items, 0, 2);
assertType('array<int>', $items);
assertType('array<int<0, max>|string, int>', $items);
assertType('array<int>', $batch);
}
}
Expand All @@ -49,7 +49,7 @@ public function doBar3(array $ints, array $strings)
{
$removed = array_splice($ints, 0, 2, $strings);
assertType('array<int>', $removed);
assertType('array<int|string>', $ints);
assertType('array<int<0, max>|string, int|string>', $ints);
assertType('array<string>', $strings);
}

Expand Down
82 changes: 82 additions & 0 deletions tests/PHPStan/Type/ArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,86 @@ public function testHasOffsetValueType(
);
}

public static function dataSpliceArray(): array
{
return [
[
new ArrayType(new UnionType([
new ConstantIntegerType(10),
new ConstantIntegerType(20),
new ConstantIntegerType(30),
new ConstantStringType('a'),
]), new MixedType()),
new ConstantIntegerType(0),
new ConstantIntegerType(0),
new ConstantArrayType([], []),
"array<'a'|int<0, max>, mixed>",
],
[
new ArrayType(new UnionType([
new IntegerType(),
new ConstantStringType('a'),
]), new MixedType()),
new ConstantIntegerType(0),
new ConstantIntegerType(0),
new ConstantArrayType([], []),
"array<'a'|int<0, max>, mixed>",
],
[
new ArrayType(new IntegerType(), new MixedType()),
new ConstantIntegerType(0),
new ConstantIntegerType(0),
new ConstantArrayType([], []),
'list',
],
[
new ArrayType(new StringType(), new MixedType()),
new ConstantIntegerType(0),
new ConstantIntegerType(0),
new ConstantArrayType([], []),
'array<string, mixed>',
],
[
new ArrayType(new MixedType(), new MixedType()),
new ConstantIntegerType(0),
new ConstantIntegerType(0),
new ConstantArrayType([], []),
'array<int<0, max>|string, mixed>',
],
[
new ArrayType(new StringType(), new MixedType()),
new ConstantIntegerType(0),
new ConstantIntegerType(0),
new ConstantArrayType(
[new ConstantStringType('key')],
[new ConstantStringType('value')],
),
'non-empty-array<0|string, mixed>',
],
[
new ArrayType(new StringType(), new MixedType()),
new ConstantIntegerType(0),
new ConstantIntegerType(0),
new ArrayType(new MixedType(), new MixedType()),
'array<int<0, max>|string, mixed>',
],
];
}

#[DataProvider('dataSpliceArray')]
public function testSpliceArray(
ArrayType $type,
Type $offsetType,
Type $lengthType,
Type $replacementType,
string $expectedType,
): void
{
$actualResult = $type->spliceArray($offsetType, $lengthType, $replacementType);
$this->assertSame(
$expectedType,
$actualResult->describe(VerbosityLevel::precise()),
);
}

}
Loading