Skip to content

Commit

Permalink
Do not check variance validity in private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jiripudil authored and ondrejmirtes committed Dec 8, 2022
1 parent e4e5aa9 commit c9d76cb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
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) {}
}

0 comments on commit c9d76cb

Please sign in to comment.