From 656836093ea0abd96860af062fdf6f67afe1f504 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 14 May 2023 17:53:35 +0200 Subject: [PATCH] [CodeQuality] Deprecate NarrowUnionTypeDocRector as focused on docblocks, and moving to type declarations (#3848) --- config/set/code-quality.php | 2 - .../TypeAnalyzer/UnionTypeAnalyzer.php | 62 -------------- phpstan.neon | 1 + .../Fixture/fixture.php.inc | 29 ------- .../Fixture/param_scalar.php.inc | 29 ------- .../Fixture/skip_constant_union.php.inc | 20 ----- .../Fixture/skip_literal_union.php.inc | 15 ---- .../Fixture/skip_non_union.php.inc | 15 ---- .../Fixture/skip_overlapped_key_param.php.inc | 14 ---- .../Fixture/skip_scalar_partial.php.inc | 14 ---- .../NarrowUnionTypeDocRectorTest.php | 28 ------- .../config/configured_rule.php | 10 --- .../ClassMethod/NarrowUnionTypeDocRector.php | 81 ++----------------- .../Rector/FunctionLike/UnionTypesRector.php | 2 +- .../NodeManipulator/VisibilityManipulator.php | 3 + utils/Command/MissingInSetCommand.php | 3 + 16 files changed, 15 insertions(+), 313 deletions(-) delete mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/fixture.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/param_scalar.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_constant_union.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_literal_union.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_non_union.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_overlapped_key_param.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_scalar_partial.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/NarrowUnionTypeDocRectorTest.php delete mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/config/configured_rule.php diff --git a/config/set/code-quality.php b/config/set/code-quality.php index d66506c06c3..e304e3d2a9d 100644 --- a/config/set/code-quality.php +++ b/config/set/code-quality.php @@ -11,7 +11,6 @@ use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector; use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector; use Rector\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector; -use Rector\CodeQuality\Rector\ClassMethod\NarrowUnionTypeDocRector; use Rector\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector; use Rector\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector; use Rector\CodeQuality\Rector\Concat\JoinStringConcatRector; @@ -179,7 +178,6 @@ SwitchNegatedTernaryRector::class, SingularSwitchToIfRector::class, SimplifyIfNullableReturnRector::class, - NarrowUnionTypeDocRector::class, FuncGetArgsToVariadicParamRector::class, CallUserFuncToMethodCallRector::class, CallUserFuncWithArrowFunctionToInlineRector::class, diff --git a/packages/PHPStanStaticTypeMapper/TypeAnalyzer/UnionTypeAnalyzer.php b/packages/PHPStanStaticTypeMapper/TypeAnalyzer/UnionTypeAnalyzer.php index 54e5d877ab3..ab85b22dbef 100644 --- a/packages/PHPStanStaticTypeMapper/TypeAnalyzer/UnionTypeAnalyzer.php +++ b/packages/PHPStanStaticTypeMapper/TypeAnalyzer/UnionTypeAnalyzer.php @@ -5,15 +5,12 @@ namespace Rector\PHPStanStaticTypeMapper\TypeAnalyzer; use PHPStan\Type\ArrayType; -use PHPStan\Type\Constant\ConstantStringType; use PHPStan\Type\IterableType; use PHPStan\Type\NullType; use PHPStan\Type\ObjectType; -use PHPStan\Type\ObjectWithoutClassType; use PHPStan\Type\TypeWithClassName; use PHPStan\Type\UnionType; use Rector\PHPStanStaticTypeMapper\ValueObject\UnionTypeAnalysis; -use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; use Traversable; final class UnionTypeAnalyzer @@ -69,65 +66,6 @@ public function matchExclusiveTypesWithClassNames(UnionType $unionType): array return $typesWithClassNames; } - public function hasObjectWithoutClassType(UnionType $unionType): bool - { - $types = $unionType->getTypes(); - foreach ($types as $type) { - if ($type instanceof ObjectWithoutClassType) { - return true; - } - } - - return false; - } - - public function hasObjectWithoutClassTypeWithOnlyFullyQualifiedObjectType(UnionType $unionType): bool - { - $types = $unionType->getTypes(); - foreach ($types as $type) { - if ($type instanceof ObjectWithoutClassType) { - continue; - } - - if (! $type instanceof FullyQualifiedObjectType) { - return false; - } - } - - return true; - } - - public function isScalar(UnionType $unionType): bool - { - $types = $unionType->getTypes(); - - if (count($types) !== 4) { - return false; - } - - foreach ($types as $type) { - if ($type->isString()->yes() && ! $type instanceof ConstantStringType) { - continue; - } - - if ($type->isFloat()->yes()) { - continue; - } - - if ($type->isInteger()->yes()) { - continue; - } - - if ($type->isBoolean()->yes()) { - continue; - } - - return false; - } - - return true; - } - public function isNullable(UnionType $unionType, bool $checkTwoTypes = false): bool { $types = $unionType->getTypes(); diff --git a/phpstan.neon b/phpstan.neon index 8f1e576a11d..3d6828d7590 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -797,3 +797,4 @@ parameters: # deprecated - '#Register "Rector\\Php80\\Rector\\FunctionLike\\UnionTypesRector" service to "php80\.php" config set#' + - '#Fetching class constant class of deprecated class Rector\\CodeQuality\\Rector\\ClassMethod\\NarrowUnionTypeDocRector#' diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/fixture.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/fixture.php.inc deleted file mode 100644 index 9ff814e4dee..00000000000 --- a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/param_scalar.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/param_scalar.php.inc deleted file mode 100644 index 8cf3d9c969c..00000000000 --- a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/param_scalar.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_constant_union.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_constant_union.php.inc deleted file mode 100644 index 9fbf4bb7ba4..00000000000 --- a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_constant_union.php.inc +++ /dev/null @@ -1,20 +0,0 @@ - diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_literal_union.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_literal_union.php.inc deleted file mode 100644 index 7c0f7a2f64f..00000000000 --- a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_literal_union.php.inc +++ /dev/null @@ -1,15 +0,0 @@ - diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_non_union.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_non_union.php.inc deleted file mode 100644 index 4404dd80713..00000000000 --- a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_non_union.php.inc +++ /dev/null @@ -1,15 +0,0 @@ - - diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_overlapped_key_param.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_overlapped_key_param.php.inc deleted file mode 100644 index 446ba7d1da0..00000000000 --- a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_overlapped_key_param.php.inc +++ /dev/null @@ -1,14 +0,0 @@ - diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/NarrowUnionTypeDocRectorTest.php b/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/NarrowUnionTypeDocRectorTest.php deleted file mode 100644 index 638dfe41b45..00000000000 --- a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/NarrowUnionTypeDocRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/config/configured_rule.php deleted file mode 100644 index 7db6eaba066..00000000000 --- a/rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rule(NarrowUnionTypeDocRector::class); -}; diff --git a/rules/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector.php b/rules/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector.php index d13f95714dd..9aa59e4b238 100644 --- a/rules/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector.php @@ -6,25 +6,15 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; -use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; -use PHPStan\Type\UnionType; -use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\Core\Rector\AbstractRector; -use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer; -use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Rector\Tests\CodeQuality\Rector\ClassMethod\NarrowUnionTypeDocRector\NarrowUnionTypeDocRectorTest + * @deprecated This split is deprecated as dangerous to rely on docblock strings. Instead, use strict type declaration rules. */ final class NarrowUnionTypeDocRector extends AbstractRector { - public function __construct( - private readonly UnionTypeAnalyzer $unionTypeAnalyzer - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Changes docblock by narrowing type', [ @@ -67,70 +57,13 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); - $params = $node->getParams(); - - $hasChanged = false; - - foreach ($params as $key => $param) { - /** @var string $paramName */ - $paramName = $this->getName($param->var); - $paramType = $phpDocInfo->getParamType($paramName); - - if (! $paramType instanceof UnionType) { - continue; - } - - if ($this->unionTypeAnalyzer->isScalar($paramType)) { - $this->changeDocObjectScalar($key, $phpDocInfo); - $hasChanged = true; - continue; - } - - if ($this->unionTypeAnalyzer->hasObjectWithoutClassType($paramType)) { - $this->changeDocObjectWithoutClassType($paramType, $key, $phpDocInfo); - $hasChanged = true; - } - } - - if ($hasChanged) { - return $node; - } + $errorMessage = sprintf( + 'Rule "%s" is deprecated, as we moved from docblocks for type declarations. Use strict type rules instead', + self::class + ); + trigger_error($errorMessage, E_USER_WARNING); + sleep(3); return null; } - - private function changeDocObjectWithoutClassType( - UnionType $unionType, - int $key, - PhpDocInfo $phpDocInfo - ): void { - if (! $this->unionTypeAnalyzer->hasObjectWithoutClassTypeWithOnlyFullyQualifiedObjectType($unionType)) { - return; - } - - $types = $unionType->getTypes(); - $resultType = ''; - foreach ($types as $type) { - if ($type instanceof FullyQualifiedObjectType) { - $resultType .= $type->getClassName() . '|'; - } - } - - $resultType = rtrim($resultType, '|'); - $paramTagValueNodes = $phpDocInfo->getParamTagValueNodes(); - - if (isset($paramTagValueNodes[$key])) { - $paramTagValueNodes[$key]->type = new IdentifierTypeNode($resultType); - } - } - - private function changeDocObjectScalar(int $key, PhpDocInfo $phpDocInfo): void - { - $paramTagValueNodes = $phpDocInfo->getParamTagValueNodes(); - - if (isset($paramTagValueNodes[$key])) { - $paramTagValueNodes[$key]->type = new IdentifierTypeNode('scalar'); - } - } } diff --git a/rules/Php80/Rector/FunctionLike/UnionTypesRector.php b/rules/Php80/Rector/FunctionLike/UnionTypesRector.php index d5da4fd0eaf..bfc10f2b81d 100644 --- a/rules/Php80/Rector/FunctionLike/UnionTypesRector.php +++ b/rules/Php80/Rector/FunctionLike/UnionTypesRector.php @@ -16,7 +16,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @deprecated This split is deprecated as dangerous to rely on doblock strings. Instead, use strict type declaration rules. + * @deprecated This split is deprecated as dangerous to rely on docblock strings. Instead, use strict type declaration rules. */ final class UnionTypesRector extends AbstractRector implements MinPhpVersionInterface { diff --git a/rules/Privatization/NodeManipulator/VisibilityManipulator.php b/rules/Privatization/NodeManipulator/VisibilityManipulator.php index e0b115ff819..2de7040a4e1 100644 --- a/rules/Privatization/NodeManipulator/VisibilityManipulator.php +++ b/rules/Privatization/NodeManipulator/VisibilityManipulator.php @@ -103,6 +103,9 @@ public function makePrivate(ClassMethod | Property | ClassConst $node): void $this->replaceVisibilityFlag($node, Visibility::PRIVATE); } + /** + * @api + */ public function removeFinal(Class_ | ClassConst $node): void { $node->flags -= Class_::MODIFIER_FINAL; diff --git a/utils/Command/MissingInSetCommand.php b/utils/Command/MissingInSetCommand.php index 0c84da14a0e..ca3dff24147 100644 --- a/utils/Command/MissingInSetCommand.php +++ b/utils/Command/MissingInSetCommand.php @@ -8,6 +8,7 @@ use Nette\Utils\FileSystem; use Nette\Utils\Strings; use Rector\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector; +use Rector\CodeQuality\Rector\ClassMethod\NarrowUnionTypeDocRector; use Rector\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector; use Rector\CodingStyle\Rector\Property\NullifyUnionNullableRector; use Rector\Core\Contract\Rector\ConfigurableRectorInterface; @@ -53,6 +54,8 @@ final class MissingInSetCommand extends Command FlipNegatedTernaryInstanceofRector::class, BinaryOpNullableToInstanceofRector::class, WhileNullableToInstanceofRector::class, + // deprecated + NarrowUnionTypeDocRector::class, ]; /**