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
4 changes: 1 addition & 3 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,7 @@ public function intersectKeyArray(Type $otherArraysType): Type
}

if ($isKeySuperType->yes()) {
return $otherArraysType->isIterableAtLeastOnce()->yes()
? TypeCombinator::intersect($this, new NonEmptyArrayType())
: $this;
return $this;
}

return new self($otherArraysType->getIterableKeyType(), $this->getIterableValueType());
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/array-intersect-key.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class Foo
public function nonEmpty(array $arr, array $arr2): void
{
assertType('non-empty-array<int|string, string>', array_intersect_key($arr));
assertType('non-empty-array<int|string, string>', array_intersect_key($arr, $arr));
assertType('array<int|string, string>', array_intersect_key($arr, $arr));
assertType('array<int, string>', array_intersect_key($arr, $arr2));
assertType('non-empty-array<int, string>', array_intersect_key($arr2, $arr));
assertType('array<int, string>', array_intersect_key($arr2, $arr));
assertType('array{}', array_intersect_key($arr, []));
assertType("array<'foo', string>", array_intersect_key($arr, ['foo' => 17]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,10 @@ public function testBug2499(): void
$this->analyse([__DIR__ . '/data/bug-2499.php'], []);
}

public function testBug10561(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-10561.php'], []);
}

}
33 changes: 33 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-10561.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Bug10561;

/**
* @param mixed[] $arr1
* @param mixed[] $arr2
*/
function func(array $arr1, array $arr2): void {

/** @var array<string, string> $inner_arr1 */
$inner_arr1 = $arr1['inner_arr'];
/** @var array<string, string> $inner_arr2 */
$inner_arr2 = $arr2['inner_arr'];

if (!$inner_arr1) {
return;
}
if (!$inner_arr2) {
return;
}

$arr_intersect = array_intersect_key($inner_arr1, $inner_arr2);
if ($arr_intersect) {
echo "not empty\n";
} else {
echo "empty\n";
}
}

$arr1 = ['inner_arr' => ['a' => 'b']];
$arr2 = ['inner_arr' => ['c' => 'd']];
func($arr1, $arr2); // Outputs "empty"
Loading