Skip to content

Commit d2a983e

Browse files
authored
[deprecation] Deprecate RemoveAnnotationRector (#8345)
1 parent 75db75e commit d2a983e

7 files changed

Lines changed: 12 additions & 151 deletions

File tree

‎phpstan.neon‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ parameters:
470470
- '#Class "Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnArrayDocblockBasedOnArrayMapRector" is missing @see annotation with test case class reference#'
471471
- '#Class "Rector\\Transform\\Rector\\FuncCall\\FuncCallToMethodCallRector" is missing @see annotation with test case class reference#'
472472
- '#Class "Rector\\Transform\\Rector\\Scalar\\ScalarValueToConstFetchRector" is missing @see annotation with test case class reference#'
473+
- '#Class "Rector\\DeadCode\\Rector\\ClassLike\\RemoveAnnotationRector" is missing @see annotation with test case class reference#'
473474

474475
# @todo fix in phpstan-rules
475476
-

‎rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/Fixture/fixture.php.inc‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/Fixture/remove_by_type.php.inc‎

Lines changed: 0 additions & 35 deletions
This file was deleted.

‎rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/RemoveAnnotationRectorTest.php‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎rules-tests/DeadCode/Rector/ClassLike/RemoveAnnotationRector/config/configured_rule.php‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎rules/DeadCode/Rector/ClassLike/RemoveAnnotationRector.php‎

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,18 @@
99
use PhpParser\Node\Stmt\ClassConst;
1010
use PhpParser\Node\Stmt\ClassLike;
1111
use PhpParser\Node\Stmt\Property;
12-
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
13-
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
14-
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
15-
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
16-
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
12+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
1713
use Rector\Contract\Rector\ConfigurableRectorInterface;
14+
use Rector\Exception\ShouldNotHappenException;
1815
use Rector\Rector\AbstractRector;
1916
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
2017
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
21-
use Webmozart\Assert\Assert;
2218

2319
/**
24-
* @see \Rector\Tests\DeadCode\Rector\ClassLike\RemoveAnnotationRector\RemoveAnnotationRectorTest
20+
* @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.
2521
*/
26-
final class RemoveAnnotationRector extends AbstractRector implements ConfigurableRectorInterface
22+
final class RemoveAnnotationRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
2723
{
28-
/**
29-
* @var string[]
30-
*/
31-
private array $annotationsToRemove = [];
32-
33-
public function __construct(
34-
private readonly PhpDocTagRemover $phpDocTagRemover,
35-
private readonly DocBlockUpdater $docBlockUpdater,
36-
private readonly PhpDocInfoFactory $phpDocInfoFactory,
37-
) {
38-
}
39-
4024
public function getRuleDefinition(): RuleDefinition
4125
{
4226
return new RuleDefinition('Remove annotation by names', [
@@ -74,47 +58,16 @@ public function getNodeTypes(): array
7458
*/
7559
public function refactor(Node $node): ?Node
7660
{
77-
Assert::notEmpty($this->annotationsToRemove);
78-
79-
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
80-
if (! $phpDocInfo instanceof PhpDocInfo) {
81-
return null;
82-
}
83-
84-
$hasChanged = false;
85-
86-
foreach ($this->annotationsToRemove as $annotationToRemove) {
87-
$namedHasChanged = $this->phpDocTagRemover->removeByName($phpDocInfo, $annotationToRemove);
88-
if ($namedHasChanged) {
89-
$hasChanged = true;
90-
}
91-
92-
if (! is_a($annotationToRemove, PhpDocTagValueNode::class, true)) {
93-
continue;
94-
}
95-
96-
$typedHasChanged = $phpDocInfo->removeByType($annotationToRemove);
97-
if ($typedHasChanged) {
98-
$hasChanged = true;
99-
}
100-
}
101-
102-
if ($hasChanged) {
103-
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
104-
105-
return $node;
106-
}
107-
108-
return null;
61+
throw new ShouldNotHappenException(sprintf(
62+
'"%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',
63+
self::class
64+
));
10965
}
11066

11167
/**
11268
* @param mixed[] $configuration
11369
*/
11470
public function configure(array $configuration): void
11571
{
116-
Assert::allString($configuration);
117-
118-
$this->annotationsToRemove = $configuration;
11972
}
12073
}

‎src/BetterPhpDocParser/PhpDocManipulator/PhpDocTagRemover.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
final class PhpDocTagRemover
1414
{
15+
/**
16+
* @api phpunit
17+
*/
1518
public function removeByName(PhpDocInfo $phpDocInfo, string $name): bool
1619
{
1720
$hasChanged = false;

0 commit comments

Comments
 (0)