Skip to content

Commit

Permalink
Merge branch refs/heads/1.11.x into 1.12.x
Browse files Browse the repository at this point in the history
  • Loading branch information
phpstan-bot authored Aug 12, 2024
2 parents d401944 + 708d938 commit 883f637
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
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"

0 comments on commit 883f637

Please sign in to comment.