Skip to content

Commit 07f6786

Browse files
committed
Check a partial application no variant fits against the closest variant
Combining the variants () and ($min, $max) of mt_rand() accepted mt_rand(?), so the count is checked against the closest variant, as for a call. Amp-Thread-ID: https://ampcode.com/threads/T-01a1c541-317f-7386-a2ed-9cbd66178685
1 parent 4d328c9 commit 07f6786

4 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎src/Reflection/PartialFunctionApplicationParameters.php‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,81 @@ public static function selectVariant(array $args, array $variants, ?array $named
351351
return $fitting[0];
352352
}
353353

354+
if (count($fitting) === 0) {
355+
$closestVariant = self::selectVariantClosestToArgumentsCount($args, $variants);
356+
if ($closestVariant !== null) {
357+
return $closestVariant;
358+
}
359+
}
360+
354361
return ParametersAcceptorSelector::combineAcceptors(count($fitting) > 0 ? $fitting : $variants);
355362
}
356363

364+
/**
365+
* Combining the variants () and ($min, $max) of mt_rand() would accept mt_rand(?),
366+
* so like a call, a count of positional arguments that the combined variants
367+
* accept but no variant does is checked against the variant closest to it.
368+
*
369+
* @param array<Arg|ArgPlaceholder|VariadicPlaceholder> $args
370+
* @param list<ParametersAcceptor> $variants
371+
*/
372+
private static function selectVariantClosestToArgumentsCount(array $args, array $variants): ?ParametersAcceptor
373+
{
374+
$argsCount = 0;
375+
$hasVariadicPlaceholder = false;
376+
foreach ($args as $arg) {
377+
if ($hasVariadicPlaceholder || ($arg instanceof Arg && $arg->unpack) || (!$arg instanceof VariadicPlaceholder && $arg->name !== null)) {
378+
return null;
379+
}
380+
if ($arg instanceof VariadicPlaceholder) {
381+
$hasVariadicPlaceholder = true;
382+
continue;
383+
}
384+
$argsCount++;
385+
}
386+
387+
$closestVariant = null;
388+
$closestDistance = null;
389+
$combinedMinCount = null;
390+
$combinedMaxCount = 0;
391+
foreach ($variants as $variant) {
392+
$minCount = 0;
393+
foreach ($variant->getParameters() as $parameter) {
394+
if ($parameter->isOptional()) {
395+
continue;
396+
}
397+
$minCount++;
398+
}
399+
$maxCount = $variant->isVariadic() ? null : count($variant->getParameters());
400+
$combinedMinCount = min($combinedMinCount ?? $minCount, $minCount);
401+
$combinedMaxCount = $maxCount === null || $combinedMaxCount === null ? null : max($combinedMaxCount, $maxCount);
402+
403+
// the variadic placeholder fills in the parameters left
404+
$isInsufficient = !$hasVariadicPlaceholder && $argsCount < $minCount;
405+
if (!$isInsufficient && ($maxCount === null || $argsCount <= $maxCount)) {
406+
// the count fits, the arguments do not for another reason
407+
return null;
408+
}
409+
410+
$distance = $isInsufficient ? $minCount - $argsCount : $argsCount - $maxCount;
411+
if ($closestDistance !== null && ($distance > $closestDistance || ($distance === $closestDistance && !$isInsufficient))) {
412+
continue;
413+
}
414+
415+
$closestVariant = $variant;
416+
$closestDistance = $distance;
417+
}
418+
419+
if (
420+
(!$hasVariadicPlaceholder && $argsCount < $combinedMinCount)
421+
|| ($combinedMaxCount !== null && $argsCount > $combinedMaxCount)
422+
) {
423+
return null;
424+
}
425+
426+
return $closestVariant;
427+
}
428+
357429
private static function required(ParameterReflection $parameter, string $name): ParameterReflection
358430
{
359431
if ($parameter instanceof ExtendedParameterReflection) {

‎tests/PHPStan/Analyser/nsrt/partial-function-application-signature.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ function functions(): void
152152
assertType('static-Closure(string): (non-empty-string|false)', strtok(?));
153153
assertType('static-Closure(string, string): (non-empty-string|false)', strtok(?, ?));
154154
assertType('static-Closure(string): int<0, max>', strlen(?));
155+
// PHP creates it from the declared mt_rand(int $min = UNKNOWN, int $max = UNKNOWN), calling it throws
155156
assertType('Closure', mt_rand(?));
156157

157158
// templates

‎tests/PHPStan/Rules/Functions/FunctionCallableRuleTest.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,14 @@ public function testPartialFunctionApplicationArguments(): void
264264
'Variadic placeholder must be last.',
265265
91,
266266
],
267+
[
268+
'Function mt_rand partially applied with 1 argument, 2 required.',
269+
97,
270+
],
271+
[
272+
'Function mt_rand partially applied with 3 arguments, 0-2 required.',
273+
98,
274+
],
267275
]);
268276
}
269277

‎tests/PHPStan/Rules/Functions/data/partial-function-application-arguments.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,12 @@ function variadicPlaceholderPlacements(): void
9090
doFoo(1, ..., ..., ?);
9191
doFoo(1, ..., c: 5.0);
9292
}
93+
94+
function multiVariant(): void
95+
{
96+
// no variant of mt_rand() takes 1 or 3 arguments
97+
mt_rand(?);
98+
mt_rand(?, ?, ?, ...);
99+
mt_rand(1, ?);
100+
mt_rand(...);
101+
}

0 commit comments

Comments
 (0)