Skip to content
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
62 changes: 36 additions & 26 deletions src/Rules/PhpDoc/VarTagTypeRuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IsSuperTypeOfResult;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -92,22 +93,25 @@
$errors = [];
$exprNativeType = $scope->getScopeNativeType($expr);
$containsPhpStanType = $this->containsPhpStanType($varTagType);
if ($this->shouldVarTagTypeBeReported($scope, $expr, $exprNativeType, $varTagType)) {

$isValidSuperTypeOfExpr = $this->isValidSuperTypeOfExpr($scope, $expr, $exprNativeType, $varTagType);
if (!$isValidSuperTypeOfExpr->yes()) {
$verbosity = VerbosityLevel::getRecommendedLevelByType($exprNativeType, $varTagType);
$errors[] = RuleErrorBuilder::message(sprintf(
'PHPDoc tag @var with type %s is not subtype of native type %s.',
$varTagType->describe($verbosity),
$exprNativeType->describe($verbosity),
))->identifier('varTag.nativeType')->build();
))->acceptsReasonsTip($isValidSuperTypeOfExpr->reasons)->identifier('varTag.nativeType')->build();
} elseif ($this->checkTypeAgainstPhpDocType || $containsPhpStanType) {
$exprType = $scope->getScopeType($expr);
if ($this->shouldVarTagTypeBeReported($scope, $expr, $exprType, $varTagType)) {
$isValidSuperTypeOfExpr = $this->isValidSuperTypeOfExpr($scope, $expr, $exprType, $varTagType);
if (!$isValidSuperTypeOfExpr->yes()) {
$verbosity = VerbosityLevel::getRecommendedLevelByType($exprType, $varTagType);
$errors[] = RuleErrorBuilder::message(sprintf(
'PHPDoc tag @var with type %s is not subtype of type %s.',
$varTagType->describe($verbosity),
$exprType->describe($verbosity),
))->identifier('varTag.type')->build();
))->acceptsReasonsTip($isValidSuperTypeOfExpr->reasons)->identifier('varTag.type')->build();
}
}

Expand Down Expand Up @@ -145,22 +149,22 @@
return false;
}

private function shouldVarTagTypeBeReported(Scope $scope, Node\Expr $expr, Type $type, Type $varTagType): bool
private function isValidSuperTypeOfExpr(Scope $scope, Node\Expr $expr, Type $type, Type $varTagType): IsSuperTypeOfResult
{
if ($expr instanceof Expr\Array_) {
if ($expr->items === []) {
$type = new ArrayType(new MixedType(), new MixedType());
}

return !$this->isAtLeastMaybeSuperTypeOfVarType($scope, $type, $varTagType);
return $this->isAtLeastMaybeSuperTypeOfVarType($scope, $type, $varTagType);
}

if ($expr instanceof Expr\ConstFetch) {
return !$this->isAtLeastMaybeSuperTypeOfVarType($scope, $type, $varTagType);
return $this->isAtLeastMaybeSuperTypeOfVarType($scope, $type, $varTagType);
}

if ($expr instanceof Node\Scalar) {
return !$this->isAtLeastMaybeSuperTypeOfVarType($scope, $type, $varTagType);
return $this->isAtLeastMaybeSuperTypeOfVarType($scope, $type, $varTagType);
}

if ($expr instanceof Expr\New_) {
Expand All @@ -169,72 +173,78 @@
}
}

return $this->checkType($scope, $type, $varTagType);
return $this->isValidSuperType($scope, $type, $varTagType);
}

private function checkType(Scope $scope, Type $type, Type $varTagType, int $depth = 0): bool
private function isValidSuperType(Scope $scope, Type $type, Type $varTagType, int $depth = 0): IsSuperTypeOfResult
{
if ($this->strictWideningCheck) {
return !$this->isSuperTypeOfVarType($scope, $type, $varTagType);
return $this->isSuperTypeOfVarType($scope, $type, $varTagType);
}

if ($type->isConstantArray()->yes()) {
if ($type->isIterableAtLeastOnce()->no()) {
$type = new ArrayType(new MixedType(), new MixedType());
return !$this->isAtLeastMaybeSuperTypeOfVarType($scope, $type, $varTagType);
return $this->isAtLeastMaybeSuperTypeOfVarType($scope, $type, $varTagType);
}
}

if ($type->isIterable()->yes() && $varTagType->isIterable()->yes()) {
if (!$this->isAtLeastMaybeSuperTypeOfVarType($scope, $type, $varTagType)) {
return true;
$isAtLeastMaybeSuperTypeOf = $this->isAtLeastMaybeSuperTypeOfVarType($scope, $type, $varTagType);
if ($isAtLeastMaybeSuperTypeOf->no()) {

Check warning on line 194 in src/Rules/PhpDoc/VarTagTypeRuleHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ($type->isIterable()->yes() && $varTagType->isIterable()->yes()) { $isAtLeastMaybeSuperTypeOf = $this->isAtLeastMaybeSuperTypeOfVarType($scope, $type, $varTagType); - if ($isAtLeastMaybeSuperTypeOf->no()) { + if (!$isAtLeastMaybeSuperTypeOf->yes()) { return $isAtLeastMaybeSuperTypeOf; }

Check warning on line 194 in src/Rules/PhpDoc/VarTagTypeRuleHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ($type->isIterable()->yes() && $varTagType->isIterable()->yes()) { $isAtLeastMaybeSuperTypeOf = $this->isAtLeastMaybeSuperTypeOfVarType($scope, $type, $varTagType); - if ($isAtLeastMaybeSuperTypeOf->no()) { + if (!$isAtLeastMaybeSuperTypeOf->yes()) { return $isAtLeastMaybeSuperTypeOf; }
return $isAtLeastMaybeSuperTypeOf;
}

$innerType = $type->getIterableValueType();
$innerVarTagType = $varTagType->getIterableValueType();

if ($type->equals($innerType) || $varTagType->equals($innerVarTagType)) {
return !$this->isSuperTypeOfVarType($scope, $innerType, $innerVarTagType);
return $this->isSuperTypeOfVarType($scope, $innerType, $innerVarTagType);
}

return $this->checkType($scope, $innerType, $innerVarTagType, $depth + 1);
return $this->isValidSuperType($scope, $innerType, $innerVarTagType, $depth + 1);
}

if ($depth === 0 && $type->isConstantValue()->yes()) {
return !$this->isAtLeastMaybeSuperTypeOfVarType($scope, $type, $varTagType);
return $this->isAtLeastMaybeSuperTypeOfVarType($scope, $type, $varTagType);
}

return !$this->isSuperTypeOfVarType($scope, $type, $varTagType);
return $this->isSuperTypeOfVarType($scope, $type, $varTagType);
}

private function isSuperTypeOfVarType(Scope $scope, Type $type, Type $varTagType): bool
private function isSuperTypeOfVarType(Scope $scope, Type $type, Type $varTagType): IsSuperTypeOfResult
{
if ($type->isSuperTypeOf($varTagType)->yes()) {
return true;
return IsSuperTypeOfResult::createYes();
}

try {
$type = $this->typeNodeResolver->resolve($type->toPhpDocNode(), $this->createNameScope($scope));
} catch (NameScopeAlreadyBeingCreatedException) {
return true;
return IsSuperTypeOfResult::createYes();
}

return $type->isSuperTypeOf($varTagType)->yes();
return $type->isSuperTypeOf($varTagType);
}

private function isAtLeastMaybeSuperTypeOfVarType(Scope $scope, Type $type, Type $varTagType): bool
private function isAtLeastMaybeSuperTypeOfVarType(Scope $scope, Type $type, Type $varTagType): IsSuperTypeOfResult
{
if (!$type->isSuperTypeOf($varTagType)->no()) {
return true;
return IsSuperTypeOfResult::createYes();
}

try {
$type = $this->typeNodeResolver->resolve($type->toPhpDocNode(), $this->createNameScope($scope));
} catch (NameScopeAlreadyBeingCreatedException) {
return true;
return IsSuperTypeOfResult::createYes();
}

$isSuperTypeOf = $type->isSuperTypeOf($varTagType);

Check warning on line 242 in src/Rules/PhpDoc/VarTagTypeRuleHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ return IsSuperTypeOfResult::createYes(); } - $isSuperTypeOf = $type->isSuperTypeOf($varTagType); + $isSuperTypeOf = $varTagType->isSuperTypeOf($type); if (!$isSuperTypeOf->no()) { return IsSuperTypeOfResult::createYes(); }

Check warning on line 242 in src/Rules/PhpDoc/VarTagTypeRuleHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ return IsSuperTypeOfResult::createYes(); } - $isSuperTypeOf = $type->isSuperTypeOf($varTagType); + $isSuperTypeOf = $varTagType->isSuperTypeOf($type); if (!$isSuperTypeOf->no()) { return IsSuperTypeOfResult::createYes(); }
if (!$isSuperTypeOf->no()) {

Check warning on line 243 in src/Rules/PhpDoc/VarTagTypeRuleHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $isSuperTypeOf = $type->isSuperTypeOf($varTagType); - if (!$isSuperTypeOf->no()) { + if ($isSuperTypeOf->yes()) { return IsSuperTypeOfResult::createYes(); }

Check warning on line 243 in src/Rules/PhpDoc/VarTagTypeRuleHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $isSuperTypeOf = $type->isSuperTypeOf($varTagType); - if (!$isSuperTypeOf->no()) { + if ($isSuperTypeOf->yes()) { return IsSuperTypeOfResult::createYes(); }
return IsSuperTypeOfResult::createYes();
}

return !$type->isSuperTypeOf($varTagType)->no();
return $isSuperTypeOf;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,10 +594,12 @@ public function testGenericSubtype(): void
[
'PHPDoc tag @var with type GenericSubtype\IRepository<E of GenericSubtype\IEntity> is not subtype of type GenericSubtype\IRepository<GenericSubtype\IEntity>.',
78,
'Template type E on class GenericSubtype\IRepository is not covariant. Learn more: <fg=cyan>https://phpstan.org/blog/whats-up-with-template-covariant</>',
],
[
'PHPDoc tag @var with type GenericSubtype\IRepository<GenericSubtype\Foo> is not subtype of type GenericSubtype\IRepository<GenericSubtype\IEntity>.',
131,
'Template type E on class GenericSubtype\IRepository is not covariant. Learn more: <fg=cyan>https://phpstan.org/blog/whats-up-with-template-covariant</>',
],
]);
}
Expand Down
Loading