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
8 changes: 7 additions & 1 deletion src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,8 @@ public function spliceArray(Type $offsetType, Type $lengthType, Type $replacemen
->spliceArray($offsetType, $lengthType, $replacementType);
}

$allKeysInteger = $this->getIterableKeyType()->isInteger()->yes();

if ($keyTypesCount + $offset <= 0) {
// A negative offset cannot reach left outside the array twice
$offset = 0;
Expand Down Expand Up @@ -1271,7 +1273,11 @@ public function spliceArray(Type $offsetType, Type $lengthType, Type $replacemen
);
}

$types[] = $builder->getArray();
$builtType = $builder->getArray();
if ($allKeysInteger && !$builtType->isList()->yes()) {
$builtType = TypeCombinator::intersect($builtType, new AccessoryArrayListType());
}
$types[] = $builtType;
}

return TypeCombinator::union(...$types);
Expand Down
55 changes: 55 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14472.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types = 1);

namespace Bug14472;

use function PHPStan\Testing\assertType;

class Foo
{
/**
* @param string[] $replacement
*/
public function arraySpliceOnConstantArrayWithIntKeys(array $replacement): void
{
$headers = [
'last_name',
'first_name',
'email',
'phone',
'position',
'client_identifier',
'profile',
'hierarchy_role',
'hierarchy_role_name',
'state',
'admin',
'confirmed',
'invited',
'using_native_app',
'using_sso',
'last_connection',
];

assertType("array{'last_name', 'first_name', 'email', 'phone', 'position', 'client_identifier', 'profile', 'hierarchy_role', 'hierarchy_role_name', 'state', 'admin', 'confirmed', 'invited', 'using_native_app', 'using_sso', 'last_connection'}", $headers);

array_splice($headers, 9, 0, $replacement);
assertType("non-empty-list<string>", $headers);
}

/**
* @param list<string> $replacement
*/
public function arraySpliceOnConstantArrayWithIntKeysListReplacement(array $replacement): void
{
$headers = ['a', 'b', 'c'];
array_splice($headers, 1, 0, $replacement);
assertType("non-empty-list<string>", $headers);
}

public function arraySpliceOnConstantArrayWithStringKeys(): void
{
$headers = ['a' => 'x', 'b' => 'y', 'c' => 'z'];
array_splice($headers, 1, 1, ['replacement']);
assertType("array{a: 'x', 0: 'replacement', c: 'z'}", $headers);
}
}
Loading