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

Do not check variance validity in private methods #2064

Merged
merged 1 commit into from Dec 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Rules/Generics/FunctionSignatureVarianceRule.php
Expand Up @@ -40,6 +40,7 @@ public function processNode(Node $node, Scope $scope): array
sprintf('in return type of function %s()', $functionName),
sprintf('in function %s()', $functionName),
false,
false,
);
}

Expand Down
1 change: 1 addition & 0 deletions src/Rules/Generics/MethodSignatureVarianceRule.php
Expand Up @@ -39,6 +39,7 @@ public function processNode(Node $node, Scope $scope): array
sprintf('in return type of method %s::%s()', $method->getDeclaringClass()->getDisplayName(), $method->getName()),
sprintf('in method %s::%s()', $method->getDeclaringClass()->getDisplayName(), $method->getName()),
$method->getName() === '__construct' || $method->isStatic(),
$method->isPrivate(),
);
}

Expand Down
27 changes: 16 additions & 11 deletions src/Rules/Generics/VarianceCheck.php
Expand Up @@ -20,21 +20,11 @@ public function checkParametersAcceptor(
string $returnTypeMessage,
string $generalMessage,
bool $isStatic,
bool $isPrivate,
): array
{
$errors = [];

foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
$variance = $isStatic
? TemplateTypeVariance::createStatic()
: TemplateTypeVariance::createContravariant();
$type = $parameterReflection->getType();
$message = sprintf($parameterTypeMessage, $parameterReflection->getName());
foreach ($this->check($variance, $type, $message) as $error) {
$errors[] = $error;
}
}

foreach ($parametersAcceptor->getTemplateTypeMap()->getTypes() as $templateType) {
if (!$templateType instanceof TemplateType
|| $templateType->getScope()->getFunctionName() === null
Expand All @@ -50,6 +40,21 @@ public function checkParametersAcceptor(
))->build();
}

if ($isPrivate) {
return $errors;
}

foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
$variance = $isStatic
? TemplateTypeVariance::createStatic()
: TemplateTypeVariance::createContravariant();
$type = $parameterReflection->getType();
$message = sprintf($parameterTypeMessage, $parameterReflection->getName());
foreach ($this->check($variance, $type, $message) as $error) {
$errors[] = $error;
}
}

$variance = TemplateTypeVariance::createCovariant();
$type = $parametersAcceptor->getReturnType();
foreach ($this->check($variance, $type, $returnTypeMessage) as $error) {
Expand Down
Expand Up @@ -69,4 +69,7 @@ function l() {}

/** @return Invariant<Out<X>> */
function m() {}

/** @return X */
private function n() {}
}
Expand Up @@ -69,4 +69,7 @@ function l() {}

/** @return Invariant<Out<X>> */
function m() {}

/** @param X $n */
private function n($n) {}
}