Skip to content

Commit

Permalink
[DX] make use of TypeCombinator to detect null type (#2637)
Browse files Browse the repository at this point in the history
* make use of TypeCombinator to detect null type

* separate resolver property type and var doc type

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jul 6, 2022
1 parent 649e511 commit 53bddfb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
30 changes: 14 additions & 16 deletions rules/Php74/Rector/Property/TypedPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use PHPStan\Type\TypeCombinator;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersionFeature;
Expand Down Expand Up @@ -126,34 +125,37 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
return null;
}

$varType = $this->varDocPropertyTypeInferer->inferProperty($node);
if ($varType instanceof MixedType) {
$resolvedPropertyType = $this->varDocPropertyTypeInferer->inferProperty($node);
if ($resolvedPropertyType instanceof MixedType) {
return null;
}

if ($this->objectTypeAnalyzer->isSpecial($varType)) {
if ($this->objectTypeAnalyzer->isSpecial($resolvedPropertyType)) {
return null;
}

$propertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($varType, TypeKind::PROPERTY);
$propertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
$resolvedPropertyType,
TypeKind::PROPERTY
);

if ($this->isNullOrNonClassLikeTypeOrMixedOrVendorLockedIn($propertyTypeNode, $node)) {
return null;
}

$propertyType = $this->familyRelationsAnalyzer->getPossibleUnionPropertyType(
$node,
$varType,
$resolvedPropertyType,
$scope,
$propertyTypeNode
);

$varType = $propertyType->getVarType();
$varDocType = $propertyType->getVarType();
$propertyTypeNode = $propertyType->getPropertyTypeNode();

$this->varTagRemover->removeVarPhpTagValueNodeIfNotComment($node, $varType);
$this->removeDefaultValueForDoctrineCollection($node, $varType);
$this->addDefaultValueNullForNullableType($node, $varType);
$this->varTagRemover->removeVarPhpTagValueNodeIfNotComment($node, $varDocType);
$this->removeDefaultValueForDoctrineCollection($node, $varDocType);
$this->addDefaultValueNullForNullableType($node, $varDocType);

$node->type = $propertyTypeNode;

Expand Down Expand Up @@ -201,11 +203,7 @@ private function removeDefaultValueForDoctrineCollection(Property $property, Typ

private function addDefaultValueNullForNullableType(Property $property, Type $propertyType): void
{
if (! $propertyType instanceof UnionType) {
return;
}

if (! $propertyType->isSuperTypeOf(new NullType())->yes()) {
if (! TypeCombinator::containsNull($propertyType)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use PHPStan\Type\VoidType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
Expand Down Expand Up @@ -180,14 +181,11 @@ private function isIdentifierWithValues(TypeNode $typeNode, array $values): bool

private function matchNullabledType(Type $returnType): ?Type
{
if (! $returnType instanceof UnionType) {
return null;
}

if (! $returnType->isSuperTypeOf(new NullType())->yes()) {
if (! TypeCombinator::containsNull($returnType)) {
return null;
}

/** @var UnionType $returnType */
if (count($returnType->getTypes()) !== 2) {
return null;
}
Expand Down

0 comments on commit 53bddfb

Please sign in to comment.