Skip to content

Commit 9556781

Browse files
committed
improve IntersectionType->getIterableKey/ValueType
1 parent 6075cb4 commit 9556781

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
@@ -774,6 +774,9 @@ public function getArraySize(): Type
774774

775775
public function getIterableKeyType(): Type
776776
{
777+
if ($this->isCallable()->yes() && $this->isArray()->yes()) {
778+
return new UnionType([new ConstantIntegerType(0), new ConstantIntegerType(1)]);
779+
}
777780
return $this->intersectTypes(static fn (Type $type): Type => $type->getIterableKeyType());
778781
}
779782

@@ -789,6 +792,9 @@ public function getLastIterableKeyType(): Type
789792

790793
public function getIterableValueType(): Type
791794
{
795+
if ($this->isCallable()->yes() && $this->isArray()->yes()) {
796+
return new UnionType([new ObjectWithoutClassType(), new StringType()]);
797+
}
792798
return $this->intersectTypes(static fn (Type $type): Type => $type->getIterableValueType());
793799
}
794800

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)