Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/Type/Php/ArrayMapFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace PHPStan\Type\Php;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
Expand All @@ -13,10 +15,10 @@
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use function array_map;
use function array_slice;
use function count;

Expand All @@ -38,12 +40,18 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$callableType = $scope->getType($functionCall->getArgs()[0]->value);
$callableIsNull = $callableType->isNull()->yes();

$callableParametersAcceptors = null;

if ($callableType->isCallable()->yes()) {
$valueTypes = [new NeverType()];
foreach ($callableType->getCallableParametersAcceptors($scope) as $parametersAcceptor) {
$valueTypes[] = $parametersAcceptor->getReturnType();
}
$valueType = TypeCombinator::union(...$valueTypes);
$callableParametersAcceptors = $callableType->getCallableParametersAcceptors($scope);
$valueType = ParametersAcceptorSelector::selectFromTypes(
array_map(
static fn (Node\Arg $arg) => $scope->getType($arg->value)->getIterableValueType(),
array_slice($functionCall->getArgs(), 1),
),
$callableParametersAcceptors,
false,
)->getReturnType();
} elseif ($callableIsNull) {
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
foreach (array_slice($functionCall->getArgs(), 1) as $index => $arg) {
Expand All @@ -70,10 +78,17 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
if ($totalCount < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
foreach ($constantArrays as $constantArray) {
$returnedArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
$valueTypes = $constantArray->getValueTypes();
foreach ($constantArray->getKeyTypes() as $i => $keyType) {
$returnedArrayBuilder->setOffsetValueType(
$keyType,
$valueType,
$callableParametersAcceptors !== null
? ParametersAcceptorSelector::selectFromTypes(
[$valueTypes[$i]],
$callableParametersAcceptors,
false,
)->getReturnType()
: $valueType,
$constantArray->isOptionalKey($i),
);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1049,4 +1049,9 @@ public function testBug11337(): void
$this->analyse([__DIR__ . '/data/bug-11337.php'], []);
}

public function testBug10715(): void
{
$this->analyse([__DIR__ . '/data/bug-10715.php'], []);
}

}
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-10715.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace Bug10715;

class Test
{
/**
* @param string|array<string> $word
*
* @return ($word is array<string> ? array<string> : string)
*/
public static function wgtrim(string|array $word): string|array
{
if (\is_array($word)) {
return array_map(static::wgtrim(...), $word);
}

return 'word';
}

/**
* @param array{foo: array<string>, bar: string} $array
*
* @return array{foo: array<string>, bar: string}
*/
public static function example(array $array): array
{
return array_map(static::wgtrim(...), $array);
}
}
Loading