Skip to content

Commit

Permalink
ArrayType - use BenevolentUnionType even if it contains StrictMixedType
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 3, 2021
1 parent 95754d1 commit c59330c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Type/ArrayType.php
Expand Up @@ -176,6 +176,9 @@ public function getIterableKeyType(): Type
if ($keyType instanceof MixedType && !$keyType instanceof TemplateMixedType) {
return new BenevolentUnionType([new IntegerType(), new StringType()]);
}
if ($keyType instanceof StrictMixedType) {
return new BenevolentUnionType([new IntegerType(), new StringType()]);
}

return $keyType;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Expand Up @@ -876,4 +876,15 @@ public function testBug5661(): void
$this->analyse([__DIR__ . '/data/bug-5661.php'], []);
}

public function testBug5872(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-5872.php'], [
[
'Parameter #2 $array of function array_map expects array, mixed given.',
12,
],
]);
}

}
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-5872.php
@@ -0,0 +1,15 @@
<?php

namespace Bug5872;

class HelloWorld
{
/**
* @phpstan-param mixed[] $mixedArray
*/
public function sayHello(array $mixedArray): void
{
var_dump(array_map('\strval', $mixedArray["api"]));
var_dump(array_map('\strval', (array) $mixedArray["api"]));
}
}

0 comments on commit c59330c

Please sign in to comment.