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
26 changes: 24 additions & 2 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,13 +521,35 @@
return new ConstantArrayType([], []);
}

$existingArrayKeyType = $this->getIterableKeyType();
$keyType = TypeTraverser::map($existingArrayKeyType, static function (Type $type, callable $traverse): Type {
if ($type instanceof UnionType) {
return $traverse($type);
}

if ($type->isInteger()->yes()) {

Check warning on line 530 in src/Type/ArrayType.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return $traverse($type); } - if ($type->isInteger()->yes()) { + if (!$type->isInteger()->no()) { return IntegerRangeType::createAllGreaterThanOrEqualTo(0); }

Check warning on line 530 in src/Type/ArrayType.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return $traverse($type); } - if ($type->isInteger()->yes()) { + if (!$type->isInteger()->no()) { return IntegerRangeType::createAllGreaterThanOrEqualTo(0); }
return IntegerRangeType::createAllGreaterThanOrEqualTo(0);
}

return $type;
});

$arrayType = new self(
TypeCombinator::union($this->getIterableKeyType(), $replacementArrayType->getKeysArray()->getIterableKeyType()),
TypeCombinator::union($keyType, $replacementArrayType->getKeysArray()->getIterableKeyType()),
TypeCombinator::union($this->getIterableValueType(), $replacementArrayType->getIterableValueType()),
);

$accessories = [];
if ($replacementArrayTypeIsIterableAtLeastOnce->yes()) {
$arrayType = new IntersectionType([$arrayType, new NonEmptyArrayType()]);
$accessories[] = new NonEmptyArrayType();
}
if ($existingArrayKeyType->isInteger()->yes()) {
$accessories[] = new AccessoryArrayListType();
}
if (count($accessories) > 0) {
$accessories[] = $arrayType;

return new IntersectionType($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 @@ -1499,7 +1499,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
36 changes: 22 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 Expand Up @@ -384,3 +384,11 @@ function lists(array $arr): void
assertType('list<string>', $arr);
assertType('list<string>', $extract);
}

/** @param array<string, string> $arr */
function more(array $arr): void
{
$extract = array_splice($arr, 0, 1, [17 => 'foo', 18 => 'bar']);
assertType('non-empty-array<0|1|string, string>', $arr);
assertType('array<string, string>', $extract);
}
21 changes: 21 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14037.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace Bug14037;

use function PHPStan\Testing\assertType;

/**
* @param array<10|20|30|'a', mixed> $a
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

also add a test for integer-range

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Done

*/
function splice(array $a): void {
array_splice($a, 0, 0);
assertType("array<'a'|int<0, max>, mixed>", $a);
}

/**
* @param array<int<10, 30>|'a', mixed> $a
*/
function splice2(array $a): void {
array_splice($a, 0, 0);
assertType("array<'a'|int<0, max>, mixed>", $a);
}
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