2020use PHPStan \Reflection \MethodReflection ;
2121use PHPStan \Reflection \ParameterReflection ;
2222use PHPStan \Reflection \ParametersAcceptor ;
23+ use PHPStan \Reflection \ParametersAcceptorSelector ;
2324use PHPStan \Reflection \PartialFunctionApplicationParameters ;
2425use PHPStan \Reflection \ReflectionProvider ;
2526use PHPStan \Reflection \ResolvedFunctionVariant ;
4243use PHPStan \Type \VerbosityLevel ;
4344use function array_fill ;
4445use function array_key_exists ;
46+ use function array_keys ;
4547use function array_last ;
4648use function array_merge ;
4749use function count ;
@@ -88,6 +90,7 @@ public function __construct(
8890 /**
8991 * @param 'attribute'|'callable'|'method'|'staticMethod'|'function'|'new' $nodeType
9092 * @param array{class-string, string}|null $renamedNamedArgumentParameterData
93+ * @param list<ParametersAcceptor> $namedCallVariants positional variants of a callee whose named arguments resolve against its php-src signature
9194 * @return list<IdentifierRuleError>
9295 */
9396 public function check (
@@ -117,6 +120,7 @@ public function check(
117120 string $ bitmaskNotAllowedMessage ,
118121 ?array $ renamedNamedArgumentParameterData ,
119122 FunctionReflection |MethodReflection |null $ calleeReflection = null ,
123+ array $ namedCallVariants = [],
120124 ): array
121125 {
122126 if ($ funcCall instanceof Node \Expr \MethodCall || $ funcCall instanceof Node \Expr \StaticCall || $ funcCall instanceof Node \Expr \FuncCall) {
@@ -312,6 +316,7 @@ public function check(
312316 ->build ();
313317 }
314318
319+ $ argumentsCount = null ;
315320 if (!$ hasNamedArguments ) {
316321 $ invokedParametersCount = count ($ arguments );
317322 foreach ($ arguments as [2 => $ unpack ]) {
@@ -320,7 +325,13 @@ public function check(
320325 break ;
321326 }
322327 }
328+ $ argumentsCount = [$ invokedParametersCount , $ functionParametersMinCount , $ functionParametersMaxCount ];
329+ } elseif (!$ isPartialApplication ) {
330+ $ argumentsCount = self ::getNamedArgumentsCount ($ arguments , $ parametersAcceptor , $ namedCallVariants );
331+ }
323332
333+ if ($ argumentsCount !== null ) {
334+ [$ invokedParametersCount , $ functionParametersMinCount , $ functionParametersMaxCount ] = $ argumentsCount ;
324335 if (
325336 // ... of a partial application fills in the parameters left
326337 ($ invokedParametersCount < $ functionParametersMinCount && !$ hasVariadicPlaceholder )
@@ -1312,6 +1323,78 @@ public static function getVariantForArgumentsCount(array $args, array $variants,
13121323 return null ;
13131324 }
13141325
1326+ return self ::getClosestVariant ($ argsCount , $ variants );
1327+ }
1328+
1329+ /**
1330+ * PHP resolves named arguments against the one signature php-src stubs declare,
1331+ * passes the default of each parameter they skip, and then the function checks
1332+ * the number of arguments like for a positional call: mt_rand(min: 1) throws.
1333+ * So the count is checked against the variants like a positional call's.
1334+ *
1335+ * @param array<int, array{Expr|Node\ArgPlaceholder, Type|null, bool, string|null, int}> $arguments
1336+ * @param list<ParametersAcceptor> $variants
1337+ * @return array{int, int, int}|null the arguments count and the minimum and maximum (-1 when variadic) to report it against, null when a variant accepts it
1338+ */
1339+ private static function getNamedArgumentsCount (array $ arguments , ParametersAcceptor $ namedArgumentsVariant , array $ variants ): ?array
1340+ {
1341+ if (count ($ variants ) < 2 ) {
1342+ return null ;
1343+ }
1344+
1345+ $ positionsByName = [];
1346+ foreach ($ namedArgumentsVariant ->getParameters () as $ i => $ parameter ) {
1347+ if ($ parameter ->isVariadic ()) {
1348+ continue ;
1349+ }
1350+ $ positionsByName [$ parameter ->getName ()] = $ i ;
1351+ }
1352+
1353+ $ passedPositions = [];
1354+ foreach ($ arguments as $ i => [, , $ unpack , $ argumentName ]) {
1355+ if ($ unpack ) {
1356+ return null ;
1357+ }
1358+ if ($ argumentName === null ) {
1359+ $ passedPositions [$ i ] = true ;
1360+ continue ;
1361+ }
1362+ if (!array_key_exists ($ argumentName , $ positionsByName )) {
1363+ // reported as an unknown parameter
1364+ return null ;
1365+ }
1366+ $ passedPositions [$ positionsByName [$ argumentName ]] = true ;
1367+ }
1368+
1369+ $ argsCount = $ passedPositions === [] ? 0 : max (array_keys ($ passedPositions )) + 1 ;
1370+ foreach ($ namedArgumentsVariant ->getParameters () as $ i => $ parameter ) {
1371+ if ($ i >= $ argsCount ) {
1372+ break ;
1373+ }
1374+ if (!array_key_exists ($ i , $ passedPositions ) && $ parameter ->getDefaultValue () instanceof ErrorType) {
1375+ // PHP throws before the function checks the count
1376+ return null ;
1377+ }
1378+ }
1379+
1380+ $ combinedVariant = ParametersAcceptorSelector ::combineAcceptors ($ variants );
1381+ $ variant = self ::acceptsArgumentsCount ($ combinedVariant , $ argsCount )
1382+ ? self ::getClosestVariant ($ argsCount , $ variants )
1383+ : $ combinedVariant ;
1384+ if ($ variant === null ) {
1385+ return null ;
1386+ }
1387+
1388+ [$ minCount , $ maxCount ] = self ::getArgumentsCountRange ($ variant );
1389+
1390+ return [$ argsCount , $ minCount , $ maxCount ?? -1 ];
1391+ }
1392+
1393+ /**
1394+ * @param list<ParametersAcceptor> $variants
1395+ */
1396+ private static function getClosestVariant (int $ argsCount , array $ variants ): ?ParametersAcceptor
1397+ {
13151398 $ closestVariant = null ;
13161399 $ closestDistance = null ;
13171400 foreach ($ variants as $ variant ) {
0 commit comments