Skip to content

Commit

Permalink
Fix calling implode() with array<string> on level 9
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 21, 2021
1 parent 1c6ce5d commit f11480b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ private function resolveIntersectionTypeNode(IntersectionTypeNode $typeNode, Nam
private function resolveArrayTypeNode(ArrayTypeNode $typeNode, NameScope $nameScope): Type
{
$itemType = $this->resolve($typeNode->type, $nameScope);
return new ArrayType(new MixedType(), $itemType);
return new ArrayType(new BenevolentUnionType([new IntegerType(), new StringType()]), $itemType);
}

private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $nameScope): Type
Expand All @@ -439,7 +439,7 @@ private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $na

if ($mainTypeName === 'array' || $mainTypeName === 'non-empty-array') {
if (count($genericTypes) === 1) { // array<ValueType>
$arrayType = new ArrayType(new MixedType(true), $genericTypes[0]);
$arrayType = new ArrayType(new BenevolentUnionType([new IntegerType(), new StringType()]), $genericTypes[0]);
} elseif (count($genericTypes) === 2) { // array<KeyType, ValueType>
$keyType = TypeCombinator::intersect($genericTypes[0], new UnionType([
new IntegerType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,4 +870,10 @@ public function testBug2782(): void
]);
}

public function testBug5661(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-5661.php'], []);
}

}
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-5661.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Bug5661;

class Foo
{

/**
* @param array<string> $array
*/
function sayHello(array $array): void
{
echo join(', ', $array) . PHP_EOL;
}

/**
* @param string[] $array
*/
function sayHello2(array $array): void
{
echo join(', ', $array) . PHP_EOL;
}

}

0 comments on commit f11480b

Please sign in to comment.