Skip to content

Commit

Permalink
Updated Rector to commit a8ae2d8c2a92536f95a70dc99eaebb01168c4a6f
Browse files Browse the repository at this point in the history
rectorphp/rector-src@a8ae2d8 implemented TypedPropertyFromStrictConstructorReadonlyClassRector (#4552)
  • Loading branch information
TomasVotruba committed Jul 23, 2023
1 parent f8344bb commit 1191015
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 33 deletions.
3 changes: 2 additions & 1 deletion config/set/type-declaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@
use Rector\TypeDeclaration\Rector\FunctionLike\AddReturnTypeDeclarationFromYieldsRector;
use Rector\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorReadonlyClassRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodReturnTypeRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector;
use Rector\TypeDeclaration\Rector\Property\VarAnnotationIncorrectNullableRector;
return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->rules([AddClosureReturnTypeRector::class, AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, ReturnAnnotationIncorrectNullableRector::class, VarAnnotationIncorrectNullableRector::class, ParamAnnotationIncorrectNullableRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, ParamTypeFromStrictTypedPropertyRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromReturnNewRector::class, TypedPropertyFromStrictGetterMethodReturnTypeRector::class, AddMethodCallBasedStrictParamTypeRector::class, ReturnTypeFromStrictBoolReturnExprRector::class, ReturnTypeFromStrictNativeCallRector::class, ReturnTypeFromStrictNewArrayRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, ReturnTypeFromStrictParamRector::class, TypedPropertyFromStrictSetUpRector::class, ParamTypeByParentCallTypeRector::class, AddParamTypeSplFixedArrayRector::class, AddParamTypeBasedOnPHPUnitDataProviderRector::class, AddParamTypeFromPropertyTypeRector::class, AddReturnTypeDeclarationFromYieldsRector::class, ReturnTypeFromReturnDirectArrayRector::class, ReturnTypeFromStrictConstantReturnRector::class, ReturnTypeFromStrictTypedCallRector::class, ReturnNeverTypeRector::class, EmptyOnNullableObjectToInstanceOfRector::class, PropertyTypeFromStrictSetterGetterRector::class, ReturnTypeFromStrictTernaryRector::class, BoolReturnTypeFromStrictScalarReturnsRector::class, NumericReturnTypeFromStrictScalarReturnsRector::class, StrictArrayParamDimFetchRector::class]);
$rectorConfig->rules([AddClosureReturnTypeRector::class, AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, ReturnAnnotationIncorrectNullableRector::class, VarAnnotationIncorrectNullableRector::class, ParamAnnotationIncorrectNullableRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, TypedPropertyFromStrictConstructorReadonlyClassRector::class, ParamTypeFromStrictTypedPropertyRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromReturnNewRector::class, TypedPropertyFromStrictGetterMethodReturnTypeRector::class, AddMethodCallBasedStrictParamTypeRector::class, ReturnTypeFromStrictBoolReturnExprRector::class, ReturnTypeFromStrictNativeCallRector::class, ReturnTypeFromStrictNewArrayRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, ReturnTypeFromStrictParamRector::class, TypedPropertyFromStrictSetUpRector::class, ParamTypeByParentCallTypeRector::class, AddParamTypeSplFixedArrayRector::class, AddParamTypeBasedOnPHPUnitDataProviderRector::class, AddParamTypeFromPropertyTypeRector::class, AddReturnTypeDeclarationFromYieldsRector::class, ReturnTypeFromReturnDirectArrayRector::class, ReturnTypeFromStrictConstantReturnRector::class, ReturnTypeFromStrictTypedCallRector::class, ReturnNeverTypeRector::class, EmptyOnNullableObjectToInstanceOfRector::class, PropertyTypeFromStrictSetterGetterRector::class, ReturnTypeFromStrictTernaryRector::class, BoolReturnTypeFromStrictScalarReturnsRector::class, NumericReturnTypeFromStrictScalarReturnsRector::class, StrictArrayParamDimFetchRector::class]);
};
8 changes: 8 additions & 0 deletions packages/PHPStanStaticTypeMapper/DoctrineTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Rector\PHPStanStaticTypeMapper;

use PHPStan\Type\ArrayType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
Expand Down Expand Up @@ -36,4 +37,11 @@ private function isCollectionObjectType(Type $type) : bool
}
return $type->getClassName() === 'Doctrine\\Common\\Collections\\Collection';
}
public function isInstanceOfCollectionType(Type $type) : bool
{
if (!$type instanceof ObjectType) {
return \false;
}
return $type->isInstanceOf('Doctrine\\Common\\Collections\\Collection')->yes();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?php

declare (strict_types=1);
namespace Rector\TypeDeclaration\Rector\Property;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
use Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Rector\TypeDeclaration\Guard\PropertyTypeOverrideGuard;
use Rector\TypeDeclaration\TypeAnalyzer\PropertyTypeDefaultValueAnalyzer;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\TrustedClassMethodPropertyTypeInferer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorReadonlyClassRector\TypedPropertyFromStrictConstructorReadonlyClassRectorTest
*/
final class TypedPropertyFromStrictConstructorReadonlyClassRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\TrustedClassMethodPropertyTypeInferer
*/
private $trustedClassMethodPropertyTypeInferer;
/**
* @readonly
* @var \Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover
*/
private $varTagRemover;
/**
* @readonly
* @var \Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector
*/
private $constructorAssignDetector;
/**
* @readonly
* @var \Rector\TypeDeclaration\Guard\PropertyTypeOverrideGuard
*/
private $propertyTypeOverrideGuard;
/**
* @readonly
* @var \Rector\Core\Reflection\ReflectionResolver
*/
private $reflectionResolver;
/**
* @readonly
* @var \Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer
*/
private $doctrineTypeAnalyzer;
/**
* @readonly
* @var \Rector\TypeDeclaration\TypeAnalyzer\PropertyTypeDefaultValueAnalyzer
*/
private $propertyTypeDefaultValueAnalyzer;
public function __construct(TrustedClassMethodPropertyTypeInferer $trustedClassMethodPropertyTypeInferer, VarTagRemover $varTagRemover, ConstructorAssignDetector $constructorAssignDetector, PropertyTypeOverrideGuard $propertyTypeOverrideGuard, ReflectionResolver $reflectionResolver, DoctrineTypeAnalyzer $doctrineTypeAnalyzer, PropertyTypeDefaultValueAnalyzer $propertyTypeDefaultValueAnalyzer)
{
$this->trustedClassMethodPropertyTypeInferer = $trustedClassMethodPropertyTypeInferer;
$this->varTagRemover = $varTagRemover;
$this->constructorAssignDetector = $constructorAssignDetector;
$this->propertyTypeOverrideGuard = $propertyTypeOverrideGuard;
$this->reflectionResolver = $reflectionResolver;
$this->doctrineTypeAnalyzer = $doctrineTypeAnalyzer;
$this->propertyTypeDefaultValueAnalyzer = $propertyTypeDefaultValueAnalyzer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Add typed public properties based only on strict constructor types in readonly classes', [new CodeSample(<<<'CODE_SAMPLE'
/**
* @immutable
*/
class SomeObject
{
public $name;
public function __construct(string $name)
{
$this->name = $name;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
/**
* @immutable
*/
class SomeObject
{
public string $name;
public function __construct(string $name)
{
$this->name = $name;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if (!$constructClassMethod instanceof ClassMethod || $node->getProperties() === []) {
return null;
}
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
if (!$classReflection instanceof ClassReflection) {
return null;
}
$hasChanged = \false;
foreach ($node->getProperties() as $property) {
if (!$this->propertyTypeOverrideGuard->isLegal($property, $classReflection)) {
continue;
}
$propertyType = $this->trustedClassMethodPropertyTypeInferer->inferProperty($node, $property, $constructClassMethod);
if ($this->shouldSkipProperty($property, $propertyType, $classReflection, $scope)) {
continue;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
$propertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::PROPERTY);
if (!$propertyTypeNode instanceof Node) {
continue;
}
$propertyProperty = $property->props[0];
$propertyName = $this->nodeNameResolver->getName($property);
if ($this->constructorAssignDetector->isPropertyAssigned($node, $propertyName)) {
$propertyProperty->default = null;
$hasChanged = \true;
}
if ($this->propertyTypeDefaultValueAnalyzer->doesConflictWithDefaultValue($propertyProperty, $propertyType)) {
continue;
}
$property->type = $propertyTypeNode;
$this->varTagRemover->removeVarTagIfUseless($phpDocInfo, $property);
$hasChanged = \true;
}
if ($hasChanged) {
return $node;
}
return null;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::TYPED_PROPERTIES;
}
private function shouldSkipProperty(Property $property, Type $propertyType, ClassReflection $classReflection, Scope $scope) : bool
{
if (!$property->isPublic()) {
return \true;
}
if ($propertyType instanceof MixedType) {
return \true;
}
if ($this->doctrineTypeAnalyzer->isInstanceOfCollectionType($propertyType)) {
return \true;
}
$isReadOnlyByPhpdoc = \false;
$propertyName = $this->nodeNameResolver->getName($property);
if ($classReflection->hasProperty($propertyName)) {
$propertyReflection = $classReflection->getProperty($propertyName, $scope);
if ($propertyReflection instanceof PhpPropertyReflection) {
$isReadOnlyByPhpdoc = $propertyReflection->isReadOnlyByPhpDoc();
}
}
if (!$isReadOnlyByPhpdoc) {
return \true;
}
return \false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
use Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Rector\TypeDeclaration\Guard\PropertyTypeOverrideGuard;
use Rector\TypeDeclaration\TypeAnalyzer\PropertyTypeDefaultValueAnalyzer;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\TrustedClassMethodPropertyTypeInferer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -60,14 +62,26 @@ final class TypedPropertyFromStrictConstructorRector extends AbstractRector impl
* @var \Rector\Core\Reflection\ReflectionResolver
*/
private $reflectionResolver;
public function __construct(TrustedClassMethodPropertyTypeInferer $trustedClassMethodPropertyTypeInferer, VarTagRemover $varTagRemover, PhpDocTypeChanger $phpDocTypeChanger, ConstructorAssignDetector $constructorAssignDetector, PropertyTypeOverrideGuard $propertyTypeOverrideGuard, ReflectionResolver $reflectionResolver)
/**
* @readonly
* @var \Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer
*/
private $doctrineTypeAnalyzer;
/**
* @readonly
* @var \Rector\TypeDeclaration\TypeAnalyzer\PropertyTypeDefaultValueAnalyzer
*/
private $propertyTypeDefaultValueAnalyzer;
public function __construct(TrustedClassMethodPropertyTypeInferer $trustedClassMethodPropertyTypeInferer, VarTagRemover $varTagRemover, PhpDocTypeChanger $phpDocTypeChanger, ConstructorAssignDetector $constructorAssignDetector, PropertyTypeOverrideGuard $propertyTypeOverrideGuard, ReflectionResolver $reflectionResolver, DoctrineTypeAnalyzer $doctrineTypeAnalyzer, PropertyTypeDefaultValueAnalyzer $propertyTypeDefaultValueAnalyzer)
{
$this->trustedClassMethodPropertyTypeInferer = $trustedClassMethodPropertyTypeInferer;
$this->varTagRemover = $varTagRemover;
$this->phpDocTypeChanger = $phpDocTypeChanger;
$this->constructorAssignDetector = $constructorAssignDetector;
$this->propertyTypeOverrideGuard = $propertyTypeOverrideGuard;
$this->reflectionResolver = $reflectionResolver;
$this->doctrineTypeAnalyzer = $doctrineTypeAnalyzer;
$this->propertyTypeDefaultValueAnalyzer = $propertyTypeDefaultValueAnalyzer;
}
public function getRuleDefinition() : RuleDefinition
{
Expand Down Expand Up @@ -141,7 +155,7 @@ public function refactor(Node $node) : ?Node
$propertyProperty->default = null;
$hasChanged = \true;
}
if ($this->doesConflictWithDefaultValue($propertyProperty, $propertyType)) {
if ($this->propertyTypeDefaultValueAnalyzer->doesConflictWithDefaultValue($propertyProperty, $propertyType)) {
continue;
}
$property->type = $propertyTypeNode;
Expand All @@ -157,28 +171,11 @@ public function provideMinPhpVersion() : int
{
return PhpVersionFeature::TYPED_PROPERTIES;
}
private function doesConflictWithDefaultValue(PropertyProperty $propertyProperty, Type $propertyType) : bool
{
if (!$propertyProperty->default instanceof Expr) {
return \false;
}
// the defaults can be in conflict
$defaultType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($propertyProperty->default);
// type is not matching, skip it
return !$defaultType->isSuperTypeOf($propertyType)->yes();
}
private function isDoctrineCollectionType(Type $type) : bool
{
if (!$type instanceof ObjectType) {
return \false;
}
return $type->isInstanceOf('Doctrine\\Common\\Collections\\Collection')->yes();
}
private function shouldSkipPropertyType(Type $propertyType) : bool
{
if ($propertyType instanceof MixedType) {
return \true;
}
return $this->isDoctrineCollectionType($propertyType);
return $this->doctrineTypeAnalyzer->isInstanceOfCollectionType($propertyType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare (strict_types=1);
namespace Rector\TypeDeclaration\TypeAnalyzer;

use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\PropertyProperty;
use PHPStan\Type\Type;
use Rector\StaticTypeMapper\StaticTypeMapper;
final class PropertyTypeDefaultValueAnalyzer
{
/**
* @readonly
* @var \Rector\StaticTypeMapper\StaticTypeMapper
*/
private $staticTypeMapper;
public function __construct(StaticTypeMapper $staticTypeMapper)
{
$this->staticTypeMapper = $staticTypeMapper;
}
public function doesConflictWithDefaultValue(PropertyProperty $propertyProperty, Type $propertyType) : bool
{
if (!$propertyProperty->default instanceof Expr) {
return \false;
}
// the defaults can be in conflict
$defaultType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($propertyProperty->default);
// type is not matching, skip it
return !$defaultType->isSuperTypeOf($propertyType)->yes();
}
}
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'c12703def127562155efcee2c348ca3a4d08c170';
public const PACKAGE_VERSION = 'a8ae2d8c2a92536f95a70dc99eaebb01168c4a6f';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-07-23 18:19:26';
public const RELEASE_DATE = '2023-07-23 17:08:46';
/**
* @var int
*/
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitc57a4a84ff14fb5da81d649a75cccd6c::getLoader();
return ComposerAutoloaderInit2a6c2d46859027bd63fed619863b9268::getLoader();

0 comments on commit 1191015

Please sign in to comment.