Skip to content

Commit

Permalink
simplify property variance check by only supporting native properties
Browse files Browse the repository at this point in the history
  • Loading branch information
jiripudil committed Apr 2, 2023
1 parent 4b629cd commit 22dd356
Showing 1 changed file with 7 additions and 27 deletions.
34 changes: 7 additions & 27 deletions src/Rules/Generics/VarianceCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace PHPStan\Rules\Generics;

use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Generic\TemplateType;
Expand Down Expand Up @@ -89,37 +89,17 @@ public function checkParametersAcceptor(

/** @return RuleError[] */
public function checkProperty(
PropertyReflection $propertyReflection,
PhpPropertyReflection $propertyReflection,
string $message,
bool $isReadOnly,
): array
{
$readableType = $propertyReflection->getReadableType();
$writableType = $propertyReflection->getWritableType();
$type = $propertyReflection->getReadableType();
$variance = $isReadOnly
? TemplateTypeVariance::createCovariant()
: TemplateTypeVariance::createInvariant();

if ($readableType->equals($writableType)) {
$variance = $isReadOnly
? TemplateTypeVariance::createCovariant()
: TemplateTypeVariance::createInvariant();

return $this->check($variance, $readableType, $message);
}

$errors = [];

if ($propertyReflection->isReadable()) {
foreach ($this->check(TemplateTypeVariance::createCovariant(), $readableType, $message) as $error) {
$errors[] = $error;
}
}

if ($propertyReflection->isWritable()) {
foreach ($this->check(TemplateTypeVariance::createContravariant(), $writableType, $message) as $error) {
$errors[] = $error;
}
}

return $errors;
return $this->check($variance, $type, $message);
}

/** @return RuleError[] */
Expand Down

0 comments on commit 22dd356

Please sign in to comment.