Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parameter names for multi-variant functions #2726

Merged
merged 6 commits into from
Nov 14, 2023
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
1 change: 1 addition & 0 deletions src/Analyser/ArgumentsNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static function reorderCallUserFuncArguments(
$scope,
$passThruArgs,
$calledOnType->getCallableParametersAcceptors($scope),
null,
);

return [$parametersAcceptor, new FuncCall(
Expand Down
5 changes: 5 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,7 @@ private function resolveType(string $exprString, Expr $node): Type
$this,
$node->getArgs(),
$calledOnType->getCallableParametersAcceptors($this),
null,
)->getReturnType();
}

Expand All @@ -1910,6 +1911,7 @@ private function resolveType(string $exprString, Expr $node): Type
$this,
$node->getArgs(),
$functionReflection->getVariants(),
$functionReflection->getNamedArgumentsVariants(),
);
$normalizedNode = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node);
if ($normalizedNode !== null) {
Expand Down Expand Up @@ -4863,6 +4865,7 @@ private function exactInstantiation(New_ $node, string $className): ?Type
$this,
$methodCall->getArgs(),
$constructorMethod->getVariants(),
$constructorMethod->getNamedArgumentsVariants(),
);
$normalizedMethodCall = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $methodCall);

Expand Down Expand Up @@ -4937,6 +4940,7 @@ private function exactInstantiation(New_ $node, string $className): ?Type
$this,
$methodCall->getArgs(),
$constructorMethod->getVariants(),
$constructorMethod->getNamedArgumentsVariants(),
);

if ($this->explicitMixedInUnknownGenericNew) {
Expand Down Expand Up @@ -5022,6 +5026,7 @@ private function methodCallReturnType(Type $typeWithMethod, string $methodName,
$this,
$methodCall->getArgs(),
$methodReflection->getVariants(),
$methodReflection->getNamedArgumentsVariants(),
);
if ($methodCall instanceof MethodCall) {
$normalizedMethodCall = ArgumentsNormalizer::reorderMethodArguments($parametersAcceptor, $methodCall);
Expand Down
5 changes: 5 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,7 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
$scope,
$expr->getArgs(),
$nameType->getCallableParametersAcceptors($scope),
null,
);
}

Expand All @@ -2018,6 +2019,7 @@ static function (): void {
$scope,
$expr->getArgs(),
$functionReflection->getVariants(),
$functionReflection->getNamedArgumentsVariants(),
);
}

Expand Down Expand Up @@ -2215,6 +2217,7 @@ static function (): void {
$scope,
$expr->getArgs(),
$methodReflection->getVariants(),
$methodReflection->getNamedArgumentsVariants(),
);

$methodThrowPoint = $this->getMethodThrowPoint($methodReflection, $parametersAcceptor, $expr, $scope);
Expand Down Expand Up @@ -2326,6 +2329,7 @@ static function (): void {
$scope,
$expr->getArgs(),
$methodReflection->getVariants(),
$methodReflection->getNamedArgumentsVariants(),
);

$methodThrowPoint = $this->getStaticMethodThrowPoint($methodReflection, $parametersAcceptor, $expr, $scope);
Expand Down Expand Up @@ -2714,6 +2718,7 @@ static function (): void {
$scope,
$expr->getArgs(),
$constructorReflection->getVariants(),
$constructorReflection->getNamedArgumentsVariants(),
);
$constructorThrowPoint = $this->getConstructorThrowPoint($constructorReflection, $parametersAcceptor, $classReflection, $expr, $expr->class, $expr->getArgs(), $scope);
if ($constructorThrowPoint !== null) {
Expand Down
6 changes: 3 additions & 3 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ public function specifyTypesInCondition(
return $extension->specifyTypes($functionReflection, $expr, $scope, $context);
}

$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $functionReflection->getVariants());
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $functionReflection->getVariants(), $functionReflection->getNamedArgumentsVariants());
if (count($expr->getArgs()) > 0) {
$specifiedTypes = $this->specifyTypesFromConditionalReturnType($context, $expr, $parametersAcceptor, $scope);
if ($specifiedTypes !== null) {
Expand Down Expand Up @@ -550,7 +550,7 @@ public function specifyTypesInCondition(
}
}

$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $methodReflection->getVariants());
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $methodReflection->getVariants(), $methodReflection->getNamedArgumentsVariants());
if (count($expr->getArgs()) > 0) {
$specifiedTypes = $this->specifyTypesFromConditionalReturnType($context, $expr, $parametersAcceptor, $scope);
if ($specifiedTypes !== null) {
Expand Down Expand Up @@ -595,7 +595,7 @@ public function specifyTypesInCondition(
}
}

$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $staticMethodReflection->getVariants());
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $staticMethodReflection->getVariants(), $staticMethodReflection->getNamedArgumentsVariants());
if (count($expr->getArgs()) > 0) {
$specifiedTypes = $this->specifyTypesFromConditionalReturnType($context, $expr, $parametersAcceptor, $scope);
if ($specifiedTypes !== null) {
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Annotations/AnnotationMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public function getVariants(): array
return $this->variants;
}

public function getNamedArgumentsVariants(): ?array
{
return null;
}

public function isDeprecated(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Dummy/ChangedTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public function getVariants(): array
return $this->variants;
}

public function getNamedArgumentsVariants(): ?array
{
return null;
}

public function isDeprecated(): TrinaryLogic
{
return $this->reflection->isDeprecated();
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Dummy/DummyConstructorReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public function getVariants(): array
];
}

public function getNamedArgumentsVariants(): ?array
{
return null;
}

public function isDeprecated(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Dummy/DummyMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public function getVariants(): array
];
}

public function getNamedArgumentsVariants(): ?array
{
return null;
}

public function isDeprecated(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/ExtendedMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ interface ExtendedMethodReflection extends MethodReflection
*/
public function getVariants(): array;

/**
* @return ParametersAcceptorWithPhpDocs[]|null
*/
public function getNamedArgumentsVariants(): ?array;

public function getAsserts(): Assertions;

public function getSelfOutType(): ?Type;
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/FunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function getFileName(): ?string;
*/
public function getVariants(): array;

/**
* @return ParametersAcceptorWithPhpDocs[]|null
*/
public function getNamedArgumentsVariants(): ?array;

public function isDeprecated(): TrinaryLogic;

public function getDeprecatedDescription(): ?string;
Expand Down
7 changes: 7 additions & 0 deletions src/Reflection/Native/NativeFunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ class NativeFunctionReflection implements FunctionReflection

/**
* @param ParametersAcceptorWithPhpDocs[] $variants
* @param ParametersAcceptorWithPhpDocs[]|null $namedArgumentsVariants
*/
public function __construct(
private string $name,
private array $variants,
private ?array $namedArgumentsVariants,
private ?Type $throwType,
private TrinaryLogic $hasSideEffects,
private bool $isDeprecated,
Expand Down Expand Up @@ -51,6 +53,11 @@ public function getVariants(): array
return $this->variants;
}

public function getNamedArgumentsVariants(): ?array
{
return $this->namedArgumentsVariants;
}

public function getThrowType(): ?Type
{
return $this->throwType;
Expand Down
7 changes: 7 additions & 0 deletions src/Reflection/Native/NativeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ class NativeMethodReflection implements ExtendedMethodReflection

/**
* @param ParametersAcceptorWithPhpDocs[] $variants
* @param ParametersAcceptorWithPhpDocs[]|null $namedArgumentsVariants
*/
public function __construct(
private ReflectionProvider $reflectionProvider,
private ClassReflection $declaringClass,
private BuiltinMethodReflection $reflection,
private array $variants,
private ?array $namedArgumentsVariants,
private TrinaryLogic $hasSideEffects,
private ?Type $throwType,
private Assertions $assertions,
Expand Down Expand Up @@ -102,6 +104,11 @@ public function getVariants(): array
return $this->variants;
}

public function getNamedArgumentsVariants(): ?array
{
return $this->namedArgumentsVariants;
}

public function getDeprecatedDescription(): ?string
{
return null;
Expand Down
14 changes: 13 additions & 1 deletion src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ public static function selectSingle(
/**
* @param Node\Arg[] $args
* @param ParametersAcceptor[] $parametersAcceptors
* @param ParametersAcceptor[]|null $namedArgumentsVariants
*/
public static function selectFromArgs(
Scope $scope,
array $args,
array $parametersAcceptors,
?array $namedArgumentsVariants = null,
): ParametersAcceptor
{
$types = [];
Expand Down Expand Up @@ -231,9 +233,15 @@ public static function selectFromArgs(
}
}

$hasName = false;
foreach ($args as $i => $arg) {
$type = $scope->getType($arg->value);
$index = $arg->name !== null ? $arg->name->toString() : $i;
if ($arg->name !== null) {
$index = $arg->name->toString();
$hasName = true;
} else {
$index = $i;
}
if ($arg->unpack) {
$unpack = true;
$types[$index] = $type->getIterableValueType();
Expand All @@ -242,6 +250,10 @@ public static function selectFromArgs(
}
}

if ($hasName && $namedArgumentsVariants !== null) {
return self::selectFromTypes($types, $namedArgumentsVariants, $unpack);
}

return self::selectFromTypes($types, $parametersAcceptors, $unpack);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Php/ClosureCallMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public function getVariants(): array
];
}

public function getNamedArgumentsVariants(): ?array
{
return null;
}

public function isDeprecated(): TrinaryLogic
{
return $this->nativeMethodReflection->isDeprecated();
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Php/EnumCasesMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public function getVariants(): array
];
}

public function getNamedArgumentsVariants(): ?array
{
return null;
}

public function isDeprecated(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
Loading
Loading