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
54 changes: 52 additions & 2 deletions src/Type/Php/ArrayMapFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use function array_map;
use function array_reduce;
use function array_slice;
use function count;

Expand All @@ -32,7 +33,8 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if (count($functionCall->getArgs()) < 2) {
$numArgs = count($functionCall->getArgs());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, i see multiple call function $functionCall->getArgs(). Can refactor?

if ($numArgs < 2) {
return null;
}

Expand All @@ -54,10 +56,58 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
)->getReturnType();
} elseif ($callableIsNull) {
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
$argTypes = [];
$areAllSameSize = true;
$expectedSize = null;
foreach (array_slice($functionCall->getArgs(), 1) as $index => $arg) {
$argTypes[$index] = $argType = $scope->getType($arg->value);
if (!$areAllSameSize || $numArgs === 2) {
continue;
}

$arraySizes = $argType->getArraySize()->getConstantScalarValues();
if ($arraySizes === []) {
$areAllSameSize = false;
continue;
}

foreach ($arraySizes as $size) {
$expectedSize ??= $size;
if ($expectedSize === $size) {
continue;
}

$areAllSameSize = false;
continue 2;
}
}

if (!$areAllSameSize) {
$firstArr = $functionCall->getArgs()[1]->value;
$identities = [];
foreach (array_slice($functionCall->getArgs(), 2) as $arg) {
$identities[] = new Node\Expr\BinaryOp\Identical($firstArr, $arg->value);
}

$and = array_reduce(
$identities,
static fn (Node\Expr $a, Node\Expr $b) => new Node\Expr\BinaryOp\BooleanAnd($a, $b),
new Node\Expr\ConstFetch(new Node\Name('true')),
);
$areAllSameSize = $scope->getType($and)->isTrue()->yes();
}

$addNull = !$areAllSameSize;

foreach ($argTypes as $index => $argType) {
$offsetValueType = $argType->getIterableValueType();
if ($addNull) {
$offsetValueType = TypeCombinator::addNull($offsetValueType);
}

$arrayBuilder->setOffsetValueType(
new ConstantIntegerType($index),
$scope->getType($arg->value)->getIterableValueType(),
$offsetValueType,
);
}
$valueType = $arrayBuilder->getArray();
Expand Down
6 changes: 5 additions & 1 deletion tests/PHPStan/Analyser/nsrt/array_map_multiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public function arrayMapNull(array $array, array $other): void

assertType('non-empty-array<string, int>', array_map(null, $array));
assertType('non-empty-array<int, array{int, int}>', array_map(null, $array, $array));
assertType('non-empty-array<int, array{int, bool}>', array_map(null, $array, $other));
assertType('non-empty-array<int, array{int, int, int}>', array_map(null, $array, $array, $array));
assertType('non-empty-array<int, array{int|null, bool|null}>', array_map(null, $array, $other));

assertType('array{1}|array{true}', array_map(null, rand() ? [1] : [true]));
assertType('array{1}|array{true, false}', array_map(null, rand() ? [1] : [true, false]));
}

}
Loading