From 05788745fd8f26f183ef103dbca2b3c976b48b0a Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 1 Mar 2021 13:10:50 +0100 Subject: [PATCH] [Downgrade] Remove excessive abstract class inheritance, use single service (#5717) --- composer.json | 32 +- config/set/downgrade-php70.php | 6 +- config/set/downgrade-php71.php | 10 +- config/set/downgrade-php72.php | 6 +- config/set/downgrade-php74.php | 4 +- config/set/downgrade-php80.php | 16 +- docs/rector_rules_overview.md | 322 ++++++------------ ...wngradeParamDeclarationRectorInterface.php | 2 +- ...bstractDowngradeParamDeclarationRector.php | 92 ----- .../DowngradeTypeDeclarationRector.php | 97 ++++++ .../DowngradeTypeParamDeclarationRector.php | 67 ---- .../DowngradeTypeReturnDeclarationRector.php | 58 ---- .../DowngradeTypeDeclarationRectorTest.php} | 4 +- .../Fixture/fixture.php.inc | 4 +- .../Fixture/return_array.php.inc} | 8 +- .../Fixture/return_string.php.inc} | 8 +- .../Fixture/skip_array.php.inc | 2 +- .../Fixture/skip_callable.php.inc | 2 +- .../config/php_70.php | 6 +- ...wngradeTypeReturnDeclarationRectorTest.php | 31 -- .../config/php_70.php | 16 - rules/downgrade-php71/config/config.php | 17 + ...adeIterablePseudoTypeDeclarationRector.php | 90 +++++ ...erablePseudoTypeParamDeclarationRector.php | 62 ---- ...rablePseudoTypeReturnDeclarationRector.php | 65 ---- ...DowngradeNullableTypeDeclarationRector.php | 140 ++++++++ ...radeNullableTypeParamDeclarationRector.php | 65 ---- ...adeNullableTypeReturnDeclarationRector.php | 66 ---- .../DowngradeVoidTypeDeclarationRector.php | 79 +++++ ...wngradeVoidTypeReturnDeclarationRector.php | 52 --- .../PhpDocFromTypeDeclarationDecorator.php | 173 ++++++++++ ...erablePseudoTypeDeclarationRectorTest.php} | 8 +- .../Fixture/param_iterable.php.inc} | 10 +- .../Fixture/return_iterable.php.inc} | 10 +- ...ePseudoTypeReturnDeclarationRectorTest.php | 31 -- ...radeNullableTypeDeclarationRectorTest.php} | 8 +- .../Fixture/param_type.php.inc} | 8 +- .../Fixture/return_type.php.inc} | 8 +- ...ullableTypeReturnDeclarationRectorTest.php | 31 -- ...owngradeVoidTypeDeclarationRectorTest.php} | 8 +- .../Fixture/docblock_exists.php.inc | 4 +- .../Fixture/docblock_tag_exists.php.inc | 4 +- .../Fixture/fixture.php.inc | 4 +- ...actDowngradeParamTypeDeclarationRector.php | 45 --- ...ctDowngradeReturnTypeDeclarationRector.php | 40 --- .../DowngradeObjectTypeDeclarationRector.php | 88 +++++ ...ngradeParamObjectTypeDeclarationRector.php | 51 --- ...gradeReturnObjectTypeDeclarationRector.php | 53 --- ...ngradeObjectTypeDeclarationRectorTest.php} | 8 +- .../Fixture/param_docblock_exists.php.inc} | 8 +- .../param_docblock_tag_exists.php.inc} | 8 +- .../Fixture/param_fixture.php.inc} | 8 +- .../param_multiple_matching_params.php.inc} | 8 +- .../Fixture/param_multiple_params.php.inc} | 8 +- .../Fixture/param_nullable_type.php.inc} | 8 +- .../Fixture/return_docblock_exists.php.inc} | 8 +- .../return_docblock_tag_exists.php.inc} | 8 +- .../Fixture/return_fixture.php.inc} | 8 +- .../Fixture/return_nullable_type.php.inc} | 8 +- .../Source/AnotherClass.php | 2 +- ...eReturnObjectTypeDeclarationRectorTest.php | 32 -- .../Source/AnotherClass.php | 10 - ...wngradeContravariantArgumentTypeRector.php | 83 ++++- ...=> DowngradeSelfTypeDeclarationRector.php} | 40 ++- ...owngradeSelfTypeDeclarationRectorTest.php} | 8 +- .../Fixture/docblock_exists.php.inc | 4 +- .../Fixture/fixture.php.inc | 4 +- .../Fixture/interface.php.inc | 4 +- .../Fixture/nullable_type.php.inc | 4 +- .../Fixture/trait.php.inc | 4 +- .../DowngradeMixedTypeDeclarationRector.php | 84 +++++ ...wngradeParamMixedTypeDeclarationRector.php | 52 --- ...ngradeReturnMixedTypeDeclarationRector.php | 60 ---- ...gradeReturnStaticTypeDeclarationRector.php | 54 --- .../DowngradeStaticTypeDeclarationRector.php | 85 +++++ .../DowngradeUnionTypeDeclarationRector.php | 88 +++++ ...wngradeUnionTypeParamDeclarationRector.php | 67 ---- ...ngradeUnionTypeReturnDeclarationRector.php | 71 ---- ...wngradeMixedTypeDeclarationRectorTest.php} | 8 +- .../Fixture/docblock_exists.php.inc | 4 +- .../Fixture/docblock_tag_exists.php.inc | 4 +- .../Fixture/fixture.php.inc | 4 +- .../Fixture/multiple_matching_params.php.inc | 4 +- .../Fixture/multiple_params.php.inc | 4 +- .../Fixture/return_docblock_exists.php.inc} | 8 +- .../return_docblock_tag_exists.php.inc} | 8 +- .../Fixture/return_fixture.php.inc} | 8 +- .../Source/AnotherClass.php | 2 +- ...eReturnStaticTypeDeclarationRectorTest.php | 32 -- .../Source/AnotherClass.php | 10 - ...ngradeStaticTypeDeclarationRectorTest.php} | 8 +- .../Fixture/docblock_exists.php.inc | 4 +- .../Fixture/docblock_tag_exists.php.inc | 4 +- .../Fixture/fixture.php.inc | 4 +- .../Source/AnotherClass.php | 2 +- ...wngradeUnionTypeDeclarationRectorTest.php} | 8 +- .../Fixture/param_union.php.inc} | 8 +- .../Fixture/return_union.php.inc} | 8 +- ...deUnionTypeReturnDeclarationRectorTest.php | 32 -- .../config/rector-rules.neon | 18 +- 100 files changed, 1354 insertions(+), 1695 deletions(-) delete mode 100644 rules/downgrade-php70/src/Rector/FunctionLike/AbstractDowngradeParamDeclarationRector.php create mode 100644 rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeDeclarationRector.php delete mode 100644 rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeParamDeclarationRector.php delete mode 100644 rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeReturnDeclarationRector.php rename rules/downgrade-php70/tests/Rector/FunctionLike/{DowngradeTypeParamDeclarationRector/DowngradeTypeParamDeclarationRectorTest.php => DowngradeTypeDeclarationRector/DowngradeTypeDeclarationRectorTest.php} (84%) rename rules/downgrade-php70/tests/Rector/FunctionLike/{DowngradeTypeParamDeclarationRector => DowngradeTypeDeclarationRector}/Fixture/fixture.php.inc (81%) rename rules/downgrade-php70/tests/Rector/FunctionLike/{DowngradeTypeReturnDeclarationRector/Fixture/array.php.inc => DowngradeTypeDeclarationRector/Fixture/return_array.php.inc} (69%) rename rules/downgrade-php70/tests/Rector/FunctionLike/{DowngradeTypeReturnDeclarationRector/Fixture/fixture.php.inc => DowngradeTypeDeclarationRector/Fixture/return_string.php.inc} (74%) rename rules/downgrade-php70/tests/Rector/FunctionLike/{DowngradeTypeParamDeclarationRector => DowngradeTypeDeclarationRector}/Fixture/skip_array.php.inc (78%) rename rules/downgrade-php70/tests/Rector/FunctionLike/{DowngradeTypeParamDeclarationRector => DowngradeTypeDeclarationRector}/Fixture/skip_callable.php.inc (79%) rename rules/downgrade-php70/tests/Rector/FunctionLike/{DowngradeTypeParamDeclarationRector => DowngradeTypeDeclarationRector}/config/php_70.php (72%) delete mode 100644 rules/downgrade-php70/tests/Rector/FunctionLike/DowngradeTypeReturnDeclarationRector/DowngradeTypeReturnDeclarationRectorTest.php delete mode 100644 rules/downgrade-php70/tests/Rector/FunctionLike/DowngradeTypeReturnDeclarationRector/config/php_70.php create mode 100644 rules/downgrade-php71/config/config.php create mode 100644 rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeDeclarationRector.php delete mode 100644 rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeParamDeclarationRector.php delete mode 100644 rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeReturnDeclarationRector.php create mode 100644 rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeDeclarationRector.php delete mode 100644 rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeParamDeclarationRector.php delete mode 100644 rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeReturnDeclarationRector.php create mode 100644 rules/downgrade-php71/src/Rector/FunctionLike/DowngradeVoidTypeDeclarationRector.php delete mode 100644 rules/downgrade-php71/src/Rector/FunctionLike/DowngradeVoidTypeReturnDeclarationRector.php create mode 100644 rules/downgrade-php71/src/TypeDeclaration/PhpDocFromTypeDeclarationDecorator.php rename rules/downgrade-php71/tests/Rector/FunctionLike/{DowngradeIterablePseudoTypeParamDeclarationRector/DowngradeIterablePseudoTypeParamDeclarationRectorTest.php => DowngradeIterablePseudoTypeDeclarationRector/DowngradeIterablePseudoTypeDeclarationRectorTest.php} (71%) rename rules/downgrade-php71/tests/Rector/FunctionLike/{DowngradeIterablePseudoTypeParamDeclarationRector/Fixture/fixture.php.inc => DowngradeIterablePseudoTypeDeclarationRector/Fixture/param_iterable.php.inc} (64%) rename rules/downgrade-php71/tests/Rector/FunctionLike/{DowngradeIterablePseudoTypeReturnDeclarationRector/Fixture/fixture.php.inc => DowngradeIterablePseudoTypeDeclarationRector/Fixture/return_iterable.php.inc} (64%) delete mode 100644 rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeIterablePseudoTypeReturnDeclarationRector/DowngradeIterablePseudoTypeReturnDeclarationRectorTest.php rename rules/downgrade-php71/tests/Rector/FunctionLike/{DowngradeNullableTypeParamDeclarationRector/DowngradeNullableTypeParamDeclarationRectorTest.php => DowngradeNullableTypeDeclarationRector/DowngradeNullableTypeDeclarationRectorTest.php} (73%) rename rules/downgrade-php71/tests/Rector/FunctionLike/{DowngradeNullableTypeParamDeclarationRector/Fixture/fixture.php.inc => DowngradeNullableTypeDeclarationRector/Fixture/param_type.php.inc} (70%) rename rules/downgrade-php71/tests/Rector/FunctionLike/{DowngradeNullableTypeReturnDeclarationRector/Fixture/fixture.php.inc => DowngradeNullableTypeDeclarationRector/Fixture/return_type.php.inc} (78%) delete mode 100644 rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeReturnDeclarationRector/DowngradeNullableTypeReturnDeclarationRectorTest.php rename rules/downgrade-php71/tests/Rector/FunctionLike/{DowngradeVoidTypeReturnDeclarationRector/DowngradeVoidTypeReturnDeclarationRectorTest.php => DowngradeVoidTypeDeclarationRector/DowngradeVoidTypeDeclarationRectorTest.php} (66%) rename rules/downgrade-php71/tests/Rector/FunctionLike/{DowngradeVoidTypeReturnDeclarationRector => DowngradeVoidTypeDeclarationRector}/Fixture/docblock_exists.php.inc (84%) rename rules/downgrade-php71/tests/Rector/FunctionLike/{DowngradeVoidTypeReturnDeclarationRector => DowngradeVoidTypeDeclarationRector}/Fixture/docblock_tag_exists.php.inc (84%) rename rules/downgrade-php71/tests/Rector/FunctionLike/{DowngradeVoidTypeReturnDeclarationRector => DowngradeVoidTypeDeclarationRector}/Fixture/fixture.php.inc (80%) delete mode 100644 rules/downgrade-php72/src/Rector/FunctionLike/AbstractDowngradeParamTypeDeclarationRector.php delete mode 100644 rules/downgrade-php72/src/Rector/FunctionLike/AbstractDowngradeReturnTypeDeclarationRector.php create mode 100644 rules/downgrade-php72/src/Rector/FunctionLike/DowngradeObjectTypeDeclarationRector.php delete mode 100644 rules/downgrade-php72/src/Rector/FunctionLike/DowngradeParamObjectTypeDeclarationRector.php delete mode 100644 rules/downgrade-php72/src/Rector/FunctionLike/DowngradeReturnObjectTypeDeclarationRector.php rename rules/downgrade-php72/tests/Rector/FunctionLike/{DowngradeParamObjectTypeDeclarationRector/DowngradeParamObjectTypeDeclarationRectorTest.php => DowngradeObjectTypeDeclarationRector/DowngradeObjectTypeDeclarationRectorTest.php} (67%) rename rules/downgrade-php72/tests/Rector/FunctionLike/{DowngradeParamObjectTypeDeclarationRector/Fixture/docblock_exists.php.inc => DowngradeObjectTypeDeclarationRector/Fixture/param_docblock_exists.php.inc} (75%) rename rules/downgrade-php72/tests/Rector/FunctionLike/{DowngradeParamObjectTypeDeclarationRector/Fixture/docblock_tag_exists.php.inc => DowngradeObjectTypeDeclarationRector/Fixture/param_docblock_tag_exists.php.inc} (76%) rename rules/downgrade-php72/tests/Rector/FunctionLike/{DowngradeParamObjectTypeDeclarationRector/Fixture/fixture.php.inc => DowngradeObjectTypeDeclarationRector/Fixture/param_fixture.php.inc} (74%) rename rules/downgrade-php72/tests/Rector/FunctionLike/{DowngradeParamObjectTypeDeclarationRector/Fixture/multiple_matching_params.php.inc => DowngradeObjectTypeDeclarationRector/Fixture/param_multiple_matching_params.php.inc} (78%) rename rules/downgrade-php72/tests/Rector/FunctionLike/{DowngradeParamObjectTypeDeclarationRector/Fixture/multiple_params.php.inc => DowngradeObjectTypeDeclarationRector/Fixture/param_multiple_params.php.inc} (74%) rename rules/downgrade-php72/tests/Rector/FunctionLike/{DowngradeParamObjectTypeDeclarationRector/Fixture/nullable_type.php.inc => DowngradeObjectTypeDeclarationRector/Fixture/param_nullable_type.php.inc} (72%) rename rules/downgrade-php72/tests/Rector/FunctionLike/{DowngradeReturnObjectTypeDeclarationRector/Fixture/docblock_exists.php.inc => DowngradeObjectTypeDeclarationRector/Fixture/return_docblock_exists.php.inc} (76%) rename rules/downgrade-php72/tests/Rector/FunctionLike/{DowngradeReturnObjectTypeDeclarationRector/Fixture/docblock_tag_exists.php.inc => DowngradeObjectTypeDeclarationRector/Fixture/return_docblock_tag_exists.php.inc} (76%) rename rules/downgrade-php72/tests/Rector/FunctionLike/{DowngradeReturnObjectTypeDeclarationRector/Fixture/fixture.php.inc => DowngradeObjectTypeDeclarationRector/Fixture/return_fixture.php.inc} (75%) rename rules/downgrade-php72/tests/Rector/FunctionLike/{DowngradeReturnObjectTypeDeclarationRector/Fixture/nullable_type.php.inc => DowngradeObjectTypeDeclarationRector/Fixture/return_nullable_type.php.inc} (74%) rename rules/downgrade-php72/tests/Rector/FunctionLike/{DowngradeParamObjectTypeDeclarationRector => DowngradeObjectTypeDeclarationRector}/Source/AnotherClass.php (69%) delete mode 100644 rules/downgrade-php72/tests/Rector/FunctionLike/DowngradeReturnObjectTypeDeclarationRector/DowngradeReturnObjectTypeDeclarationRectorTest.php delete mode 100644 rules/downgrade-php72/tests/Rector/FunctionLike/DowngradeReturnObjectTypeDeclarationRector/Source/AnotherClass.php rename rules/downgrade-php74/src/Rector/ClassMethod/{DowngradeReturnSelfTypeDeclarationRector.php => DowngradeSelfTypeDeclarationRector.php} (55%) rename rules/downgrade-php74/tests/Rector/ClassMethod/{DowngradeReturnSelfTypeDeclarationRector/DowngradeReturnSelfTypeDeclarationRectorTest.php => DowngradeSelfTypeDeclarationRector/DowngradeSelfTypeDeclarationRectorTest.php} (68%) rename rules/downgrade-php74/tests/Rector/ClassMethod/{DowngradeReturnSelfTypeDeclarationRector => DowngradeSelfTypeDeclarationRector}/Fixture/docblock_exists.php.inc (84%) rename rules/downgrade-php74/tests/Rector/ClassMethod/{DowngradeReturnSelfTypeDeclarationRector => DowngradeSelfTypeDeclarationRector}/Fixture/fixture.php.inc (80%) rename rules/downgrade-php74/tests/Rector/ClassMethod/{DowngradeReturnSelfTypeDeclarationRector => DowngradeSelfTypeDeclarationRector}/Fixture/interface.php.inc (79%) rename rules/downgrade-php74/tests/Rector/ClassMethod/{DowngradeReturnSelfTypeDeclarationRector => DowngradeSelfTypeDeclarationRector}/Fixture/nullable_type.php.inc (82%) rename rules/downgrade-php74/tests/Rector/ClassMethod/{DowngradeReturnSelfTypeDeclarationRector => DowngradeSelfTypeDeclarationRector}/Fixture/trait.php.inc (81%) create mode 100644 rules/downgrade-php80/src/Rector/FunctionLike/DowngradeMixedTypeDeclarationRector.php delete mode 100644 rules/downgrade-php80/src/Rector/FunctionLike/DowngradeParamMixedTypeDeclarationRector.php delete mode 100644 rules/downgrade-php80/src/Rector/FunctionLike/DowngradeReturnMixedTypeDeclarationRector.php delete mode 100644 rules/downgrade-php80/src/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector.php create mode 100644 rules/downgrade-php80/src/Rector/FunctionLike/DowngradeStaticTypeDeclarationRector.php create mode 100644 rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector.php delete mode 100644 rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeParamDeclarationRector.php delete mode 100644 rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeReturnDeclarationRector.php rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeParamMixedTypeDeclarationRector/DowngradeParamMixedTypeDeclarationRectorTest.php => DowngradeMixedTypeDeclarationRector/DowngradeMixedTypeDeclarationRectorTest.php} (67%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeParamMixedTypeDeclarationRector => DowngradeMixedTypeDeclarationRector}/Fixture/docblock_exists.php.inc (84%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeParamMixedTypeDeclarationRector => DowngradeMixedTypeDeclarationRector}/Fixture/docblock_tag_exists.php.inc (84%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeParamMixedTypeDeclarationRector => DowngradeMixedTypeDeclarationRector}/Fixture/fixture.php.inc (80%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeParamMixedTypeDeclarationRector => DowngradeMixedTypeDeclarationRector}/Fixture/multiple_matching_params.php.inc (85%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeParamMixedTypeDeclarationRector => DowngradeMixedTypeDeclarationRector}/Fixture/multiple_params.php.inc (82%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeReturnMixedTypeDeclarationRector/Fixture/docblock_exists.php.inc => DowngradeMixedTypeDeclarationRector/Fixture/return_docblock_exists.php.inc} (80%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeReturnMixedTypeDeclarationRector/Fixture/docblock_tag_exists.php.inc => DowngradeMixedTypeDeclarationRector/Fixture/return_docblock_tag_exists.php.inc} (80%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeReturnMixedTypeDeclarationRector/Fixture/fixture.php.inc => DowngradeMixedTypeDeclarationRector/Fixture/return_fixture.php.inc} (78%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeParamMixedTypeDeclarationRector => DowngradeMixedTypeDeclarationRector}/Source/AnotherClass.php (69%) delete mode 100644 rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/DowngradeReturnStaticTypeDeclarationRectorTest.php delete mode 100644 rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/Source/AnotherClass.php rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeReturnMixedTypeDeclarationRector/DowngradeReturnMixedTypeDeclarationRectorTest.php => DowngradeStaticTypeDeclarationRector/DowngradeStaticTypeDeclarationRectorTest.php} (67%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeReturnStaticTypeDeclarationRector => DowngradeStaticTypeDeclarationRector}/Fixture/docblock_exists.php.inc (86%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeReturnStaticTypeDeclarationRector => DowngradeStaticTypeDeclarationRector}/Fixture/docblock_tag_exists.php.inc (87%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeReturnStaticTypeDeclarationRector => DowngradeStaticTypeDeclarationRector}/Fixture/fixture.php.inc (84%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeReturnMixedTypeDeclarationRector => DowngradeStaticTypeDeclarationRector}/Source/AnotherClass.php (69%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeUnionTypeParamDeclarationRector/DowngradeUnionTypeParamDeclarationRectorTest.php => DowngradeUnionTypeDeclarationRector/DowngradeUnionTypeDeclarationRectorTest.php} (75%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeUnionTypeParamDeclarationRector/Fixture/fixture.php.inc => DowngradeUnionTypeDeclarationRector/Fixture/param_union.php.inc} (71%) rename rules/downgrade-php80/tests/Rector/FunctionLike/{DowngradeUnionTypeReturnDeclarationRector/Fixture/fixture.php.inc => DowngradeUnionTypeDeclarationRector/Fixture/return_union.php.inc} (78%) delete mode 100644 rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeUnionTypeReturnDeclarationRector/DowngradeUnionTypeReturnDeclarationRectorTest.php diff --git a/composer.json b/composer.json index 2888e6d2993..f783c18eac4 100644 --- a/composer.json +++ b/composer.json @@ -48,16 +48,16 @@ "symfony/finder": "^4.4.8|^5.1", "symfony/http-kernel": "^4.4.8|^5.1", "symfony/process": "^4.4.8|^5.1", - "symplify/astral": "dev-master", - "symplify/autowire-array-parameter": "dev-master", - "symplify/console-color-diff": "dev-master", - "symplify/package-builder": "dev-master", - "symplify/rule-doc-generator": "dev-master", - "symplify/set-config-resolver": "dev-master", - "symplify/simple-php-doc-parser": "dev-master", - "symplify/skipper": "dev-master", - "symplify/smart-file-system": "dev-master", - "symplify/symfony-php-config": "dev-master", + "symplify/astral": "^9.2.3", + "symplify/autowire-array-parameter": "^9.2.3", + "symplify/console-color-diff": "^9.2.3", + "symplify/package-builder": "^9.2.3", + "symplify/rule-doc-generator": "^9.2.3", + "symplify/set-config-resolver": "^9.2.3", + "symplify/simple-php-doc-parser": "^9.2.3", + "symplify/skipper": "^9.2.3", + "symplify/smart-file-system": "^9.2.3", + "symplify/symfony-php-config": "^9.2.3", "webmozart/assert": "^1.9" }, "require-dev": { @@ -71,14 +71,14 @@ "phpunit/phpunit": "^9.5", "symfony/security-core": "^5.2", "symfony/security-http": "^5.2", - "symplify/changelog-linker": "dev-master", - "symplify/coding-standard": "dev-master", + "symplify/changelog-linker": "^9.2.3", + "symplify/coding-standard": "^9.2.3", "symplify/easy-ci": "^9.1.0", - "symplify/easy-coding-standard": "dev-master", - "symplify/easy-testing": "dev-master", + "symplify/easy-coding-standard": "^9.2.3", + "symplify/easy-testing": "^9.2.3", "symplify/monorepo-builder": "^9.2", - "symplify/phpstan-extensions": "dev-master", - "symplify/phpstan-rules": "dev-master", + "symplify/phpstan-extensions": "^9.2.3", + "symplify/phpstan-rules": "^9.2.3", "tracy/tracy": "^2.8" }, "replace": { diff --git a/config/set/downgrade-php70.php b/config/set/downgrade-php70.php index 02a7184ee55..55e40feac22 100644 --- a/config/set/downgrade-php70.php +++ b/config/set/downgrade-php70.php @@ -2,12 +2,10 @@ declare(strict_types=1); -use Rector\DowngradePhp70\Rector\FunctionLike\DowngradeTypeParamDeclarationRector; -use Rector\DowngradePhp70\Rector\FunctionLike\DowngradeTypeReturnDeclarationRector; +use Rector\DowngradePhp70\Rector\FunctionLike\DowngradeTypeDeclarationRector; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $containerConfigurator): void { $services = $containerConfigurator->services(); - $services->set(DowngradeTypeParamDeclarationRector::class); - $services->set(DowngradeTypeReturnDeclarationRector::class); + $services->set(DowngradeTypeDeclarationRector::class); }; diff --git a/config/set/downgrade-php71.php b/config/set/downgrade-php71.php index c3137f8af3c..d20c503f386 100644 --- a/config/set/downgrade-php71.php +++ b/config/set/downgrade-php71.php @@ -6,18 +6,16 @@ use Rector\Core\Configuration\Option; use Rector\Core\ValueObject\PhpVersion; use Rector\DowngradePhp71\Rector\Array_\SymmetricArrayDestructuringToListRector; use Rector\DowngradePhp71\Rector\ClassConst\DowngradeClassConstantVisibilityRector; -use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeParamDeclarationRector; -use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeReturnDeclarationRector; -use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeReturnDeclarationRector; +use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeDeclarationRector; +use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeDeclarationRector; use Rector\DowngradePhp71\Rector\String_\DowngradeNegativeStringOffsetToStrlenRector; use Rector\DowngradePhp71\Rector\TryCatch\DowngradePipeToMultiCatchExceptionRector; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $containerConfigurator): void { $services = $containerConfigurator->services(); - $services->set(DowngradeNullableTypeParamDeclarationRector::class); - $services->set(DowngradeNullableTypeReturnDeclarationRector::class); - $services->set(DowngradeVoidTypeReturnDeclarationRector::class); + $services->set(DowngradeNullableTypeDeclarationRector::class); + $services->set(DowngradeVoidTypeDeclarationRector::class); $parameters = $containerConfigurator->parameters(); $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_70); diff --git a/config/set/downgrade-php72.php b/config/set/downgrade-php72.php index 194aadc5240..de5f60bc280 100644 --- a/config/set/downgrade-php72.php +++ b/config/set/downgrade-php72.php @@ -5,14 +5,12 @@ declare(strict_types=1); use Rector\Core\Configuration\Option; use Rector\Core\ValueObject\PhpVersion; use Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector; -use Rector\DowngradePhp72\Rector\FunctionLike\DowngradeParamObjectTypeDeclarationRector; -use Rector\DowngradePhp72\Rector\FunctionLike\DowngradeReturnObjectTypeDeclarationRector; +use Rector\DowngradePhp72\Rector\FunctionLike\DowngradeObjectTypeDeclarationRector; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $containerConfigurator): void { $services = $containerConfigurator->services(); - $services->set(DowngradeParamObjectTypeDeclarationRector::class); - $services->set(DowngradeReturnObjectTypeDeclarationRector::class); + $services->set(DowngradeObjectTypeDeclarationRector::class); $parameters = $containerConfigurator->parameters(); $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_71); diff --git a/config/set/downgrade-php74.php b/config/set/downgrade-php74.php index e31849dd06c..8d3ea5fdb61 100644 --- a/config/set/downgrade-php74.php +++ b/config/set/downgrade-php74.php @@ -8,7 +8,7 @@ use Rector\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector; use Rector\DowngradePhp74\Rector\ArrowFunction\ArrowFunctionToAnonymousFunctionRector; use Rector\DowngradePhp74\Rector\ClassMethod\DowngradeContravariantArgumentTypeRector; use Rector\DowngradePhp74\Rector\ClassMethod\DowngradeCovariantReturnTypeRector; -use Rector\DowngradePhp74\Rector\ClassMethod\DowngradeReturnSelfTypeDeclarationRector; +use Rector\DowngradePhp74\Rector\ClassMethod\DowngradeSelfTypeDeclarationRector; use Rector\DowngradePhp74\Rector\Coalesce\DowngradeNullCoalescingOperatorRector; use Rector\DowngradePhp74\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector; use Rector\DowngradePhp74\Rector\FuncCall\DowngradeStripTagsCallWithArrayRector; @@ -39,5 +39,5 @@ return static function (ContainerConfigurator $containerConfigurator): void { $parameters->set(Option::IMPORT_DOC_BLOCKS, false); $services->set(DowngradeFreadFwriteFalsyToNegationRector::class); - $services->set(DowngradeReturnSelfTypeDeclarationRector::class); + $services->set(DowngradeSelfTypeDeclarationRector::class); }; diff --git a/config/set/downgrade-php80.php b/config/set/downgrade-php80.php index a85759b4e46..b97d65dc41d 100644 --- a/config/set/downgrade-php80.php +++ b/config/set/downgrade-php80.php @@ -9,11 +9,9 @@ use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector; use Rector\DowngradePhp80\Rector\ClassConstFetch\DowngradeClassOnObjectToGetClassRector; use Rector\DowngradePhp80\Rector\ClassMethod\DowngradeTrailingCommasInParamUseRector; use Rector\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector; -use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeParamMixedTypeDeclarationRector; -use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeReturnMixedTypeDeclarationRector; -use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeReturnStaticTypeDeclarationRector; -use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeParamDeclarationRector; -use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeReturnDeclarationRector; +use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector; +use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeStaticTypeDeclarationRector; +use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector; use Rector\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector; use Rector\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; @@ -21,11 +19,9 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura return static function (ContainerConfigurator $containerConfigurator): void { $services = $containerConfigurator->services(); $services->set(DowngradeUnionTypeTypedPropertyRector::class); - $services->set(DowngradeUnionTypeReturnDeclarationRector::class); - $services->set(DowngradeUnionTypeParamDeclarationRector::class); - $services->set(DowngradeParamMixedTypeDeclarationRector::class); - $services->set(DowngradeReturnMixedTypeDeclarationRector::class); - $services->set(DowngradeReturnStaticTypeDeclarationRector::class); + $services->set(DowngradeUnionTypeDeclarationRector::class); + $services->set(DowngradeMixedTypeDeclarationRector::class); + $services->set(DowngradeStaticTypeDeclarationRector::class); $parameters = $containerConfigurator->parameters(); $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_74); diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index 9192658a724..9c5a1cb05bf 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 673 Rules Overview +# 667 Rules Overview
@@ -34,17 +34,17 @@ - [DoctrineGedmoToKnplabs](#doctrinegedmotoknplabs) (7) -- [DowngradePhp70](#downgradephp70) (2) +- [DowngradePhp70](#downgradephp70) (1) -- [DowngradePhp71](#downgradephp71) (9) +- [DowngradePhp71](#downgradephp71) (7) -- [DowngradePhp72](#downgradephp72) (3) +- [DowngradePhp72](#downgradephp72) (2) - [DowngradePhp73](#downgradephp73) (4) - [DowngradePhp74](#downgradephp74) (11) -- [DowngradePhp80](#downgradephp80) (12) +- [DowngradePhp80](#downgradephp80) (10) - [EarlyReturn](#earlyreturn) (11) @@ -2535,7 +2535,7 @@ return static function (ContainerConfigurator $containerConfigurator): void { ### SplitDoubleAssignRector -Split multiple inline assigns to `each` own lines default value, to prevent undefined array issues +Split multiple inline assigns to each own lines default value, to prevent undefined array issues - class: [`Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector`](/rules/coding-style/src/Rector/Assign/SplitDoubleAssignRector.php) @@ -4337,10 +4337,11 @@ Turns fluent interface calls to classic ones. ```diff $someClass = new SomeClass(); --return $someClass->someFunction() -- ->otherFunction(); +$someClass->someFunction(); +$someClass->otherFunction(); + +-return $someClass->someFunction() +- ->otherFunction(); +return $someClass; ``` @@ -5759,44 +5760,22 @@ Change Tree from gedmo/doctrine-extensions to knplabs/doctrine-behaviors ## DowngradePhp70 -### DowngradeTypeParamDeclarationRector +### DowngradeTypeDeclarationRector -Remove the type params, add `@param` tags instead +Remove the type params and return type, add `@param` and `@return` tags instead -- class: [`Rector\DowngradePhp70\Rector\FunctionLike\DowngradeTypeParamDeclarationRector`](/rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeParamDeclarationRector.php) +- class: [`Rector\DowngradePhp70\Rector\FunctionLike\DowngradeTypeDeclarationRector`](/rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeDeclarationRector.php) ```diff class SomeClass { -- public function run(string $input) +- public function run(string $input): string + /** + * @param string $input ++ * @return string + */ + public function run($input) { - // do something - } - } -``` - -
- -### DowngradeTypeReturnDeclarationRector - -Remove returning types, add a `@return` tag instead - -- class: [`Rector\DowngradePhp70\Rector\FunctionLike\DowngradeTypeReturnDeclarationRector`](/rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeReturnDeclarationRector.php) - -```diff - class SomeClass - { -- public function getResponse(): string -+ /** -+ * @return string -+ */ -+ public function getResponse() - { - return 'Hello world'; } } ``` @@ -5825,42 +5804,21 @@ Downgrade class constant visibility
-### DowngradeIterablePseudoTypeParamDeclarationRector +### DowngradeIterablePseudoTypeDeclarationRector -Remove the iterable pseudo type params, add `@param` tags instead +Remove the iterable pseudo type params and returns, add `@param` and `@return` tags instead -- class: [`Rector\DowngradePhp71\Rector\FunctionLike\DowngradeIterablePseudoTypeParamDeclarationRector`](/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeParamDeclarationRector.php) +- class: [`Rector\DowngradePhp71\Rector\FunctionLike\DowngradeIterablePseudoTypeDeclarationRector`](/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeDeclarationRector.php) ```diff class SomeClass { -- public function run(iterable $iterator) +- public function run(iterable $iterator): iterable + /** + * @param mixed[]|\Traversable $iterator -+ */ -+ public function run($iterator) - { - // do something - } - } -``` - -
- -### DowngradeIterablePseudoTypeReturnDeclarationRector - -Remove returning iterable pseud type, add a `@return` tag instead - -- class: [`Rector\DowngradePhp71\Rector\FunctionLike\DowngradeIterablePseudoTypeReturnDeclarationRector`](/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeReturnDeclarationRector.php) - -```diff - class SomeClass - { -- public function run(): iterable -+ /** + * @return mixed[]|\Traversable + */ -+ public function run() ++ public function run($iterator) { // do something } @@ -5886,47 +5844,22 @@ Downgrade negative string offset to `strlen`
-### DowngradeNullableTypeParamDeclarationRector +### DowngradeNullableTypeDeclarationRector Remove the nullable type params, add `@param` tags instead -- class: [`Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeParamDeclarationRector`](/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeParamDeclarationRector.php) +- class: [`Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeDeclarationRector`](/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeDeclarationRector.php) ```diff class SomeClass { -- public function run(?string $input) +- public function run(?string $input): ?string + /** + * @param string|null $input ++ * @return string|null $input + */ + public function run($input) { - // do something - } - } -``` - -
- -### DowngradeNullableTypeReturnDeclarationRector - -Remove returning nullable types, add a `@return` tag instead - -- class: [`Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeReturnDeclarationRector`](/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeReturnDeclarationRector.php) - -```diff - class SomeClass - { -- public function getResponseOrNothing(bool $flag): ?string -+ /** -+ * @return string|null -+ */ -+ public function getResponseOrNothing(bool $flag) - { - if ($flag) { - return 'Hello world'; - } - return null; } } ``` @@ -5953,11 +5886,11 @@ Downgrade single one | separated to multi catch exception
-### DowngradeVoidTypeReturnDeclarationRector +### DowngradeVoidTypeDeclarationRector Remove "void" return type, add a `"@return` void" tag instead -- class: [`Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeReturnDeclarationRector`](/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeVoidTypeReturnDeclarationRector.php) +- class: [`Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeDeclarationRector`](/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeVoidTypeDeclarationRector.php) ```diff class SomeClass @@ -5989,18 +5922,19 @@ Downgrade Symmetric array destructuring to `list()` function ## DowngradePhp72 -### DowngradeParamObjectTypeDeclarationRector +### DowngradeObjectTypeDeclarationRector -Remove the 'PHPStan\Type\ObjectWithoutClassType' param type, add a `@param` tag instead +Remove the "object" param and return type, add a `@param` and `@return` tags instead -- class: [`Rector\DowngradePhp72\Rector\FunctionLike\DowngradeParamObjectTypeDeclarationRector`](/rules/downgrade-php72/src/Rector/FunctionLike/DowngradeParamObjectTypeDeclarationRector.php) +- class: [`Rector\DowngradePhp72\Rector\FunctionLike\DowngradeObjectTypeDeclarationRector`](/rules/downgrade-php72/src/Rector/FunctionLike/DowngradeObjectTypeDeclarationRector.php) ```diff class SomeClass { -- public function someFunction(object $someObject) +- public function someFunction(object $someObject): object + /** + * @param object $someObject ++ * @return object + */ + public function someFunction($someObject) { @@ -6043,28 +5977,6 @@ Remove argument type declarations in the parent and in all child classes, whenev
-### DowngradeReturnObjectTypeDeclarationRector - -Remove "object" return type, add a `"@return` object" tag instead - -- class: [`Rector\DowngradePhp72\Rector\FunctionLike\DowngradeReturnObjectTypeDeclarationRector`](/rules/downgrade-php72/src/Rector/FunctionLike/DowngradeReturnObjectTypeDeclarationRector.php) - -```diff - class SomeClass - { -- public function getSomeObject(): object -+ /** -+ * @return object -+ */ -+ public function getSomeObject() - { - return new SomeObject(); - } - } -``` - -
- ## DowngradePhp73 ### DowngradeFlexibleHeredocSyntaxRector @@ -6238,7 +6150,9 @@ Remove contravariant argument type declarations class A { public function contraVariantArguments(ChildType $type) - { /* … */ } +- { +- } ++ { } } class B extends A @@ -6248,7 +6162,8 @@ Remove contravariant argument type declarations + * @param ParentType $type + */ + public function contraVariantArguments($type) - { /* … */ } + { + } } ``` @@ -6267,7 +6182,8 @@ Make method return same type as parent class A { public function covariantReturnTypes(): ParentType - { /* … */ } + { + } } class B extends A @@ -6277,7 +6193,8 @@ Make method return same type as parent + * @return ChildType + */ + public function covariantReturnTypes(): ParentType - { /* … */ } + { + } } ``` @@ -6333,14 +6250,14 @@ Remove "_" as thousands separator in numbers
-### DowngradeReturnSelfTypeDeclarationRector +### DowngradeSelfTypeDeclarationRector Remove "self" return type, add a `"@return` self" tag instead -- class: [`Rector\DowngradePhp74\Rector\ClassMethod\DowngradeReturnSelfTypeDeclarationRector`](/rules/downgrade-php74/src/Rector/ClassMethod/DowngradeReturnSelfTypeDeclarationRector.php) +- class: [`Rector\DowngradePhp74\Rector\ClassMethod\DowngradeSelfTypeDeclarationRector`](/rules/downgrade-php74/src/Rector/ClassMethod/DowngradeSelfTypeDeclarationRector.php) ```diff - class A + class SomeClass { - public function foo(): self + public function foo() @@ -6456,6 +6373,28 @@ Downgrade `match()` to `switch()`
+### DowngradeMixedTypeDeclarationRector + +Remove the "mixed" param and return type, add a `@param` and `@return` tag instead + +- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector`](/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeMixedTypeDeclarationRector.php) + +```diff + class SomeClass + { +- public function someFunction(mixed $anything): mixed ++ /** ++ * @param mixed $anything ++ * @return mixed ++ */ ++ public function someFunction($anything) + { + } + } +``` + +
+ ### DowngradeNonCapturingCatchesRector Downgrade catch () without variable to one @@ -6494,27 +6433,6 @@ Change nullsafe operator to ternary operator rector
-### DowngradeParamMixedTypeDeclarationRector - -Remove the 'PHPStan\Type\MixedType' param type, add a `@param` tag instead - -- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeParamMixedTypeDeclarationRector`](/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeParamMixedTypeDeclarationRector.php) - -```diff - class SomeClass - { -- public function someFunction(mixed $anything) -+ /** -+ * @param mixed $anything -+ */ -+ public function someFunction($anything) - { - } - } -``` - -
- ### DowngradePropertyPromotionRector Change constructor property promotion to property asssign @@ -6536,36 +6454,11 @@ Change constructor property promotion to property asssign
-### DowngradeReturnMixedTypeDeclarationRector +### DowngradeStaticTypeDeclarationRector -Remove "mixed" return type, add a `"@return` mixed" tag instead +Remove "static" return and param type, add a `"@param` `$this"` and `"@return` `$this"` tag instead -- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeReturnMixedTypeDeclarationRector`](/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeReturnMixedTypeDeclarationRector.php) - -```diff - class SomeClass - { -- public function getAnything(bool $flag): mixed -+ /** -+ * @return mixed -+ */ -+ public function getAnything(bool $flag) - { - if ($flag) { - return 1; - } - return 'Hello world' - } - } -``` - -
- -### DowngradeReturnStaticTypeDeclarationRector - -Remove "static" return type, add a `"@return` `$this"` tag instead - -- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeReturnStaticTypeDeclarationRector`](/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector.php) +- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeStaticTypeDeclarationRector`](/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeStaticTypeDeclarationRector.php) ```diff class SomeClass @@ -6613,18 +6506,19 @@ Remove trailing commas in param or use list
-### DowngradeUnionTypeParamDeclarationRector +### DowngradeUnionTypeDeclarationRector -Remove the union type params, add `@param` tags instead +Remove the union type params and returns, add `@param/@return` tags instead -- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeParamDeclarationRector`](/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeParamDeclarationRector.php) +- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector`](/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector.php) ```diff class SomeClass { -- public function echoInput(string|int $input) +- public function echoInput(string|int $input): int|bool + /** + * @param string|int $input ++ * @return int|bool + */ + public function echoInput($input) { @@ -6635,31 +6529,6 @@ Remove the union type params, add `@param` tags instead
-### DowngradeUnionTypeReturnDeclarationRector - -Remove returning union types, add a `@return` tag instead - -- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeReturnDeclarationRector`](/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeReturnDeclarationRector.php) - -```diff - class SomeClass - { -- public function getSomeObject(bool $flag): string|int -+ /** -+ * @return string|int -+ */ -+ public function getSomeObject(bool $flag) - { - if ($flag) { - return 1; - } - return 'Hello world'; - } - } -``` - -
- ### DowngradeUnionTypeTypedPropertyRector Removes union type property type definition, adding `@var` annotations instead. @@ -7468,14 +7337,14 @@ Renames property to respect is/has/was method naming ```diff class SomeClass { -- private $full = false; -+ private $isFull = false; +-private $full = false; ++private $isFull = false; - public function isFull() - { -- return $this->full; -+ return $this->isFull; - } + public function isFull() + { +- return $this->full; ++ return $this->isFull; + } + } ``` @@ -7539,16 +7408,16 @@ Renames value variable name in foreach loop to match expression variable ```diff class SomeClass { - public function run() - { - $array = []; -- foreach ($variables as $foo) { -- $array[] = $property; -+ foreach ($variables as $variable) { -+ $array[] = $variable; - } + public function run() + { + $array = []; +- foreach ($variables as $foo) { +- $array[] = $property; ++ foreach ($variables as $variable) { ++ $array[] = $variable; } } + } ```
@@ -11678,14 +11547,17 @@ Change annotation to attribute - class: [`Rector\Php80\Rector\Class_\AnnotationToAttributeRector`](/rules/php80/src/Rector/Class_/AnnotationToAttributeRector.php) ```diff - use Doctrine\ORM\Attributes as ORM; + use Symfony\Component\Routing\Annotation\Route; --/** -- * @ORM\Entity -- */ -+#[ORM\Entity] - class SomeClass + class SymfonyRoute { +- /** +- * @Route("/path", name="action") +- */ ++ #[Route(path: '/path', name: 'action')] + public function action() + { + } } ``` @@ -13077,7 +12949,7 @@ return static function (ContainerConfigurator $containerConfigurator): void { ### PassFactoryToUniqueObjectRector -Convert new `X/Static::call()` to factories in entities, pass them via constructor to `each` other +Convert new `X/Static::call()` to factories in entities, pass them via constructor to each other :wrench: **configure it!** diff --git a/rules/downgrade-php70/src/Contract/Rector/DowngradeParamDeclarationRectorInterface.php b/rules/downgrade-php70/src/Contract/Rector/DowngradeParamDeclarationRectorInterface.php index fd4439db51b..45f518bb65e 100644 --- a/rules/downgrade-php70/src/Contract/Rector/DowngradeParamDeclarationRectorInterface.php +++ b/rules/downgrade-php70/src/Contract/Rector/DowngradeParamDeclarationRectorInterface.php @@ -12,5 +12,5 @@ interface DowngradeParamDeclarationRectorInterface /** * Indicate if the parameter must be removed */ - public function shouldRemoveParamDeclaration(Param $param, FunctionLike $functionLike): bool; + public function isNullableParam(Param $param, FunctionLike $functionLike): bool; } diff --git a/rules/downgrade-php70/src/Rector/FunctionLike/AbstractDowngradeParamDeclarationRector.php b/rules/downgrade-php70/src/Rector/FunctionLike/AbstractDowngradeParamDeclarationRector.php deleted file mode 100644 index 91052221d07..00000000000 --- a/rules/downgrade-php70/src/Rector/FunctionLike/AbstractDowngradeParamDeclarationRector.php +++ /dev/null @@ -1,92 +0,0 @@ -phpDocTypeChanger = $phpDocTypeChanger; - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [Function_::class, ClassMethod::class]; - } - - /** - * @param ClassMethod|Function_ $node - */ - public function refactor(Node $node): ?Node - { - if ($node->params === []) { - return null; - } - - foreach ($node->params as $param) { - $this->refactorParam($param, $node); - } - - return null; - } - - /** - * @param ClassMethod|Function_ $functionLike - */ - private function refactorParam(Param $param, FunctionLike $functionLike): void - { - if (! $this->shouldRemoveParamDeclaration($param, $functionLike)) { - return; - } - - $this->decorateWithDocBlock($functionLike, $param); - $param->type = null; - } - - /** - * @param ClassMethod|Function_ $functionLike - */ - private function decorateWithDocBlock(FunctionLike $functionLike, Param $param): void - { - if ($param->type === null) { - return; - } - - $type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type); - if ($type instanceof IterableType) { - $type = new UnionType([$type, new IntersectionType([new ObjectType(Traversable::class)])]); - } - - $paramName = $this->getName($param->var) ?? ''; - - $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike); - $this->phpDocTypeChanger->changeParamType($phpDocInfo, $type, $param, $paramName); - } -} diff --git a/rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeDeclarationRector.php b/rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeDeclarationRector.php new file mode 100644 index 00000000000..5345140ab62 --- /dev/null +++ b/rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeDeclarationRector.php @@ -0,0 +1,97 @@ +phpDocFromTypeDeclarationDecorator = $phpDocFromTypeDeclarationDecorator; + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [Function_::class, ClassMethod::class]; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition( + 'Remove the type params and return type, add @param and @return tags instead', + [ + new CodeSample( + <<<'CODE_SAMPLE' +class SomeClass +{ + public function run(string $input): string + { + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +class SomeClass +{ + /** + * @param string $input + * @return string + */ + public function run($input) + { + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @param Function_|ClassMethod $node + */ + public function refactor(Node $node): ?Node + { + $this->refactorParams($node); + $this->phpDocFromTypeDeclarationDecorator->decorateReturn($node); + + return $node; + } + + /** + * @param ClassMethod|Function_ $functionLike + */ + private function refactorParams(FunctionLike $functionLike): void + { + foreach ($functionLike->params as $param) { + $this->phpDocFromTypeDeclarationDecorator->decorateParam( + $param, + $functionLike, + [ArrayType::class, CallableType::class] + ); + } + } +} diff --git a/rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeParamDeclarationRector.php b/rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeParamDeclarationRector.php deleted file mode 100644 index 86ee07d7da3..00000000000 --- a/rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeParamDeclarationRector.php +++ /dev/null @@ -1,67 +0,0 @@ -type === null) { - return false; - } - - $type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type); - if (is_a($type, ArrayType::class, true)) { - return false; - } - return ! is_a($type, CallableType::class, true); - } -} diff --git a/rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeReturnDeclarationRector.php b/rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeReturnDeclarationRector.php deleted file mode 100644 index 58d62970465..00000000000 --- a/rules/downgrade-php70/src/Rector/FunctionLike/DowngradeTypeReturnDeclarationRector.php +++ /dev/null @@ -1,58 +0,0 @@ -parameters(); - $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersionFeature::NULLABLE_TYPE - 1); + $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersionFeature::SCALAR_TYPES - 1); $services = $containerConfigurator->services(); - $services->set(DowngradeTypeParamDeclarationRector::class); + $services->set(DowngradeTypeDeclarationRector::class); }; diff --git a/rules/downgrade-php70/tests/Rector/FunctionLike/DowngradeTypeReturnDeclarationRector/DowngradeTypeReturnDeclarationRectorTest.php b/rules/downgrade-php70/tests/Rector/FunctionLike/DowngradeTypeReturnDeclarationRector/DowngradeTypeReturnDeclarationRectorTest.php deleted file mode 100644 index 96b9f1ff954..00000000000 --- a/rules/downgrade-php70/tests/Rector/FunctionLike/DowngradeTypeReturnDeclarationRector/DowngradeTypeReturnDeclarationRectorTest.php +++ /dev/null @@ -1,31 +0,0 @@ -doTestFileInfo($fileInfo); - } - - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - protected function provideConfigFilePath(): string - { - return __DIR__ . '/config/php_70.php'; - } -} diff --git a/rules/downgrade-php70/tests/Rector/FunctionLike/DowngradeTypeReturnDeclarationRector/config/php_70.php b/rules/downgrade-php70/tests/Rector/FunctionLike/DowngradeTypeReturnDeclarationRector/config/php_70.php deleted file mode 100644 index ee44d7e1137..00000000000 --- a/rules/downgrade-php70/tests/Rector/FunctionLike/DowngradeTypeReturnDeclarationRector/config/php_70.php +++ /dev/null @@ -1,16 +0,0 @@ -parameters(); - $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersionFeature::NULLABLE_TYPE - 1); - - $services = $containerConfigurator->services(); - $services->set(DowngradeTypeReturnDeclarationRector::class); -}; diff --git a/rules/downgrade-php71/config/config.php b/rules/downgrade-php71/config/config.php new file mode 100644 index 00000000000..744ba42f805 --- /dev/null +++ b/rules/downgrade-php71/config/config.php @@ -0,0 +1,17 @@ +services(); + + $services->defaults() + ->public() + ->autowire() + ->autoconfigure(); + + $services->load('Rector\DowngradePhp71\\', __DIR__ . '/../src') + ->exclude([__DIR__ . '/../src/Rector']); +}; diff --git a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeDeclarationRector.php b/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeDeclarationRector.php new file mode 100644 index 00000000000..4f184ac32c6 --- /dev/null +++ b/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeDeclarationRector.php @@ -0,0 +1,90 @@ +phpDocFromTypeDeclarationDecorator = $phpDocFromTypeDeclarationDecorator; + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [Function_::class, ClassMethod::class]; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition( + 'Remove the iterable pseudo type params and returns, add @param and @return tags instead', + [ + new CodeSample( + <<<'CODE_SAMPLE' +class SomeClass +{ + public function run(iterable $iterator): iterable + { + // do something + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +class SomeClass +{ + /** + * @param mixed[]|\Traversable $iterator + * @return mixed[]|\Traversable + */ + public function run($iterator) + { + // do something + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @param Function_|ClassMethod $node + */ + public function refactor(Node $node): ?Node + { + foreach ($node->params as $param) { + $this->phpDocFromTypeDeclarationDecorator->decorateParamWithSpecificType( + $param, + $node, + IterableType::class + ); + } + + $this->phpDocFromTypeDeclarationDecorator->decorateReturnWithSpecificType($node, IterableType::class); + + return $node; + } +} diff --git a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeParamDeclarationRector.php b/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeParamDeclarationRector.php deleted file mode 100644 index a5f7f7c8bfd..00000000000 --- a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeParamDeclarationRector.php +++ /dev/null @@ -1,62 +0,0 @@ -type === null) { - return false; - } - if (! $param->type instanceof Identifier) { - return false; - } - return $param->type->toString() === 'iterable'; - } -} diff --git a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeReturnDeclarationRector.php b/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeReturnDeclarationRector.php deleted file mode 100644 index 1b5616badf2..00000000000 --- a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeIterablePseudoTypeReturnDeclarationRector.php +++ /dev/null @@ -1,65 +0,0 @@ -returnType; - - if ($functionLikeReturnType === null) { - return false; - } - - return $this->isName($functionLikeReturnType, 'iterable'); - } -} diff --git a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeDeclarationRector.php b/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeDeclarationRector.php new file mode 100644 index 00000000000..a73c13cd8ac --- /dev/null +++ b/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeDeclarationRector.php @@ -0,0 +1,140 @@ +phpDocTypeChanger = $phpDocTypeChanger; + $this->phpDocFromTypeDeclarationDecorator = $phpDocFromTypeDeclarationDecorator; + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [Function_::class, ClassMethod::class]; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition('Remove the nullable type params, add @param tags instead', [ + new CodeSample( + <<<'CODE_SAMPLE' +class SomeClass +{ + public function run(?string $input): ?string + { + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +class SomeClass +{ + /** + * @param string|null $input + * @return string|null $input + */ + public function run($input) + { + } +} +CODE_SAMPLE + ), + ]); + } + + /** + * @param ClassMethod|Function_ $node + */ + public function refactor(Node $node): ?Node + { + foreach ($node->params as $param) { + $this->refactorParamType($param, $node); + } + + $this->phpDocFromTypeDeclarationDecorator->decorateReturnWithSpecificType($node, NullableType::class); + + return $node; + } + + private function isNullableParam(Param $param): bool + { + if ($param->variadic) { + return false; + } + + if ($param->type === null) { + return false; + } + + // Check it is the union type + return $param->type instanceof NullableType; + } + + /** + * @param ClassMethod|Function_ $functionLike + */ + private function refactorParamType(Param $param, FunctionLike $functionLike): void + { + if (! $this->isNullableParam($param)) { + return; + } + + $this->decorateWithDocBlock($functionLike, $param); + $param->type = null; + } + + /** + * @param ClassMethod|Function_ $functionLike + */ + private function decorateWithDocBlock(FunctionLike $functionLike, Param $param): void + { + if ($param->type === null) { + return; + } + + $type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type); + + $paramName = $this->getName($param->var); + if ($paramName === null) { + throw new ShouldNotHappenException(); + } + + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike); + $this->phpDocTypeChanger->changeParamType($phpDocInfo, $type, $param, $paramName); + } +} diff --git a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeParamDeclarationRector.php b/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeParamDeclarationRector.php deleted file mode 100644 index 83e48e1a7dc..00000000000 --- a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeParamDeclarationRector.php +++ /dev/null @@ -1,65 +0,0 @@ -variadic) { - return false; - } - - if ($param->type === null) { - return false; - } - - // Check it is the union type - return $param->type instanceof NullableType; - } -} diff --git a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeReturnDeclarationRector.php b/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeReturnDeclarationRector.php deleted file mode 100644 index 87a00c3cbdb..00000000000 --- a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeReturnDeclarationRector.php +++ /dev/null @@ -1,66 +0,0 @@ -returnType instanceof NullableType; - } -} diff --git a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeVoidTypeDeclarationRector.php b/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeVoidTypeDeclarationRector.php new file mode 100644 index 00000000000..e82676d3581 --- /dev/null +++ b/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeVoidTypeDeclarationRector.php @@ -0,0 +1,79 @@ +phpDocFromTypeDeclarationDecorator = $phpDocFromTypeDeclarationDecorator; + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [Function_::class, ClassMethod::class]; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition( + 'Remove "void" return type, add a "@return void" tag instead', + [ + new CodeSample( + <<<'CODE_SAMPLE' +class SomeClass +{ + public function run(): void + { + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +class SomeClass +{ + /** + * @return void + */ + public function run() + { + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @param ClassMethod|Function_ $node + */ + public function refactor(Node $node): ?Node + { + $this->phpDocFromTypeDeclarationDecorator->decorateReturnWithSpecificType($node, VoidType::class); + + return $node; + } +} diff --git a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeVoidTypeReturnDeclarationRector.php b/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeVoidTypeReturnDeclarationRector.php deleted file mode 100644 index 84303661fc3..00000000000 --- a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeVoidTypeReturnDeclarationRector.php +++ /dev/null @@ -1,52 +0,0 @@ -staticTypeMapper = $staticTypeMapper; + $this->phpDocInfoFactory = $phpDocInfoFactory; + $this->nodeNameResolver = $nodeNameResolver; + $this->phpDocTypeChanger = $phpDocTypeChanger; + $this->typeUnwrapper = $typeUnwrapper; + } + + /** + * @param ClassMethod|Function_ $functionLike + */ + public function decorateReturn(FunctionLike $functionLike): void + { + if ($functionLike->returnType === null) { + return; + } + + $type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($functionLike->returnType); + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike); + $this->phpDocTypeChanger->changeReturnType($phpDocInfo, $type); + + $functionLike->returnType = null; + } + + /** + * @param ClassMethod|Function_ $functionLike + * @param array> $excludedTypes + */ + public function decorateParam(Param $param, FunctionLike $functionLike, array $excludedTypes = []): void + { + if ($param->type === null) { + return; + } + + $type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type); + foreach ($excludedTypes as $excludedType) { + if (is_a($type, $excludedType, true)) { + return; + } + } + + $this->moveParamTypeToParamDoc($functionLike, $param, $type); + } + + /** + * @param ClassMethod|Function_ $functionLike + * @param class-string $requireTypeNode + */ + public function decorateParamWithSpecificType( + Param $param, + FunctionLike $functionLike, + string $requireTypeNode + ): void { + if ($param->type === null) { + return; + } + + if (! $this->isTypeMatch($param->type, $requireTypeNode)) { + return; + } + + $type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type); + $this->moveParamTypeToParamDoc($functionLike, $param, $type); + } + + /** + * @param ClassMethod|Function_ $functionLike + * @param class-string $requireTypeNode + */ + public function decorateReturnWithSpecificType(FunctionLike $functionLike, string $requireTypeNode): void + { + if ($functionLike->returnType === null) { + return; + } + + if (! $this->isTypeMatch($functionLike->returnType, $requireTypeNode)) { + return; + } + + $this->decorateReturn($functionLike); + } + + /** + * @param class-string $requireTypeNodeClass + */ + private function isTypeMatch(Node $typeNode, string $requireTypeNodeClass): bool + { + if (is_a($requireTypeNodeClass, Node::class, true)) { + if (! is_a($typeNode, $requireTypeNodeClass, true)) { + return false; + } + } + + if (is_a($requireTypeNodeClass, Type::class, true)) { + $returnType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($typeNode); + + // cover nullable union types + if ($returnType instanceof UnionType) { + $returnType = $this->typeUnwrapper->unwrapNullableType($returnType); + } + + if (! is_a($returnType, $requireTypeNodeClass, true)) { + return false; + } + } + + return true; + } + + /** + * @param ClassMethod|Function_ $functionLike + */ + private function moveParamTypeToParamDoc(FunctionLike $functionLike, Param $param, Type $type): void + { + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike); + $paramName = $this->nodeNameResolver->getName($param); + $this->phpDocTypeChanger->changeParamType($phpDocInfo, $type, $param, $paramName); + + $param->type = null; + } +} diff --git a/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeIterablePseudoTypeParamDeclarationRector/DowngradeIterablePseudoTypeParamDeclarationRectorTest.php b/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeIterablePseudoTypeDeclarationRector/DowngradeIterablePseudoTypeDeclarationRectorTest.php similarity index 71% rename from rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeIterablePseudoTypeParamDeclarationRector/DowngradeIterablePseudoTypeParamDeclarationRectorTest.php rename to rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeIterablePseudoTypeDeclarationRector/DowngradeIterablePseudoTypeDeclarationRectorTest.php index 68b8d701057..e7b70e4e81c 100644 --- a/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeIterablePseudoTypeParamDeclarationRector/DowngradeIterablePseudoTypeParamDeclarationRectorTest.php +++ b/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeIterablePseudoTypeDeclarationRector/DowngradeIterablePseudoTypeDeclarationRectorTest.php @@ -2,14 +2,14 @@ declare(strict_types=1); -namespace Rector\DowngradePhp71\Tests\Rector\FunctionLike\DowngradeIterablePseudoTypeParamDeclarationRector; +namespace Rector\DowngradePhp71\Tests\Rector\FunctionLike\DowngradeIterablePseudoTypeDeclarationRector; use Iterator; -use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeIterablePseudoTypeParamDeclarationRector; +use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeIterablePseudoTypeDeclarationRector; use Rector\Testing\PHPUnit\AbstractRectorTestCase; use Symplify\SmartFileSystem\SmartFileInfo; -final class DowngradeIterablePseudoTypeParamDeclarationRectorTest extends AbstractRectorTestCase +final class DowngradeIterablePseudoTypeDeclarationRectorTest extends AbstractRectorTestCase { /** * @dataProvider provideData() @@ -26,6 +26,6 @@ final class DowngradeIterablePseudoTypeParamDeclarationRectorTest extends Abstra protected function getRectorClass(): string { - return DowngradeIterablePseudoTypeParamDeclarationRector::class; + return DowngradeIterablePseudoTypeDeclarationRector::class; } } diff --git a/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeIterablePseudoTypeParamDeclarationRector/Fixture/fixture.php.inc b/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeIterablePseudoTypeDeclarationRector/Fixture/param_iterable.php.inc similarity index 64% rename from rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeIterablePseudoTypeParamDeclarationRector/Fixture/fixture.php.inc rename to rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeIterablePseudoTypeDeclarationRector/Fixture/param_iterable.php.inc index c342e70157e..e70e7233c1f 100644 --- a/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeIterablePseudoTypeParamDeclarationRector/Fixture/fixture.php.inc +++ b/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeIterablePseudoTypeDeclarationRector/Fixture/param_iterable.php.inc @@ -1,8 +1,8 @@ doTestFileInfo($fileInfo); - } - - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - protected function getRectorClass(): string - { - return DowngradeIterablePseudoTypeReturnDeclarationRector::class; - } -} diff --git a/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeParamDeclarationRector/DowngradeNullableTypeParamDeclarationRectorTest.php b/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeDeclarationRector/DowngradeNullableTypeDeclarationRectorTest.php similarity index 73% rename from rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeParamDeclarationRector/DowngradeNullableTypeParamDeclarationRectorTest.php rename to rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeDeclarationRector/DowngradeNullableTypeDeclarationRectorTest.php index da9062c283c..4c174e7e0ec 100644 --- a/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeParamDeclarationRector/DowngradeNullableTypeParamDeclarationRectorTest.php +++ b/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeDeclarationRector/DowngradeNullableTypeDeclarationRectorTest.php @@ -2,14 +2,14 @@ declare(strict_types=1); -namespace Rector\DowngradePhp71\Tests\Rector\FunctionLike\DowngradeNullableTypeParamDeclarationRector; +namespace Rector\DowngradePhp71\Tests\Rector\FunctionLike\DowngradeNullableTypeDeclarationRector; use Iterator; -use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeParamDeclarationRector; +use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeDeclarationRector; use Rector\Testing\PHPUnit\AbstractRectorTestCase; use Symplify\SmartFileSystem\SmartFileInfo; -final class DowngradeNullableTypeParamDeclarationRectorTest extends AbstractRectorTestCase +final class DowngradeNullableTypeDeclarationRectorTest extends AbstractRectorTestCase { /** * @dataProvider provideData() @@ -26,6 +26,6 @@ final class DowngradeNullableTypeParamDeclarationRectorTest extends AbstractRect protected function getRectorClass(): string { - return DowngradeNullableTypeParamDeclarationRector::class; + return DowngradeNullableTypeDeclarationRector::class; } } diff --git a/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeParamDeclarationRector/Fixture/fixture.php.inc b/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeDeclarationRector/Fixture/param_type.php.inc similarity index 70% rename from rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeParamDeclarationRector/Fixture/fixture.php.inc rename to rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeDeclarationRector/Fixture/param_type.php.inc index 4285d0031dc..596bbc83c29 100644 --- a/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeParamDeclarationRector/Fixture/fixture.php.inc +++ b/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeDeclarationRector/Fixture/param_type.php.inc @@ -1,8 +1,8 @@ doTestFileInfo($fileInfo); - } - - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - protected function getRectorClass(): string - { - return DowngradeNullableTypeReturnDeclarationRector::class; - } -} diff --git a/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeVoidTypeReturnDeclarationRector/DowngradeVoidTypeReturnDeclarationRectorTest.php b/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeVoidTypeDeclarationRector/DowngradeVoidTypeDeclarationRectorTest.php similarity index 66% rename from rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeVoidTypeReturnDeclarationRector/DowngradeVoidTypeReturnDeclarationRectorTest.php rename to rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeVoidTypeDeclarationRector/DowngradeVoidTypeDeclarationRectorTest.php index 8fcc377c5ad..6a308a9a8f6 100644 --- a/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeVoidTypeReturnDeclarationRector/DowngradeVoidTypeReturnDeclarationRectorTest.php +++ b/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeVoidTypeDeclarationRector/DowngradeVoidTypeDeclarationRectorTest.php @@ -2,14 +2,14 @@ declare(strict_types=1); -namespace Rector\DowngradePhp71\Tests\Rector\FunctionLike\DowngradeVoidTypeReturnDeclarationRector; +namespace Rector\DowngradePhp71\Tests\Rector\FunctionLike\DowngradeVoidTypeDeclarationRector; use Iterator; -use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeReturnDeclarationRector; +use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeDeclarationRector; use Rector\Testing\PHPUnit\AbstractRectorTestCase; use Symplify\SmartFileSystem\SmartFileInfo; -final class DowngradeVoidTypeReturnDeclarationRectorTest extends AbstractRectorTestCase +final class DowngradeVoidTypeDeclarationRectorTest extends AbstractRectorTestCase { /** * @dataProvider provideData() @@ -26,6 +26,6 @@ final class DowngradeVoidTypeReturnDeclarationRectorTest extends AbstractRectorT protected function getRectorClass(): string { - return DowngradeVoidTypeReturnDeclarationRector::class; + return DowngradeVoidTypeDeclarationRector::class; } } diff --git a/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeVoidTypeReturnDeclarationRector/Fixture/docblock_exists.php.inc b/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeVoidTypeDeclarationRector/Fixture/docblock_exists.php.inc similarity index 84% rename from rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeVoidTypeReturnDeclarationRector/Fixture/docblock_exists.php.inc rename to rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeVoidTypeDeclarationRector/Fixture/docblock_exists.php.inc index fe44a965f41..c36c456d97c 100644 --- a/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeVoidTypeReturnDeclarationRector/Fixture/docblock_exists.php.inc +++ b/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeVoidTypeDeclarationRector/Fixture/docblock_exists.php.inc @@ -1,6 +1,6 @@ typeUnwrapper = $typeUnwrapper; - } - - public function shouldRemoveParamDeclaration(Param $param, FunctionLike $functionLike): bool - { - if ($param->variadic) { - return false; - } - - if ($param->type === null) { - return false; - } - - $type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type); - $type = $this->typeUnwrapper->unwrapNullableType($type); - - return is_a($type, $this->getTypeToRemove(), true); - } - - protected function getRectorDefinitionDescription(): string - { - return sprintf("Remove the '%s' param type, add a @param tag instead", $this->getTypeToRemove()); - } -} diff --git a/rules/downgrade-php72/src/Rector/FunctionLike/AbstractDowngradeReturnTypeDeclarationRector.php b/rules/downgrade-php72/src/Rector/FunctionLike/AbstractDowngradeReturnTypeDeclarationRector.php deleted file mode 100644 index 4d1cdc6dacc..00000000000 --- a/rules/downgrade-php72/src/Rector/FunctionLike/AbstractDowngradeReturnTypeDeclarationRector.php +++ /dev/null @@ -1,40 +0,0 @@ -typeUnwrapper = $typeUnwrapper; - } - - /** - * @param ClassMethod|Function_ $functionLike - */ - public function shouldRemoveReturnDeclaration(FunctionLike $functionLike): bool - { - if ($functionLike->returnType === null) { - return false; - } - - $type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($functionLike->returnType); - $type = $this->typeUnwrapper->unwrapNullableType($type); - - return is_a($type, $this->getTypeToRemove(), true); - } -} diff --git a/rules/downgrade-php72/src/Rector/FunctionLike/DowngradeObjectTypeDeclarationRector.php b/rules/downgrade-php72/src/Rector/FunctionLike/DowngradeObjectTypeDeclarationRector.php new file mode 100644 index 00000000000..0b8863b3b9a --- /dev/null +++ b/rules/downgrade-php72/src/Rector/FunctionLike/DowngradeObjectTypeDeclarationRector.php @@ -0,0 +1,88 @@ +phpDocFromTypeDeclarationDecorator = $phpDocFromTypeDeclarationDecorator; + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [Function_::class, ClassMethod::class]; + } + + /** + * @param Function_|ClassMethod $node + */ + public function refactor(Node $node): ?Node + { + foreach ($node->params as $param) { + $this->phpDocFromTypeDeclarationDecorator->decorateParamWithSpecificType( + $param, + $node, + ObjectWithoutClassType::class + ); + } + + $this->phpDocFromTypeDeclarationDecorator->decorateReturnWithSpecificType($node, ObjectWithoutClassType::class); + + return $node; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition( + 'Remove the "object" param and return type, add a @param and @return tags instead', + [ + new CodeSample( + <<<'CODE_SAMPLE' +class SomeClass +{ + public function someFunction(object $someObject): object + { + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +class SomeClass +{ + /** + * @param object $someObject + * @return object + */ + public function someFunction($someObject) + { + } +} +CODE_SAMPLE + ), + ] + ); + } +} diff --git a/rules/downgrade-php72/src/Rector/FunctionLike/DowngradeParamObjectTypeDeclarationRector.php b/rules/downgrade-php72/src/Rector/FunctionLike/DowngradeParamObjectTypeDeclarationRector.php deleted file mode 100644 index 2b0c6aa6072..00000000000 --- a/rules/downgrade-php72/src/Rector/FunctionLike/DowngradeParamObjectTypeDeclarationRector.php +++ /dev/null @@ -1,51 +0,0 @@ -getRectorDefinitionDescription(), - [ - new CodeSample( - <<<'CODE_SAMPLE' -class SomeClass -{ - public function someFunction(object $someObject) - { - } -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -class SomeClass -{ - /** - * @param object $someObject - */ - public function someFunction($someObject) - { - } -} -CODE_SAMPLE - ), - ] - ); - } - - public function getTypeToRemove(): string - { - return ObjectWithoutClassType::class; - } -} diff --git a/rules/downgrade-php72/src/Rector/FunctionLike/DowngradeReturnObjectTypeDeclarationRector.php b/rules/downgrade-php72/src/Rector/FunctionLike/DowngradeReturnObjectTypeDeclarationRector.php deleted file mode 100644 index 2507d9dc548..00000000000 --- a/rules/downgrade-php72/src/Rector/FunctionLike/DowngradeReturnObjectTypeDeclarationRector.php +++ /dev/null @@ -1,53 +0,0 @@ -doTestFileInfo($fileInfo); - } - - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - protected function getRectorClass(): string - { - return DowngradeReturnObjectTypeDeclarationRector::class; - } -} diff --git a/rules/downgrade-php72/tests/Rector/FunctionLike/DowngradeReturnObjectTypeDeclarationRector/Source/AnotherClass.php b/rules/downgrade-php72/tests/Rector/FunctionLike/DowngradeReturnObjectTypeDeclarationRector/Source/AnotherClass.php deleted file mode 100644 index 29f1f4b5cd6..00000000000 --- a/rules/downgrade-php72/tests/Rector/FunctionLike/DowngradeReturnObjectTypeDeclarationRector/Source/AnotherClass.php +++ /dev/null @@ -1,10 +0,0 @@ -phpDocTypeChanger = $phpDocTypeChanger; + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [ClassMethod::class, Function_::class]; + } + public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Remove contravariant argument type declarations', [ @@ -38,13 +59,15 @@ class ChildType extends ParentType {} class A { public function contraVariantArguments(ChildType $type) - { /* … */ } + { + } } class B extends A { public function contraVariantArguments(ParentType $type) - { /* … */ } + { + } } CODE_SAMPLE , @@ -55,7 +78,7 @@ class ChildType extends ParentType {} class A { public function contraVariantArguments(ChildType $type) - { /* … */ } + { } } class B extends A @@ -64,14 +87,15 @@ class B extends A * @param ParentType $type */ public function contraVariantArguments($type) - { /* … */ } + { + } } CODE_SAMPLE ), ]); } - public function shouldRemoveParamDeclaration(Param $param, FunctionLike $functionLike): bool + public function isNullableParam(Param $param, FunctionLike $functionLike): bool { if ($param->variadic) { return false; @@ -95,6 +119,22 @@ CODE_SAMPLE return $this->getDifferentParamTypeFromAncestorClass($param, $functionLike) !== null; } + /** + * @param ClassMethod|Function_ $node + */ + public function refactor(Node $node): ?Node + { + if ($node->params === []) { + return null; + } + + foreach ($node->params as $param) { + $this->refactorParam($param, $node); + } + + return null; + } + private function getDifferentParamTypeFromAncestorClass(Param $param, FunctionLike $functionLike): ?string { $scope = $functionLike->getAttribute(AttributeKey::SCOPE); @@ -190,4 +230,33 @@ CODE_SAMPLE return null; } + + /** + * @param ClassMethod|Function_ $functionLike + */ + private function refactorParam(Param $param, FunctionLike $functionLike): void + { + if (! $this->isNullableParam($param, $functionLike)) { + return; + } + + $this->decorateWithDocBlock($functionLike, $param); + $param->type = null; + } + + /** + * @param ClassMethod|Function_ $functionLike + */ + private function decorateWithDocBlock(FunctionLike $functionLike, Param $param): void + { + if ($param->type === null) { + return; + } + + $type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type); + $paramName = $this->getName($param->var) ?? ''; + + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike); + $this->phpDocTypeChanger->changeParamType($phpDocInfo, $type, $param, $paramName); + } } diff --git a/rules/downgrade-php74/src/Rector/ClassMethod/DowngradeReturnSelfTypeDeclarationRector.php b/rules/downgrade-php74/src/Rector/ClassMethod/DowngradeSelfTypeDeclarationRector.php similarity index 55% rename from rules/downgrade-php74/src/Rector/ClassMethod/DowngradeReturnSelfTypeDeclarationRector.php rename to rules/downgrade-php74/src/Rector/ClassMethod/DowngradeSelfTypeDeclarationRector.php index 0fa0f919ad3..095113e0207 100644 --- a/rules/downgrade-php74/src/Rector/ClassMethod/DowngradeReturnSelfTypeDeclarationRector.php +++ b/rules/downgrade-php74/src/Rector/ClassMethod/DowngradeSelfTypeDeclarationRector.php @@ -7,15 +7,34 @@ namespace Rector\DowngradePhp74\Rector\ClassMethod; use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Type\ThisType; -use Rector\DowngradePhp72\Rector\FunctionLike\AbstractDowngradeReturnTypeDeclarationRector; +use Rector\Core\Rector\AbstractRector; +use Rector\DowngradePhp71\TypeDeclaration\PhpDocFromTypeDeclarationDecorator; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Rector\DowngradePhp74\Tests\Rector\ClassMethod\DowngradeReturnSelfTypeDeclarationRector\DowngradeReturnSelfTypeDeclarationRectorTest + * @see \Rector\DowngradePhp74\Tests\Rector\ClassMethod\DowngradeSelfTypeDeclarationRector\DowngradeSelfTypeDeclarationRectorTest */ -final class DowngradeReturnSelfTypeDeclarationRector extends AbstractDowngradeReturnTypeDeclarationRector +final class DowngradeSelfTypeDeclarationRector extends AbstractRector { + /** + * @var PhpDocFromTypeDeclarationDecorator + */ + private $phpDocFromTypeDeclarationDecorator; + + public function __construct(PhpDocFromTypeDeclarationDecorator $phpDocFromTypeDeclarationDecorator) + { + $this->phpDocFromTypeDeclarationDecorator = $phpDocFromTypeDeclarationDecorator; + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [ClassMethod::class]; + } + public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -23,7 +42,7 @@ final class DowngradeReturnSelfTypeDeclarationRector extends AbstractDowngradeRe [ new CodeSample( <<<'CODE_SAMPLE' -class A +class SomeClass { public function foo(): self { @@ -33,7 +52,7 @@ class A CODE_SAMPLE , <<<'CODE_SAMPLE' -class A +class SomeClass { public function foo() { @@ -47,15 +66,12 @@ CODE_SAMPLE } /** - * @return array> + * @param ClassMethod $node */ - public function getNodeTypes(): array + public function refactor(Node $node): ?Node { - return [ClassMethod::class]; - } + $this->phpDocFromTypeDeclarationDecorator->decorateReturnWithSpecificType($node, ThisType::class); - public function getTypeToRemove(): string - { - return ThisType::class; + return $node; } } diff --git a/rules/downgrade-php74/tests/Rector/ClassMethod/DowngradeReturnSelfTypeDeclarationRector/DowngradeReturnSelfTypeDeclarationRectorTest.php b/rules/downgrade-php74/tests/Rector/ClassMethod/DowngradeSelfTypeDeclarationRector/DowngradeSelfTypeDeclarationRectorTest.php similarity index 68% rename from rules/downgrade-php74/tests/Rector/ClassMethod/DowngradeReturnSelfTypeDeclarationRector/DowngradeReturnSelfTypeDeclarationRectorTest.php rename to rules/downgrade-php74/tests/Rector/ClassMethod/DowngradeSelfTypeDeclarationRector/DowngradeSelfTypeDeclarationRectorTest.php index af0e420a62e..34074be30e8 100644 --- a/rules/downgrade-php74/tests/Rector/ClassMethod/DowngradeReturnSelfTypeDeclarationRector/DowngradeReturnSelfTypeDeclarationRectorTest.php +++ b/rules/downgrade-php74/tests/Rector/ClassMethod/DowngradeSelfTypeDeclarationRector/DowngradeSelfTypeDeclarationRectorTest.php @@ -2,14 +2,14 @@ declare(strict_types=1); -namespace Rector\DowngradePhp74\Tests\Rector\ClassMethod\DowngradeReturnSelfTypeDeclarationRector; +namespace Rector\DowngradePhp74\Tests\Rector\ClassMethod\DowngradeSelfTypeDeclarationRector; use Iterator; -use Rector\DowngradePhp74\Rector\ClassMethod\DowngradeReturnSelfTypeDeclarationRector; +use Rector\DowngradePhp74\Rector\ClassMethod\DowngradeSelfTypeDeclarationRector; use Rector\Testing\PHPUnit\AbstractRectorTestCase; use Symplify\SmartFileSystem\SmartFileInfo; -final class DowngradeReturnSelfTypeDeclarationRectorTest extends AbstractRectorTestCase +final class DowngradeSelfTypeDeclarationRectorTest extends AbstractRectorTestCase { /** * @requires PHP 7.4 @@ -27,6 +27,6 @@ final class DowngradeReturnSelfTypeDeclarationRectorTest extends AbstractRectorT protected function getRectorClass(): string { - return DowngradeReturnSelfTypeDeclarationRector::class; + return DowngradeSelfTypeDeclarationRector::class; } } diff --git a/rules/downgrade-php74/tests/Rector/ClassMethod/DowngradeReturnSelfTypeDeclarationRector/Fixture/docblock_exists.php.inc b/rules/downgrade-php74/tests/Rector/ClassMethod/DowngradeSelfTypeDeclarationRector/Fixture/docblock_exists.php.inc similarity index 84% rename from rules/downgrade-php74/tests/Rector/ClassMethod/DowngradeReturnSelfTypeDeclarationRector/Fixture/docblock_exists.php.inc rename to rules/downgrade-php74/tests/Rector/ClassMethod/DowngradeSelfTypeDeclarationRector/Fixture/docblock_exists.php.inc index 4610371a9e8..e06f3973ac8 100644 --- a/rules/downgrade-php74/tests/Rector/ClassMethod/DowngradeReturnSelfTypeDeclarationRector/Fixture/docblock_exists.php.inc +++ b/rules/downgrade-php74/tests/Rector/ClassMethod/DowngradeSelfTypeDeclarationRector/Fixture/docblock_exists.php.inc @@ -1,6 +1,6 @@ phpDocFromTypeDeclarationDecorator = $phpDocFromTypeDeclarationDecorator; + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [Function_::class, ClassMethod::class]; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition( + 'Remove the "mixed" param and return type, add a @param and @return tag instead', + [ + new CodeSample( + <<<'CODE_SAMPLE' +class SomeClass +{ + public function someFunction(mixed $anything): mixed + { + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +class SomeClass +{ + /** + * @param mixed $anything + * @return mixed + */ + public function someFunction($anything) + { + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @param ClassMethod|Function_ $node + */ + public function refactor(Node $node): ?Node + { + foreach ($node->getParams() as $param) { + $this->phpDocFromTypeDeclarationDecorator->decorateParamWithSpecificType($param, $node, MixedType::class); + } + + $this->phpDocFromTypeDeclarationDecorator->decorateReturnWithSpecificType($node, MixedType::class); + + return $node; + } +} diff --git a/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeParamMixedTypeDeclarationRector.php b/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeParamMixedTypeDeclarationRector.php deleted file mode 100644 index 58b3cb38dfe..00000000000 --- a/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeParamMixedTypeDeclarationRector.php +++ /dev/null @@ -1,52 +0,0 @@ -getRectorDefinitionDescription(), - [ - new CodeSample( - <<<'CODE_SAMPLE' -class SomeClass -{ - public function someFunction(mixed $anything) - { - } -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -class SomeClass -{ - /** - * @param mixed $anything - */ - public function someFunction($anything) - { - } -} -CODE_SAMPLE - ), - ] - ); - } - - public function getTypeToRemove(): string - { - return MixedType::class; - } -} diff --git a/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeReturnMixedTypeDeclarationRector.php b/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeReturnMixedTypeDeclarationRector.php deleted file mode 100644 index 857a4ec5078..00000000000 --- a/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeReturnMixedTypeDeclarationRector.php +++ /dev/null @@ -1,60 +0,0 @@ -phpDocFromTypeDeclarationDecorator = $phpDocFromTypeDeclarationDecorator; + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [Function_::class, ClassMethod::class]; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition( + 'Remove "static" return and param type, add a "@param $this" and "@return $this" tag instead', + [ + new CodeSample( + <<<'CODE_SAMPLE' +class SomeClass +{ + public function getStatic(): static + { + return new static(); + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +class SomeClass +{ + /** + * @return static + */ + public function getStatic() + { + return new static(); + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @param ClassMethod|Function_ $node + */ + public function refactor(Node $node): ?Node + { + foreach ($node->getParams() as $param) { + $this->phpDocFromTypeDeclarationDecorator->decorateParamWithSpecificType($param, $node, StaticType::class); + } + + $this->phpDocFromTypeDeclarationDecorator->decorateReturnWithSpecificType($node, StaticType::class); + + return $node; + } +} diff --git a/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector.php b/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector.php new file mode 100644 index 00000000000..1d8d0fda023 --- /dev/null +++ b/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector.php @@ -0,0 +1,88 @@ +phpDocFromTypeDeclarationDecorator = $phpDocFromTypeDeclarationDecorator; + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [Function_::class, ClassMethod::class]; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition( + 'Remove the union type params and returns, add @param/@return tags instead', + [ + new CodeSample( + <<<'CODE_SAMPLE' +class SomeClass +{ + public function echoInput(string|int $input): int|bool + { + echo $input; + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +class SomeClass +{ + /** + * @param string|int $input + * @return int|bool + */ + public function echoInput($input) + { + echo $input; + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @param ClassMethod|Function_ $node + */ + public function refactor(Node $node): ?Node + { + foreach ($node->getParams() as $param) { + $this->phpDocFromTypeDeclarationDecorator->decorateParamWithSpecificType($param, $node, UnionType::class); + } + + $this->phpDocFromTypeDeclarationDecorator->decorateReturnWithSpecificType($node, UnionType::class); + + return $node; + } +} diff --git a/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeParamDeclarationRector.php b/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeParamDeclarationRector.php deleted file mode 100644 index 2b8cc30d469..00000000000 --- a/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeParamDeclarationRector.php +++ /dev/null @@ -1,67 +0,0 @@ -variadic) { - return false; - } - - if ($param->type === null) { - return false; - } - - // Check it is the union type - return $param->type instanceof UnionType; - } -} diff --git a/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeReturnDeclarationRector.php b/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeReturnDeclarationRector.php deleted file mode 100644 index f2ce5463446..00000000000 --- a/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeReturnDeclarationRector.php +++ /dev/null @@ -1,71 +0,0 @@ -returnType === null) { - return false; - } - - // Check it is the union type - return $functionLike->returnType instanceof UnionType; - } -} diff --git a/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeParamMixedTypeDeclarationRector/DowngradeParamMixedTypeDeclarationRectorTest.php b/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeMixedTypeDeclarationRector/DowngradeMixedTypeDeclarationRectorTest.php similarity index 67% rename from rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeParamMixedTypeDeclarationRector/DowngradeParamMixedTypeDeclarationRectorTest.php rename to rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeMixedTypeDeclarationRector/DowngradeMixedTypeDeclarationRectorTest.php index c5e21036092..6cf3c082daf 100644 --- a/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeParamMixedTypeDeclarationRector/DowngradeParamMixedTypeDeclarationRectorTest.php +++ b/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeMixedTypeDeclarationRector/DowngradeMixedTypeDeclarationRectorTest.php @@ -2,14 +2,14 @@ declare(strict_types=1); -namespace Rector\DowngradePhp80\Tests\Rector\FunctionLike\DowngradeParamMixedTypeDeclarationRector; +namespace Rector\DowngradePhp80\Tests\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector; use Iterator; -use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeParamMixedTypeDeclarationRector; +use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector; use Rector\Testing\PHPUnit\AbstractRectorTestCase; use Symplify\SmartFileSystem\SmartFileInfo; -final class DowngradeParamMixedTypeDeclarationRectorTest extends AbstractRectorTestCase +final class DowngradeMixedTypeDeclarationRectorTest extends AbstractRectorTestCase { /** * @requires PHP 8.0 @@ -27,6 +27,6 @@ final class DowngradeParamMixedTypeDeclarationRectorTest extends AbstractRectorT protected function getRectorClass(): string { - return DowngradeParamMixedTypeDeclarationRector::class; + return DowngradeMixedTypeDeclarationRector::class; } } diff --git a/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeParamMixedTypeDeclarationRector/Fixture/docblock_exists.php.inc b/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeMixedTypeDeclarationRector/Fixture/docblock_exists.php.inc similarity index 84% rename from rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeParamMixedTypeDeclarationRector/Fixture/docblock_exists.php.inc rename to rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeMixedTypeDeclarationRector/Fixture/docblock_exists.php.inc index fc25ff2d0ae..15474e0edfd 100644 --- a/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeParamMixedTypeDeclarationRector/Fixture/docblock_exists.php.inc +++ b/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeMixedTypeDeclarationRector/Fixture/docblock_exists.php.inc @@ -1,6 +1,6 @@ doTestFileInfo($fileInfo); - } - - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - protected function getRectorClass(): string - { - return DowngradeReturnStaticTypeDeclarationRector::class; - } -} diff --git a/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/Source/AnotherClass.php b/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/Source/AnotherClass.php deleted file mode 100644 index f15b03e253c..00000000000 --- a/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/Source/AnotherClass.php +++ /dev/null @@ -1,10 +0,0 @@ -doTestFileInfo($fileInfo); - } - - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - protected function getRectorClass(): string - { - return DowngradeUnionTypeReturnDeclarationRector::class; - } -} diff --git a/utils/phpstan-extensions/config/rector-rules.neon b/utils/phpstan-extensions/config/rector-rules.neon index a2e55490ac4..8124350152b 100644 --- a/utils/phpstan-extensions/config/rector-rules.neon +++ b/utils/phpstan-extensions/config/rector-rules.neon @@ -4,7 +4,6 @@ services: tags: [phpstan.rules.rule] arguments: allowedParentTypes: - - Rector\Core\Rector\AbstractRector - Rector\Utils\DoctrineAnnotationParserSyncer\FileSyncer\AbstractClassSyncer - PhpParser\NodeAbstract - PHPStan\Type\BooleanType @@ -15,6 +14,23 @@ services: # value objects - Rector\Defluent\ValueObject\AbstractRootExpr - Symplify\SetConfigResolver\Provider\AbstractSetProvider + allowedDirectParentTypes: + - Rector\Core\Rector\AbstractRector + - Rector\Core\Rector\AbstractTemporaryRector + - Rector\PhpSpecToPHPUnit\Rector\AbstractPhpSpecToPHPUnitRector + # solve later + - Rector\Nette\Rector\FuncCall\AbstractPregToNetteUtilsStringsRector + - Rector\Nette\Rector\Identical\AbstractWithFunctionToNetteUtilsStringsRector + - Rector\Order\Rector\AbstractConstantPropertyMethodOrderRector + - Rector\Php72\Rector\FuncCall\AbstractConvertToAnonymousFunctionRector + - Rector\TypeDeclaration\Rector\FunctionLike\AbstractTypeDeclarationRector + - Rector\Transform\Rector\AbstractToMethodCallRector + - Rector\Symfony3\Rector\MethodCall\AbstractFormAddRector + - Rector\Symfony\Rector\MethodCall\AbstractToConstructorInjectionRector + - Rector\NetteCodeQuality\Rector\ArrayDimFetch\AbstractArrayDimFetchToAnnotatedControlVariableRector + - Rector\DowngradePhp74\Rector\Property\AbstractDowngradeTypedPropertyRector + - Rector\Defluent\Rector\AbstractFluentChainMethodCallRector + - Rector\NetteKdyby\Rector\ClassMethod\AbstractKdybyEventSubscriberRector - class: Symplify\PHPStanRules\Rules\ServiceAndValueObjectHaveSameStartsRule