diff --git a/src/Analyser/ArgumentsNormalizer.php b/src/Analyser/ArgumentsNormalizer.php index d9a5e84f168..d5c217116a5 100644 --- a/src/Analyser/ArgumentsNormalizer.php +++ b/src/Analyser/ArgumentsNormalizer.php @@ -305,13 +305,17 @@ public static function reorderNewArguments( */ public static function getArgByPositionOrName(array $args, int $position, string $name): ?Arg { + $argByPosition = null; foreach ($args as $i => $arg) { - if ($arg->name === null ? $i === $position : $arg->name->toString() === $name) { + if ($i === $position) { + $argByPosition = $arg; + } + if ($arg->name !== null && $arg->name->toString() === $name) { return $arg; } } - return null; + return $argByPosition; } /** diff --git a/src/Rules/Functions/ArrayFilterRule.php b/src/Rules/Functions/ArrayFilterRule.php index f7c32cc2c72..f215ae6564e 100644 --- a/src/Rules/Functions/ArrayFilterRule.php +++ b/src/Rules/Functions/ArrayFilterRule.php @@ -8,7 +8,6 @@ use PHPStan\Analyser\Scope; use PHPStan\DependencyInjection\AutowiredParameter; use PHPStan\DependencyInjection\RegisteredRule; -use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; @@ -54,27 +53,20 @@ public function processNode(Node $node, Scope $scope): array return []; } - $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( - $scope, - $node->getArgs(), - $functionReflection->getVariants(), - $functionReflection->getNamedArgumentsVariants(), - ); - - $normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node); - if ($normalizedFuncCall === null) { + $args = $node->getArgs(); + if (count($args) !== 1) { return []; } - $args = $normalizedFuncCall->getArgs(); - if (count($args) !== 1) { + $arrayArg = ArgumentsNormalizer::getArgByPositionOrName($node->getArgs(), 0, 'array'); + if ($arrayArg === null) { return []; } if ($this->treatPhpDocTypesAsCertain) { - $arrayType = $scope->getType($args[0]->value); + $arrayType = $scope->getType($arrayArg->value); } else { - $arrayType = $scope->getNativeType($args[0]->value); + $arrayType = $scope->getNativeType($arrayArg->value); } if ($arrayType->isIterableAtLeastOnce()->no()) { @@ -84,7 +76,7 @@ public function processNode(Node $node, Scope $scope): array $arrayType->describe(VerbosityLevel::value()), ))->identifier('arrayFilter.empty'); if ($this->treatPhpDocTypesAsCertain) { - $nativeArrayType = $scope->getNativeType($args[0]->value); + $nativeArrayType = $scope->getNativeType($arrayArg->value); if ($this->treatPhpDocTypesAsCertainTip && !$nativeArrayType->isIterableAtLeastOnce()->no()) { $errorBuilder->treatPhpDocTypesAsCertainTip(); } diff --git a/src/Rules/Functions/ArrayValuesRule.php b/src/Rules/Functions/ArrayValuesRule.php index 0285a5c983f..aac578c33cc 100644 --- a/src/Rules/Functions/ArrayValuesRule.php +++ b/src/Rules/Functions/ArrayValuesRule.php @@ -8,12 +8,10 @@ use PHPStan\Analyser\Scope; use PHPStan\DependencyInjection\AutowiredParameter; use PHPStan\DependencyInjection\RegisteredRule; -use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Type\VerbosityLevel; -use function count; use function sprintf; /** @@ -53,28 +51,15 @@ public function processNode(Node $node, Scope $scope): array return []; } - $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( - $scope, - $node->getArgs(), - $functionReflection->getVariants(), - $functionReflection->getNamedArgumentsVariants(), - ); - - $normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node); - - if ($normalizedFuncCall === null) { - return []; - } - - $args = $normalizedFuncCall->getArgs(); - if (count($args) === 0) { + $arrayArg = ArgumentsNormalizer::getArgByPositionOrName($node->getArgs(), 0, 'array'); + if ($arrayArg === null) { return []; } if ($this->treatPhpDocTypesAsCertain) { - $arrayType = $scope->getType($args[0]->value); + $arrayType = $scope->getType($arrayArg->value); } else { - $arrayType = $scope->getNativeType($args[0]->value); + $arrayType = $scope->getNativeType($arrayArg->value); } if ($arrayType->isIterableAtLeastOnce()->no()) { @@ -84,7 +69,7 @@ public function processNode(Node $node, Scope $scope): array $arrayType->describe(VerbosityLevel::value()), ))->identifier('arrayValues.empty'); if ($this->treatPhpDocTypesAsCertain) { - $nativeArrayType = $scope->getNativeType($args[0]->value); + $nativeArrayType = $scope->getNativeType($arrayArg->value); if ($this->treatPhpDocTypesAsCertainTip && !$nativeArrayType->isIterableAtLeastOnce()->no()) { $errorBuilder->treatPhpDocTypesAsCertainTip(); } @@ -102,7 +87,7 @@ public function processNode(Node $node, Scope $scope): array $arrayType->describe(VerbosityLevel::value()), ))->identifier('arrayValues.list'); if ($this->treatPhpDocTypesAsCertain) { - $nativeArrayType = $scope->getNativeType($args[0]->value); + $nativeArrayType = $scope->getNativeType($arrayArg->value); if ($this->treatPhpDocTypesAsCertainTip && !$nativeArrayType->isList()->yes()) { $errorBuilder->treatPhpDocTypesAsCertainTip(); } diff --git a/tests/PHPStan/Analyser/ArgumentsNormalizerTest.php b/tests/PHPStan/Analyser/ArgumentsNormalizerTest.php index 6d851071f06..364748ce902 100644 --- a/tests/PHPStan/Analyser/ArgumentsNormalizerTest.php +++ b/tests/PHPStan/Analyser/ArgumentsNormalizerTest.php @@ -414,12 +414,12 @@ public static function dataGetArgByPositionOrName(): iterable // positional arguments followed by the named one yield [[null, 'mode', 'callback'], 1, 'callback', 2]; - // named argument at the position is bound to a different parameter - yield [['callback'], 0, 'array', null]; - yield [[null, 'mode'], 1, 'callback', null]; + // fall back to the argument at the position when the named argument is for a different parameter + yield [['callback'], 0, 'array', 0]; + yield [[null, 'mode'], 1, 'callback', 1]; - // argument names are case-sensitive - yield [['Array'], 0, 'array', null]; + // argument names are case-sensitive, but fall back to the argument at the position + yield [['Array'], 0, 'array', 0]; } /** diff --git a/turbo-ext/src/ParserVisitors.h b/turbo-ext/src/ParserVisitors.h index dd208318813..ebd030c3027 100644 --- a/turbo-ext/src/ParserVisitors.h +++ b/turbo-ext/src/ParserVisitors.h @@ -242,18 +242,21 @@ inline zend_object *argForParameter(zval *args, zend_ulong position, const char static NodeProp identifierProp = PT_IDENTIFIER_PROP; if (args == NULL || Z_TYPE_P(args) != IS_ARRAY) return NULL; + zend_object *argByPosition = NULL; for (auto entry : zv::ArrRef(args)) { zv::Ref arg = entry.value().deref(); if (!arg.isObject()) continue; + if (entry.stringKeyOrNull() == NULL && entry.indexKey() == position) { + argByPosition = arg.asObject(); + } + zval *argName = argNameProp.of(arg.asObject()); if (argName != NULL && Z_TYPE_P(argName) == IS_OBJECT) { zend_string *argNameString = nameString(Z_OBJ_P(argName), identifierProp); if (argNameString != NULL && zend_string_equals_cstr(argNameString, name, nameLen)) return arg.asObject(); - continue; } - if (entry.stringKeyOrNull() == NULL && entry.indexKey() == position) return arg.asObject(); } - return NULL; + return argByPosition; } template