Skip to content

Commit d9d93b7

Browse files
VincentLangletondrejmirtes
authored andcommitted
Simplified ArrayType with Mixed
1 parent 25a06dd commit d9d93b7

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/Type/ArrayType.php

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PHPStan\Type\Constant\ConstantIntegerType;
2222
use PHPStan\Type\Constant\ConstantStringType;
2323
use PHPStan\Type\Generic\TemplateMixedType;
24+
use PHPStan\Type\Generic\TemplateStrictMixedType;
2425
use PHPStan\Type\Generic\TemplateTypeMap;
2526
use PHPStan\Type\Generic\TemplateTypeVariance;
2627
use PHPStan\Type\Traits\MaybeCallableTypeTrait;
@@ -50,6 +51,10 @@ public function __construct(Type $keyType, private Type $itemType)
5051
if ($keyType->describe(VerbosityLevel::value()) === '(int|string)') {
5152
$keyType = new MixedType();
5253
}
54+
if ($keyType instanceof StrictMixedType && !$keyType instanceof TemplateStrictMixedType) {
55+
$keyType = new UnionType([new StringType(), new IntegerType()]);
56+
}
57+
5358
$this->keyType = $keyType;
5459
}
5560

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ class CallToFunctionParametersRuleTest extends RuleTestCase
2121

2222
private bool $checkExplicitMixed = false;
2323

24+
private bool $checkImplicitMixed = false;
25+
2426
protected function getRule(): Rule
2527
{
2628
$broker = $this->createReflectionProvider();
2729
return new CallToFunctionParametersRule(
2830
$broker,
29-
new FunctionCallParametersCheck(new RuleLevelHelper($broker, true, false, true, $this->checkExplicitMixed, false, true, false), new NullsafeCheck(), new PhpVersion(80000), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true, true),
31+
new FunctionCallParametersCheck(new RuleLevelHelper($broker, true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, true, false), new NullsafeCheck(), new PhpVersion(80000), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true, true),
3032
);
3133
}
3234

@@ -1903,4 +1905,11 @@ public function testBug11759(): void
19031905
$this->analyse([__DIR__ . '/data/bug-11759.php'], []);
19041906
}
19051907

1908+
public function testBug12051(): void
1909+
{
1910+
$this->checkExplicitMixed = true;
1911+
$this->checkImplicitMixed = true;
1912+
$this->analyse([__DIR__ . '/data/bug-12051.php'], []);
1913+
}
1914+
19061915
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Bug12051;
4+
5+
/** @param array<int|string, mixed> $a */
6+
function foo($a): void {
7+
print "ok\n";
8+
}
9+
10+
/**
11+
* @param array<mixed> $a
12+
*/
13+
function bar($a): void {
14+
foo($a);
15+
}

0 commit comments

Comments
 (0)