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
12 changes: 12 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
Expand Down Expand Up @@ -878,6 +879,17 @@ public function tryRemove(Type $typeToRemove): ?Type
return new ConstantArrayType([], []);
}

if ($typeToRemove instanceof HasOffsetType) {
return $this->unsetOffset($typeToRemove->getOffsetType());
}

if (
$typeToRemove instanceof HasOffsetValueType
&& $typeToRemove->getValueType()->isSuperTypeOf($this->itemType)->yes()
) {
return $this->unsetOffset($typeToRemove->getOffsetType());
}

return null;
}

Expand Down
91 changes: 91 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9461.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug9461;

use function array_key_exists;
use function PHPStan\Testing\assertType;

function test(): void {
/** @var array<0|string, mixed> */
$defaults = ['test', 'a' => 1];

assertType('array<0|string, mixed>', $defaults);
if (!array_key_exists(0, $defaults)) {
assertType('array<string, mixed>', $defaults);
} else {
assertType('non-empty-array<0|string, mixed>&hasOffset(0)', $defaults);
}
Comment thread
VincentLanglet marked this conversation as resolved.
}

function testStringKey(): void {
/** @var array<'foo'|int, mixed> */
$arr = [];

if (!array_key_exists('foo', $arr)) {
assertType('array<int, mixed>', $arr);
} else {
assertType("non-empty-array<'foo'|int, mixed>&hasOffset('foo')", $arr);
}
}

function testGenericIntKey(): void {
/** @var array<int, mixed> */
$arr = [];

if (!array_key_exists(0, $arr)) {
Comment thread
VincentLanglet marked this conversation as resolved.
assertType('array<int<min, -1>|int<1, max>, mixed>', $arr);
}
}

function testKeyExists(): void {
/** @var array<0|string, mixed> */
$arr = [];

if (!key_exists(0, $arr)) {
assertType('array<string, mixed>', $arr);
}
}

function testNonEmptyArray(): void {
/** @var non-empty-array<0|string, mixed> */
$arr = ['test'];

assertType('non-empty-array<0|string, mixed>', $arr);
if (!array_key_exists(0, $arr)) {
assertType('non-empty-array<string, mixed>', $arr);
} else {
assertType('non-empty-array<0|string, mixed>&hasOffset(0)', $arr);
}
}

function testIssetDoesNotNarrowKeyType(): void {
/** @var array<0|string, mixed> */
$arr = [];

if (!isset($arr[0])) {
// isset also checks for null, so !isset doesn't mean key doesn't exist
assertType('array<0|string, mixed>', $arr);
} else {
assertType('non-empty-array<0|string, mixed>&hasOffsetValue(0, mixed~null)', $arr);
}
}

/**
* @param array<0|string, mixed>|string $label
* @return array<string, mixed>
*/
function makeSeedFromLabel($label = []): array
{
$defaults = is_array($label) ? $label : [$label];
assertType('array<0|string, mixed>', $defaults);

if (array_key_exists(0, $defaults)) {
$defaults['content'] = $defaults[0];
unset($defaults[0]);
}
assertType('array<string, mixed>', $defaults);

return $defaults;
}
18 changes: 18 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6167,6 +6167,24 @@ public static function dataRemove(): array
IntersectionType::class,
'non-empty-array<string, string>&oversized-array',
],
[
new ArrayType(new UnionType([new ConstantIntegerType(0), new StringType()]), new MixedType()),
new HasOffsetValueType(new ConstantIntegerType(0), new MixedType()),
ArrayType::class,
'array<string, mixed>',
],
[
new ArrayType(new UnionType([new ConstantIntegerType(0), new StringType()]), new IntegerType()),
new HasOffsetValueType(new ConstantIntegerType(0), new IntegerType()),
ArrayType::class,
'array<string, int>',
],
[
new ArrayType(new UnionType([new ConstantIntegerType(0), new StringType()]), new MixedType()),
new HasOffsetValueType(new ConstantIntegerType(0), new StringType()),
ArrayType::class,
'array<0|string, mixed>',
],
];
}

Expand Down
Loading