Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\PassedByReference;
use PHPStan\Reflection\Php\DummyParameter;
use PHPStan\Reflection\UnpackedArgumentTypes;
use PHPStan\Turbo\ShadowedByTurboExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
Expand All @@ -26,6 +27,7 @@
use function array_key_exists;
use function array_map;
use function count;
use function in_array;
use function max;

/**
Expand Down Expand Up @@ -93,40 +95,53 @@ public function resolve(

$types = [];
$nativeTypes = [];
$arrayTypes = [];
$nativeArrayTypes = [];
$hasUnpackedArg = false;
$unpacked = [];
foreach ($intrinsicArgs as $arg) {
$hasUnpackedArg = $hasUnpackedArg || $arg->unpack;
$result = $storage?->findExpressionResult($arg->value);
$type = $result !== null ? $result->getType() : $this->nodeScopeResolver->readScopeStateOrSyntheticType($arg->value, $scope);
$nativeType = $result !== null ? $result->getNativeType() : $this->nodeScopeResolver->readScopeStateOrSyntheticType($arg->value, $scope->doNotTreatPhpDocTypesAsCertain());
if ($arrayMapArgs !== null) {
$arrayTypes[] = $type;
$nativeArrayTypes[] = $nativeType;
$type = $type->getIterableValueType();
$nativeType = $nativeType->getIterableValueType();
}
$types[] = $type;
$nativeTypes[] = $nativeType;
$types[] = $result !== null ? $result->getType() : $this->nodeScopeResolver->readScopeStateOrSyntheticType($arg->value, $scope);
$nativeTypes[] = $result !== null ? $result->getNativeType() : $this->nodeScopeResolver->readScopeStateOrSyntheticType($arg->value, $scope->doNotTreatPhpDocTypesAsCertain());
$unpacked[] = $arg->unpack;
}

return new ClosureParameterTypes(
$this->createIntrinsicParameters($types, $unpacked, $arrayMapArgs !== null),
$this->createIntrinsicParameters($nativeTypes, $unpacked, $arrayMapArgs !== null),
);
}

/**
* @param list<Type> $argTypes
* @param list<bool> $unpacked
* @return list<DummyParameter>
*/
private function createIntrinsicParameters(array $argTypes, array $unpacked, bool $isArrayMap): array
{
$restType = null;
if (in_array(true, $unpacked, true)) {
[$argTypes, $restType] = UnpackedArgumentTypes::expand($argTypes, $unpacked);
}

// an unpacked arg is one parameter here, so its arrays cannot be told apart
if ($arrayMapArgs !== null && !$hasUnpackedArg) {
$types = ArrayMapCallbackParameterTypes::addPaddingNull($types, $arrayTypes);
$nativeTypes = ArrayMapCallbackParameterTypes::addPaddingNull($nativeTypes, $nativeArrayTypes);
$types = $argTypes;
if ($isArrayMap) {
$types = [];
foreach ($argTypes as $argType) {
$types[] = $argType->getIterableValueType();
}
// with a rest, the number of arrays is not known
if ($restType === null) {
$types = ArrayMapCallbackParameterTypes::addPaddingNull($types, $argTypes);
}
}

$parameters = [];
foreach ($types as $type) {
$parameters[] = new DummyParameter('item', $type, optional: false, passedByReference: PassedByReference::createNo(), variadic: false, defaultValue: null);
}
$nativeParameters = [];
foreach ($nativeTypes as $nativeType) {
$nativeParameters[] = new DummyParameter('item', $nativeType, optional: false, passedByReference: PassedByReference::createNo(), variadic: false, defaultValue: null);
if ($restType !== null) {
$parameters[] = new DummyParameter('item', $isArrayMap ? $restType->getIterableValueType() : $restType, optional: true, passedByReference: PassedByReference::createNo(), variadic: true, defaultValue: null);
}

return new ClosureParameterTypes($parameters, $nativeParameters);
return $parameters;
}

/**
Expand Down
86 changes: 41 additions & 45 deletions src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
use function constant;
use function count;
use function defined;
use function in_array;
use function is_int;
use function is_string;
use function sprintf;
Expand Down Expand Up @@ -232,62 +233,29 @@ public static function applyIntrinsicArgOverrides(
$positionalArgs = self::reorderIntrinsicArgs($args, $namedArgumentsVariants[0] ?? $parametersAcceptors[0]);
$arrayMapArgs = $positionalArgs[0]->value->getAttribute(ArrayMapArgVisitor::ATTRIBUTE_NAME);
if ($arrayMapArgs !== null) {
$callbackValueTypes = [];
$callbackArrayTypes = [];
$nativeCallbackValueTypes = [];
$nativeCallbackArrayTypes = [];
$argTypes = [];
$nativeArgTypes = [];
$unpacked = [];
foreach ($arrayMapArgs as $arg) {
$argType = ($typeGetter)($arg->value);
$nativeArgType = ($nativeTypeGetter)($arg->value);
if ($arg->unpack) {
$constantArrays = $argType->getConstantArrays();
if (count($constantArrays) > 0) {
foreach ($constantArrays as $constantArray) {
$valueTypes = $constantArray->getValueTypes();
foreach ($valueTypes as $valueType) {
$callbackValueTypes[] = ($iterableValueTypeGetter)($valueType);
$callbackArrayTypes[] = $valueType;
}
}
}
$nativeConstantArrays = $nativeArgType->getConstantArrays();
if (count($nativeConstantArrays) > 0) {
foreach ($nativeConstantArrays as $constantArray) {
$valueTypes = $constantArray->getValueTypes();
foreach ($valueTypes as $valueType) {
$nativeCallbackValueTypes[] = ($iterableValueTypeGetter)($valueType);
$nativeCallbackArrayTypes[] = $valueType;
}
}
}
} else {
$callbackValueTypes[] = ($iterableValueTypeGetter)($argType);
$callbackArrayTypes[] = $argType;
$nativeCallbackValueTypes[] = ($iterableValueTypeGetter)($nativeArgType);
$nativeCallbackArrayTypes[] = $nativeArgType;
}
}

$callbackParameters = [];
foreach (ArrayMapCallbackParameterTypes::addPaddingNull($callbackValueTypes, $callbackArrayTypes) as $valueType) {
$callbackParameters[] = new DummyParameter('item', $valueType, optional: false, passedByReference: PassedByReference::createNo(), variadic: false, defaultValue: null);
}
$nativeCallbackParameters = [];
foreach (ArrayMapCallbackParameterTypes::addPaddingNull($nativeCallbackValueTypes, $nativeCallbackArrayTypes) as $valueType) {
$nativeCallbackParameters[] = new DummyParameter('item', $valueType, optional: false, passedByReference: PassedByReference::createNo(), variadic: false, defaultValue: null);
$argTypes[] = ($typeGetter)($arg->value);
$nativeArgTypes[] = ($nativeTypeGetter)($arg->value);
$unpacked[] = $arg->unpack;
}
$callbackParameters = self::createArrayMapCallbackParameters($argTypes, $unpacked, $iterableValueTypeGetter);
$nativeCallbackParameters = self::createArrayMapCallbackParameters($nativeArgTypes, $unpacked, $iterableValueTypeGetter);

$acceptor = $parametersAcceptors[0];
$parameters = $acceptor->getParameters();
if (isset($parameters[0])) {
// with an unknown number of arrays, the callback keeps the type from the signature
if (isset($parameters[0]) && $callbackParameters !== null) {
$callableType = new UnionType([
new CallableType($callbackParameters, new MixedType(), false),
new NullType(),
]);
$nativeCallableType = new UnionType([
$nativeCallableType = $nativeCallbackParameters !== null ? new UnionType([
new CallableType($nativeCallbackParameters, new MixedType(), false),
new NullType(),
]);
]) : self::wrapParameter($parameters[0])->getNativeType();
$parameters[0] = self::overrideParameterType($parameters[0], $callableType, $nativeCallableType);
$parametersAcceptors = [self::overrideAcceptorParameters($acceptor, $parameters)];
}
Expand Down Expand Up @@ -1425,6 +1393,34 @@ private static function getCurlOptValueType(int $curlOpt): ?Type
return null;
}

/**
* @param list<Type> $argTypes
* @param list<bool> $unpacked
* @param Closure(Type): Type $iterableValueTypeGetter
* @return list<DummyParameter>|null null when the number of arrays is not known
*/
private static function createArrayMapCallbackParameters(array $argTypes, array $unpacked, Closure $iterableValueTypeGetter): ?array
{
if (in_array(true, $unpacked, true)) {
[$argTypes, $restType] = UnpackedArgumentTypes::expand($argTypes, $unpacked);
if ($restType !== null) {
return null;
}
}

$valueTypes = [];
foreach ($argTypes as $argType) {
$valueTypes[] = ($iterableValueTypeGetter)($argType);
}

$parameters = [];
foreach (ArrayMapCallbackParameterTypes::addPaddingNull($valueTypes, $argTypes) as $valueType) {
$parameters[] = new DummyParameter('item', $valueType, optional: false, passedByReference: PassedByReference::createNo(), variadic: false, defaultValue: null);
}

return $parameters;
}

private static function overrideParameterType(ParameterReflection $original, Type $type, Type $nativeType): ExtendedDummyParameter
{
$wrapped = self::wrapParameter($original);
Expand Down
94 changes: 94 additions & 0 deletions src/Reflection/UnpackedArgumentTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection;

use PHPStan\Turbo\ReferencedByTurboExtension;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
use function min;

/**
* The types of the values a call passes, with each unpacked arg (`...$arg`)
* expanded into one value per item of its array.
*/
#[ReferencedByTurboExtension(key: 'unpackedArgumentTypes')]
final class UnpackedArgumentTypes
{

/**
* Returns the type of each value whose position is known, and the type of
* the values after them when their number is not known. That is the case
* after an unpacked arg that is not a list shape, or that is a list shape
* of more than one size.
*
* @param list<Type> $argTypes
* @param list<bool> $unpacked whether each arg is unpacked
* @return array{list<Type>, Type|null}
*/
public static function expand(array $argTypes, array $unpacked): array
{
$types = [];
$restTypes = [];
foreach ($argTypes as $i => $argType) {
if (!$unpacked[$i]) {
if ($restTypes !== []) {
// PHP does not allow a positional arg after an unpacked one
$restTypes[] = $argType;
continue;
}

$types[] = $argType;
continue;
}

if ($restTypes !== []) {
$restTypes[] = $argType->getIterableValueType();
continue;
}

$constantArrays = $argType->getConstantArrays();
$shapesAreLists = true;
foreach ($constantArrays as $constantArray) {
if ($constantArray->isList()->yes()) {
continue;
}

// string keys are passed as named args
$shapesAreLists = false;
break;
}

if (!$shapesAreLists || $constantArrays === []) {
$restTypes[] = $argType->getIterableValueType();
continue;
}

$requiredCounts = [];
foreach ($constantArrays as $constantArray) {
// the optional keys of a list are its last keys
$requiredCounts[] = count($constantArray->getValueTypes()) - count($constantArray->getOptionalKeys());
}
$requiredCount = min($requiredCounts);

for ($position = 0; $position < $requiredCount; $position++) {
$positionTypes = [];
foreach ($constantArrays as $constantArray) {
$positionTypes[] = $constantArray->getValueTypes()[$position];
}
$types[] = TypeCombinator::union(...$positionTypes);
}

foreach ($constantArrays as $constantArray) {
$valueTypes = $constantArray->getValueTypes();
$valueTypesCount = count($valueTypes);
for ($position = $requiredCount; $position < $valueTypesCount; $position++) {
$restTypes[] = $valueTypes[$position];
}
}
}

return [$types, $restTypes === [] ? null : TypeCombinator::union(...$restTypes)];
}

}
Loading