Updated Rector to commit 118fbde3b1ccc3685c55cd2704bc971d2d49ef41

118fbde3b1 [TypeDeclaration] Remove ParamTypeFromStrictTypedPropertyRector as seems similar feature with AddParamTypeFromPropertyTypeRector (#5293)
This commit is contained in:
Tomas Votruba 2023-11-28 10:10:36 +00:00
parent 198f3d0446
commit 7a2348073c
6 changed files with 5 additions and 221 deletions

View File

@ -34,10 +34,9 @@ use Rector\TypeDeclaration\Rector\ClassMethod\StrictStringParamConcatRector;
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
use Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector;
use Rector\TypeDeclaration\Rector\FunctionLike\AddReturnTypeDeclarationFromYieldsRector;
use Rector\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector;
return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->rules([AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, ParamTypeFromStrictTypedPropertyRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromStrictFluentReturnRector::class, ReturnTypeFromReturnNewRector::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, ReturnUnionTypeRector::class, StrictStringParamConcatRector::class]);
$rectorConfig->rules([AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromStrictFluentReturnRector::class, ReturnTypeFromReturnNewRector::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, ReturnUnionTypeRector::class, StrictStringParamConcatRector::class]);
};

View File

@ -1,4 +1,4 @@
# 353 Rules Overview
# 352 Rules Overview
<br>
@ -56,7 +56,7 @@
- [Transform](#transform) (22)
- [TypeDeclaration](#typedeclaration) (41)
- [TypeDeclaration](#typedeclaration) (40)
- [Visibility](#visibility) (3)
@ -6602,27 +6602,6 @@ Change param type based on parent param type
<br>
### ParamTypeFromStrictTypedPropertyRector
Add param type from `$param` set to typed property
- class: [`Rector\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector`](../rules/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector.php)
```diff
final class SomeClass
{
private int $age;
- public function setAge($age)
+ public function setAge(int $age)
{
$this->age = $age;
}
}
```
<br>
### PropertyTypeFromStrictSetterGetterRector
Add property type based on strict setter and getter method

View File

@ -1,192 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\Rector\Param;
use PhpParser\Node;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\ParamTypeFromStrictTypedPropertyRectorTest
*/
final class ParamTypeFromStrictTypedPropertyRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\Core\Reflection\ReflectionResolver
*/
private $reflectionResolver;
/**
* @readonly
* @var \Rector\VendorLocker\ParentClassMethodTypeOverrideGuard
*/
private $parentClassMethodTypeOverrideGuard;
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
/**
* @readonly
* @var \Rector\StaticTypeMapper\StaticTypeMapper
*/
private $staticTypeMapper;
public function __construct(ReflectionResolver $reflectionResolver, ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard, BetterNodeFinder $betterNodeFinder, StaticTypeMapper $staticTypeMapper)
{
$this->reflectionResolver = $reflectionResolver;
$this->parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard;
$this->betterNodeFinder = $betterNodeFinder;
$this->staticTypeMapper = $staticTypeMapper;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Add param type from $param set to typed property', [new CodeSample(<<<'CODE_SAMPLE'
final class SomeClass
{
private int $age;
public function setAge($age)
{
$this->age = $age;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeClass
{
private int $age;
public function setAge(int $age)
{
$this->age = $age;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
$hasChanged = \false;
foreach ($node->getParams() as $param) {
$changedParam = $this->decorateParamWithType($node, $param, $scope);
if ($changedParam instanceof Param) {
$hasChanged = \true;
}
}
if ($hasChanged) {
return $node;
}
return null;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::TYPED_PROPERTIES;
}
private function decorateParamWithType(ClassMethod $classMethod, Param $param, Scope $scope) : ?Param
{
if ($param->type !== null) {
return null;
}
if ($classMethod->isAbstract()) {
return null;
}
if ($this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod)) {
return null;
}
$originalParamType = $this->resolveParamOriginalType($param, $scope);
$paramName = $this->getName($param);
/** @var Assign[] $assigns */
$assigns = $this->betterNodeFinder->findInstanceOf((array) $classMethod->getStmts(), Assign::class);
foreach ($assigns as $assign) {
if (!$assign->var instanceof PropertyFetch) {
continue;
}
if (!$this->nodeComparator->areNodesEqual($assign->expr, $param->var)) {
continue;
}
if ($this->hasTypeChangedBeforeAssign($assign, $paramName, $originalParamType)) {
return null;
}
$singlePropertyTypeNode = $this->matchPropertySingleTypeNode($assign->var);
if (!$singlePropertyTypeNode instanceof Node) {
return null;
}
$param->type = $singlePropertyTypeNode;
return $param;
}
return null;
}
/**
* @return Identifier|Name|ComplexType|null
*/
private function matchPropertySingleTypeNode(PropertyFetch $propertyFetch) : ?Node
{
$phpPropertyReflection = $this->reflectionResolver->resolvePropertyReflectionFromPropertyFetch($propertyFetch);
if (!$phpPropertyReflection instanceof PhpPropertyReflection) {
return null;
}
$propertyType = $phpPropertyReflection->getNativeType();
if ($propertyType instanceof MixedType) {
return null;
}
if ($propertyType instanceof UnionType) {
return null;
}
if ($propertyType instanceof NullableType) {
return null;
}
return $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::PROPERTY);
}
private function hasTypeChangedBeforeAssign(Assign $assign, string $paramName, Type $originalType) : bool
{
$scope = $assign->getAttribute(AttributeKey::SCOPE);
if (!$scope instanceof Scope) {
return \false;
}
if (!$scope->hasVariableType($paramName)->yes()) {
return \false;
}
$currentParamType = $scope->getVariableType($paramName);
return !$currentParamType->equals($originalType);
}
private function resolveParamOriginalType(Param $param, Scope $scope) : Type
{
$paramName = $this->getName($param);
if (!$scope->hasVariableType($paramName)->yes()) {
return new MixedType();
}
return $scope->getVariableType($paramName);
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '4a2b4a63878dabbb02eeb9fc73587e9b57ad0bdf';
public const PACKAGE_VERSION = '118fbde3b1ccc3685c55cd2704bc971d2d49ef41';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-11-28 11:41:03';
public const RELEASE_DATE = '2023-11-28 11:08:22';
/**
* @var int
*/

View File

@ -2323,7 +2323,6 @@ return array(
'Rector\\TypeDeclaration\\Rector\\Empty_\\EmptyOnNullableObjectToInstanceOfRector' => $baseDir . '/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php',
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddParamTypeSplFixedArrayRector' => $baseDir . '/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeSplFixedArrayRector.php',
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddReturnTypeDeclarationFromYieldsRector' => $baseDir . '/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php',
'Rector\\TypeDeclaration\\Rector\\Param\\ParamTypeFromStrictTypedPropertyRector' => $baseDir . '/rules/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector.php',
'Rector\\TypeDeclaration\\Rector\\Property\\AddPropertyTypeDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php',
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromAssignsRector' => $baseDir . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php',
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromStrictConstructorRector' => $baseDir . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector.php',

View File

@ -2541,7 +2541,6 @@ class ComposerStaticInita55c41c7fa52abd86138c6f32df1d185
'Rector\\TypeDeclaration\\Rector\\Empty_\\EmptyOnNullableObjectToInstanceOfRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php',
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddParamTypeSplFixedArrayRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeSplFixedArrayRector.php',
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddReturnTypeDeclarationFromYieldsRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php',
'Rector\\TypeDeclaration\\Rector\\Param\\ParamTypeFromStrictTypedPropertyRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector.php',
'Rector\\TypeDeclaration\\Rector\\Property\\AddPropertyTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php',
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromAssignsRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php',
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromStrictConstructorRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector.php',