From c0a9a351f7f1eba6db49ea70b6cab73baa50723b Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 11 Aug 2026 00:07:03 +0200 Subject: [PATCH] [deprecation] Deprecate RemoveAnnotationRector --- phpstan.neon | 1 + .../Fixture/fixture.php.inc | 22 ------- .../Fixture/remove_by_type.php.inc | 35 ----------- .../RemoveAnnotationRectorTest.php | 28 --------- .../config/configured_rule.php | 11 ---- .../ClassLike/RemoveAnnotationRector.php | 63 +++---------------- .../PhpDocManipulator/PhpDocTagRemover.php | 3 + 7 files changed, 12 insertions(+), 151 deletions(-) delete mode 100644 rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/Fixture/fixture.php.inc delete mode 100644 rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/Fixture/remove_by_type.php.inc delete mode 100644 rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/RemoveAnnotationRectorTest.php delete mode 100644 rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/config/configured_rule.php diff --git a/phpstan.neon b/phpstan.neon index 6ad28fedba5..ebd330b357f 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -470,6 +470,7 @@ parameters: - '#Class "Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnArrayDocblockBasedOnArrayMapRector" is missing @see annotation with test case class reference#' - '#Class "Rector\\Transform\\Rector\\FuncCall\\FuncCallToMethodCallRector" is missing @see annotation with test case class reference#' - '#Class "Rector\\Transform\\Rector\\Scalar\\ScalarValueToConstFetchRector" is missing @see annotation with test case class reference#' + - '#Class "Rector\\DeadCode\\Rector\\ClassLike\\RemoveAnnotationRector" is missing @see annotation with test case class reference#' # @todo fix in phpstan-rules - diff --git a/rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/Fixture/fixture.php.inc b/rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/Fixture/fixture.php.inc deleted file mode 100644 index 2ad15301d8e..00000000000 --- a/rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,22 +0,0 @@ - ------ - diff --git a/rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/Fixture/remove_by_type.php.inc b/rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/Fixture/remove_by_type.php.inc deleted file mode 100644 index f9f4ca2ffb8..00000000000 --- a/rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/Fixture/remove_by_type.php.inc +++ /dev/null @@ -1,35 +0,0 @@ - ------ - diff --git a/rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/RemoveAnnotationRectorTest.php b/rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/RemoveAnnotationRectorTest.php deleted file mode 100644 index 3eb8efe4831..00000000000 --- a/rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/RemoveAnnotationRectorTest.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/DeadCode/Rector/ClassLike/RemoveAnnotationRector/config/configured_rule.php b/rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/config/configured_rule.php deleted file mode 100644 index 6ac5e717858..00000000000 --- a/rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/config/configured_rule.php +++ /dev/null @@ -1,11 +0,0 @@ -ruleWithConfiguration(RemoveAnnotationRector::class, ['method', 'JMS\DiExtraBundle\Annotation\InjectParams']); -}; diff --git a/rules/DeadCode/Rector/ClassLike/RemoveAnnotationRector.php b/rules/DeadCode/Rector/ClassLike/RemoveAnnotationRector.php index 977d28c894c..7b487f2087a 100644 --- a/rules/DeadCode/Rector/ClassLike/RemoveAnnotationRector.php +++ b/rules/DeadCode/Rector/ClassLike/RemoveAnnotationRector.php @@ -9,34 +9,18 @@ use PhpParser\Node\Stmt\ClassConst; use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\Property; -use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode; -use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; -use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; -use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover; -use Rector\Comments\NodeDocBlock\DocBlockUpdater; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; use Rector\Contract\Rector\ConfigurableRectorInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; -use Webmozart\Assert\Assert; /** - * @see \Rector\Tests\DeadCode\Rector\ClassLike\RemoveAnnotationRector\RemoveAnnotationRectorTest + * @deprecated This rule is deprecated, as removing an annotation by name is a coding standard concern, not an upgrade path. It is not part of any set and a coding standard tool handles it better. */ -final class RemoveAnnotationRector extends AbstractRector implements ConfigurableRectorInterface +final class RemoveAnnotationRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface { - /** - * @var string[] - */ - private array $annotationsToRemove = []; - - public function __construct( - private readonly PhpDocTagRemover $phpDocTagRemover, - private readonly DocBlockUpdater $docBlockUpdater, - private readonly PhpDocInfoFactory $phpDocInfoFactory, - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Remove annotation by names', [ @@ -74,38 +58,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - Assert::notEmpty($this->annotationsToRemove); - - $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); - if (! $phpDocInfo instanceof PhpDocInfo) { - return null; - } - - $hasChanged = false; - - foreach ($this->annotationsToRemove as $annotationToRemove) { - $namedHasChanged = $this->phpDocTagRemover->removeByName($phpDocInfo, $annotationToRemove); - if ($namedHasChanged) { - $hasChanged = true; - } - - if (! is_a($annotationToRemove, PhpDocTagValueNode::class, true)) { - continue; - } - - $typedHasChanged = $phpDocInfo->removeByType($annotationToRemove); - if ($typedHasChanged) { - $hasChanged = true; - } - } - - if ($hasChanged) { - $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); - - return $node; - } - - return null; + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated, as removing an annotation by name is a coding standard concern, not an upgrade path; use a coding standard tool instead', + self::class + )); } /** @@ -113,8 +69,5 @@ public function refactor(Node $node): ?Node */ public function configure(array $configuration): void { - Assert::allString($configuration); - - $this->annotationsToRemove = $configuration; } } diff --git a/src/BetterPhpDocParser/PhpDocManipulator/PhpDocTagRemover.php b/src/BetterPhpDocParser/PhpDocManipulator/PhpDocTagRemover.php index d808b41ca95..52dd2f478f8 100644 --- a/src/BetterPhpDocParser/PhpDocManipulator/PhpDocTagRemover.php +++ b/src/BetterPhpDocParser/PhpDocManipulator/PhpDocTagRemover.php @@ -12,6 +12,9 @@ final class PhpDocTagRemover { + /** + * @api phpunit + */ public function removeByName(PhpDocInfo $phpDocInfo, string $name): bool { $hasChanged = false;