Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
-
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

63 changes: 8 additions & 55 deletions rules/DeadCode/Rector/ClassLike/RemoveAnnotationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down Expand Up @@ -74,47 +58,16 @@ 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
));
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allString($configuration);

$this->annotationsToRemove = $configuration;
}
}
3 changes: 3 additions & 0 deletions src/BetterPhpDocParser/PhpDocManipulator/PhpDocTagRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

final class PhpDocTagRemover
{
/**
* @api phpunit
*/
public function removeByName(PhpDocInfo $phpDocInfo, string $name): bool
{
$hasChanged = false;
Expand Down
Loading