Skip to content

Commit 0f041b2

Browse files
committed
improve IntersectionType->getIterableKey/ValueType
1 parent f2be72c commit 0f041b2

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

src/Type/IntersectionType.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,9 @@ public function getArraySize(): Type
782782

783783
public function getIterableKeyType(): Type
784784
{
785+
if ($this->isCallable()->yes() && $this->isArray()->yes()) {
786+
return new UnionType([new ConstantIntegerType(0), new ConstantIntegerType(1)]);
787+
}
785788
return $this->intersectTypes(static fn (Type $type): Type => $type->getIterableKeyType());
786789
}
787790

@@ -797,6 +800,9 @@ public function getLastIterableKeyType(): Type
797800

798801
public function getIterableValueType(): Type
799802
{
803+
if ($this->isCallable()->yes() && $this->isArray()->yes()) {
804+
return new UnionType([new ObjectWithoutClassType(), new StringType()]);
805+
}
800806
return $this->intersectTypes(static fn (Type $type): Type => $type->getIterableValueType());
801807
}
802808

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ private static function findTestFiles(): iterable
174174
yield __DIR__ . '/../Rules/Variables/data/bug-7417.php';
175175
yield __DIR__ . '/../Rules/Arrays/data/bug-7469.php';
176176
yield __DIR__ . '/../Rules/Variables/data/bug-3391.php';
177+
yield __DIR__ . '/../Rules/Methods/data/bug-14549.php';
177178

178179
yield __DIR__ . '/../Rules/Functions/data/bug-anonymous-function-method-constant.php';
179180

tests/PHPStan/Rules/Methods/data/bug-14549.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22

33
namespace Bug14549;
44

5+
use function PHPStan\Testing\assertType;
6+
57
class MondayMorning
68
{
79
/**
810
* @param callable-array $task
911
*/
1012
public function call(array $task): void
1113
{
14+
foreach($task as $k => $v) {
15+
assertType('0|1', $k);
16+
assertType('object|string', $v);
17+
}
18+
assertType('class-string|object', $task[0]);
19+
assertType('string', $task[1]);
1220
}
1321
}
1422

0 commit comments

Comments
 (0)