diff --git a/build/target-repository/docs/rector_rules_overview.md b/build/target-repository/docs/rector_rules_overview.md index e940876e7f7..926261dd876 100644 --- a/build/target-repository/docs/rector_rules_overview.md +++ b/build/target-repository/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 353 Rules Overview +# 352 Rules Overview
@@ -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
-### 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; - } - } -``` - -
- ### PropertyTypeFromStrictSetterGetterRector Add property type based on strict setter and getter method diff --git a/config/set/type-declaration.php b/config/set/type-declaration.php index 58fede5a6f2..0f256f3c0ef 100644 --- a/config/set/type-declaration.php +++ b/config/set/type-declaration.php @@ -33,7 +33,6 @@ 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; @@ -46,7 +45,6 @@ AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, - ParamTypeFromStrictTypedPropertyRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromStrictFluentReturnRector::class, ReturnTypeFromReturnNewRector::class, diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/changed_after_assign_type.php.inc b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/changed_after_assign_type.php.inc deleted file mode 100644 index 4aac1df5f29..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/changed_after_assign_type.php.inc +++ /dev/null @@ -1,39 +0,0 @@ -kind = $kind; - - if (!is_array($kind)) { - $kind = [$kind]; - } - } -} - -?> ------ -kind = $kind; - - if (!is_array($kind)) { - $kind = [$kind]; - } - } -} - -?> diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/even_constructor.php.inc b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/even_constructor.php.inc deleted file mode 100644 index 53615c122e8..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/even_constructor.php.inc +++ /dev/null @@ -1,31 +0,0 @@ -age = $age; - } -} - -?> ------ -age = $age; - } -} - -?> diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/external_type.php.inc b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/external_type.php.inc deleted file mode 100644 index 89e16428d40..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/external_type.php.inc +++ /dev/null @@ -1,31 +0,0 @@ -age = $age; - } -} - -?> ------ -age = $age; - } -} - -?> diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/multiple_params.php.inc b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/multiple_params.php.inc deleted file mode 100644 index 756a52b1747..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/multiple_params.php.inc +++ /dev/null @@ -1,37 +0,0 @@ -age = $age; - $this->nick = $name; - } -} - -?> ------ -age = $age; - $this->nick = $name; - } -} - -?> diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_already_type.php.inc b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_already_type.php.inc deleted file mode 100644 index a5bb0b29759..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_already_type.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -age = $age; - } -} diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_changed_type.php.inc b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_changed_type.php.inc deleted file mode 100644 index 469c44c18b6..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_changed_type.php.inc +++ /dev/null @@ -1,17 +0,0 @@ -kind = $kind; - } -} diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_doc_type.php.inc b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_doc_type.php.inc deleted file mode 100644 index 3a57a0e17fe..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_doc_type.php.inc +++ /dev/null @@ -1,16 +0,0 @@ -age = $age; - } -} diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_nullable_type.php.inc b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_nullable_type.php.inc deleted file mode 100644 index 34810e689db..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_nullable_type.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -age = $age; - } -} diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_parent_class_with_args.php.inc b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_parent_class_with_args.php.inc deleted file mode 100644 index daa822970b7..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_parent_class_with_args.php.inc +++ /dev/null @@ -1,15 +0,0 @@ -items = $args; - } -} diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_union_type.php.inc b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_union_type.php.inc deleted file mode 100644 index cab47d8db8d..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_union_type.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -age = $age; - } -} diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_unknown_parent_class.php.inc b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_unknown_parent_class.php.inc deleted file mode 100644 index 057c7471f46..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/skip_unknown_parent_class.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -items = $args; - } -} diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/some_class.php.inc b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/some_class.php.inc deleted file mode 100644 index 4f2c73d7fc5..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/some_class.php.inc +++ /dev/null @@ -1,31 +0,0 @@ -age = $age; - } -} - -?> ------ -age = $age; - } -} - -?> diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/vendor_external_type.php.inc b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/vendor_external_type.php.inc deleted file mode 100644 index aea6e92da2d..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Fixture/vendor_external_type.php.inc +++ /dev/null @@ -1,31 +0,0 @@ -name = $age; - } -} - -?> ------ -name = $age; - } -} - -?> diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/ParamTypeFromStrictTypedPropertyRectorTest.php b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/ParamTypeFromStrictTypedPropertyRectorTest.php deleted file mode 100644 index 1d40449d38c..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/ParamTypeFromStrictTypedPropertyRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Source/ExternalClass.php b/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Source/ExternalClass.php deleted file mode 100644 index 525203cc0d9..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector/Source/ExternalClass.php +++ /dev/null @@ -1,10 +0,0 @@ -phpVersion(PhpVersionFeature::DEPRECATE_DYNAMIC_PROPERTIES); - - $rectorConfig->rule(ParamTypeFromStrictTypedPropertyRector::class); -}; diff --git a/rules/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector.php b/rules/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector.php deleted file mode 100644 index 9ab5132766b..00000000000 --- a/rules/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector.php +++ /dev/null @@ -1,207 +0,0 @@ -age = $age; - } -} -CODE_SAMPLE - - , - <<<'CODE_SAMPLE' -final class SomeClass -{ - private int $age; - - public function setAge(int $age) - { - $this->age = $age; - } -} -CODE_SAMPLE - ), - ]); - } - - /** - * @return array> - */ - 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); - } -}