Merge VarDocPropertyTypeInferer (#1543)

This commit is contained in:
Tomas Votruba 2021-12-22 00:11:30 +01:00 committed by GitHub
parent 2cdb53d920
commit 6861ae9a4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 37 deletions

View File

@ -29,7 +29,7 @@ use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php74\TypeAnalyzer\ObjectTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer;
use Rector\TypeDeclaration\TypeInferer\VarDocPropertyTypeInferer;
use Rector\VendorLocker\VendorLockResolver;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -46,7 +46,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class TypedPropertyRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly PropertyTypeInferer $propertyTypeInferer,
private readonly VarDocPropertyTypeInferer $varDocPropertyTypeInferer,
private readonly VendorLockResolver $vendorLockResolver,
private readonly DoctrineTypeAnalyzer $doctrineTypeAnalyzer,
private readonly VarTagRemover $varTagRemover,
@ -103,7 +103,7 @@ CODE_SAMPLE
return null;
}
$varType = $this->propertyTypeInferer->inferProperty($node);
$varType = $this->varDocPropertyTypeInferer->inferProperty($node);
if ($varType instanceof MixedType) {
return null;
}

View File

@ -24,7 +24,7 @@ use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\Php80\ValueObject\PropertyPromotionCandidate;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer;
use Rector\TypeDeclaration\TypeInferer\VarDocPropertyTypeInferer;
final class PromotedPropertyCandidateResolver
{
@ -32,7 +32,7 @@ final class PromotedPropertyCandidateResolver
private readonly NodeNameResolver $nodeNameResolver,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly NodeComparator $nodeComparator,
private readonly PropertyTypeInferer $propertyTypeInferer,
private readonly VarDocPropertyTypeInferer $varDocPropertyTypeInferer,
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly TypeComparator $typeComparator,
private readonly TypeFactory $typeFactory
@ -242,7 +242,7 @@ final class PromotedPropertyCandidateResolver
}
// @todo unknown type, not suitable?
$propertyType = $this->propertyTypeInferer->inferProperty($property);
$propertyType = $this->varDocPropertyTypeInferer->inferProperty($property);
return $this->hasConflictingParamType($matchedParam, $propertyType);
}
}

View File

@ -15,18 +15,17 @@ use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer;
use Rector\TypeDeclaration\TypeInferer\VarDocPropertyTypeInferer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @deprecated Split to smaller specific rules.
* @see \Rector\Tests\TypeDeclaration\Rector\Property\PropertyTypeDeclarationRector\PropertyTypeDeclarationRectorTest
*/
final class PropertyTypeDeclarationRector extends AbstractRector
{
public function __construct(
private readonly PropertyTypeInferer $propertyTypeInferer,
private readonly VarDocPropertyTypeInferer $varDocPropertyTypeInferer,
private readonly PhpDocTypeChanger $phpDocTypeChanger
) {
}
@ -92,7 +91,7 @@ CODE_SAMPLE
return null;
}
$type = $this->propertyTypeInferer->inferProperty($node);
$type = $this->varDocPropertyTypeInferer->inferProperty($node);
if ($type instanceof MixedType) {
return null;
}

View File

@ -1,23 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
final class VarDocPropertyTypeInferer
{
public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory
) {
}
public function inferProperty(Property $property): Type
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
return $phpDocInfo->getVarType();
}
}

View File

@ -11,26 +11,28 @@ use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\VoidType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer;
use Rector\TypeDeclaration\TypeAnalyzer\GenericClassStringTypeNormalizer;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\DefaultValuePropertyTypeInferer;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\VarDocPropertyTypeInferer;
final class PropertyTypeInferer
final class VarDocPropertyTypeInferer
{
public function __construct(
private readonly GenericClassStringTypeNormalizer $genericClassStringTypeNormalizer,
private readonly DefaultValuePropertyTypeInferer $defaultValuePropertyTypeInferer,
private readonly VarDocPropertyTypeInferer $varDocPropertyTypeInferer,
private readonly TypeFactory $typeFactory,
private readonly DoctrineTypeAnalyzer $doctrineTypeAnalyzer,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
) {
}
public function inferProperty(Property $property): Type
{
$resolvedType = $this->varDocPropertyTypeInferer->inferProperty($property);
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
$resolvedType = $phpDocInfo->getVarType();
if ($resolvedType instanceof VoidType) {
return new MixedType();
}