Skip to content

Improve return type of array map #4068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 2.1.x
Choose a base branch
from
Open
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
62 changes: 47 additions & 15 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2335,12 +2335,31 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
return new ErrorType();
}

return ParametersAcceptorSelector::selectFromArgs(
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$this,
$node->getArgs(),
$calledOnType->getCallableParametersAcceptors($this),
null,
)->getReturnType();
);

$functionName = null;
if ($node->name instanceof String_) {
/** @var non-empty-string $name */
$name = $node->name->value;
$functionName = new Name($name);
} elseif ($node->name instanceof FuncCall && $node->name->name instanceof Name) {
$functionName = $node->name->name;
}

if ($functionName !== null && $this->reflectionProvider->hasFunction($functionName, $this)) {
$functionReflection = $this->reflectionProvider->getFunction($functionName, $this);
$resolvedType = $this->getDynamicFunctionReturnType($parametersAcceptor, $node, $functionReflection);
if ($resolvedType !== null) {
return $resolvedType;
}
}

return $parametersAcceptor->getReturnType();
}

if (!$this->reflectionProvider->hasFunction($node->name, $this)) {
Expand Down Expand Up @@ -2369,19 +2388,9 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
);
$normalizedNode = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node);
if ($normalizedNode !== null) {
foreach ($this->dynamicReturnTypeExtensionRegistry->getDynamicFunctionReturnTypeExtensions() as $dynamicFunctionReturnTypeExtension) {
if (!$dynamicFunctionReturnTypeExtension->isFunctionSupported($functionReflection)) {
continue;
}

$resolvedType = $dynamicFunctionReturnTypeExtension->getTypeFromFunctionCall(
$functionReflection,
$normalizedNode,
$this,
);
if ($resolvedType !== null) {
return $resolvedType;
}
$resolvedType = $this->getDynamicFunctionReturnType($parametersAcceptor, $normalizedNode, $functionReflection);
if ($resolvedType !== null) {
return $resolvedType;
}
}

Expand All @@ -2391,6 +2400,29 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
return new MixedType();
}

private function getDynamicFunctionReturnType(ParametersAcceptor $parametersAcceptor, FuncCall $node, FunctionReflection $functionReflection): ?Type
{
$normalizedNode = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node);
if ($normalizedNode !== null) {
foreach ($this->dynamicReturnTypeExtensionRegistry->getDynamicFunctionReturnTypeExtensions() as $dynamicFunctionReturnTypeExtension) {
if (!$dynamicFunctionReturnTypeExtension->isFunctionSupported($functionReflection)) {
continue;
}

$resolvedType = $dynamicFunctionReturnTypeExtension->getTypeFromFunctionCall(
$functionReflection,
$node,
$this,
);
if ($resolvedType !== null) {
return $resolvedType;
}
}
}

return null;
}

private function getNullsafeShortCircuitingType(Expr $expr, Type $type): Type
{
if ($expr instanceof Expr\NullsafePropertyFetch || $expr instanceof Expr\NullsafeMethodCall) {
Expand Down
27 changes: 10 additions & 17 deletions src/Type/Php/ArrayMapFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
Expand Down Expand Up @@ -41,21 +41,18 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

$singleArrayArgument = !isset($functionCall->getArgs()[2]);
$callableType = $scope->getType($functionCall->getArgs()[0]->value);
$callback = $functionCall->getArgs()[0]->value;
$callableType = $scope->getType($callback);
$callableIsNull = $callableType->isNull()->yes();

$callableParametersAcceptors = null;

if ($callableType->isCallable()->yes()) {
$callableParametersAcceptors = $callableType->getCallableParametersAcceptors($scope);
$valueType = ParametersAcceptorSelector::selectFromTypes(
$valueType = $scope->getType(new FuncCall(
$callback,
array_map(
static fn (Node\Arg $arg) => $scope->getType($arg->value)->getIterableValueType(),
static fn (Node\Arg $arg) => new Node\Arg(new TypeExpr($scope->getType($arg->value)->getIterableValueType())),
array_slice($functionCall->getArgs(), 1),
),
$callableParametersAcceptors,
false,
)->getReturnType();
));
} elseif ($callableIsNull) {
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
$argTypes = [];
Expand Down Expand Up @@ -134,13 +131,9 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
foreach ($constantArray->getKeyTypes() as $i => $keyType) {
$returnedArrayBuilder->setOffsetValueType(
$keyType,
$callableParametersAcceptors !== null
? ParametersAcceptorSelector::selectFromTypes(
[$valueTypes[$i]],
$callableParametersAcceptors,
false,
)->getReturnType()
: $valueType,
$scope->getType(new FuncCall($callback, [
new Node\Arg(new TypeExpr($valueTypes[$i])),
])),
$constantArray->isOptionalKey($i),
);
}
Expand Down
45 changes: 45 additions & 0 deletions tests/PHPStan/Analyser/nsrt/array-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,48 @@ static function(string $string): string {

assertType('array{foo?: string, bar?: string, baz?: string}', $mapped);
}

class Foo
{
/**
* @template T of int
* @param T $n
* @return (T is 3 ? 'Fizz' : (T is 5 ? 'Buzz' : T))
*/
public static function fizzbuzz(int $n): int|string
{
return match ($n) {
3 => 'Fizz',
5 => 'Buzz',
default => $n,
};
}

public function doFoo(): void
{
$a = range(0, 1);

assertType("array{'0', '1'}", array_map('strval', $a));
assertType("array{'0', '1'}", array_map(strval(...), $a));
assertType("array{'0'|'1', '0'|'1'}", array_map(fn ($v) => strval($v), $a));
assertType("array{'0'|'1', '0'|'1'}", array_map(fn ($v) => (string)$v, $a));
}

public function doFizzBuzz(): void
{
assertType("array{1, 2, 'Fizz', 4, 'Buzz', 6}", array_map([__CLASS__, 'fizzbuzz'], range(1, 6)));
assertType("array{1, 2, 'Fizz', 4, 'Buzz', 6}", array_map([$this, 'fizzbuzz'], range(1, 6)));
assertType("array{1, 2, 'Fizz', 4, 'Buzz', 6}", array_map(self::fizzbuzz(...), range(1, 6)));
assertType("array{1, 2, 'Fizz', 4, 'Buzz', 6}", array_map($this->fizzbuzz(...), range(1, 6)));
}

/**
* @param array<string, 'a'|'b'|'A'|'B'> $array
*/
public function doUppercase(array $array): void
{
assertType("array<string, 'A'|'B'>", array_map(strtoupper(...), $array));
assertType("array{'A', 'B'}", array_map(strtoupper(...), ['A', 'B']));
}

}
Loading