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
2 changes: 1 addition & 1 deletion .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
-
name: 'Active Classes'
run: |
vendor/bin/class-leak check bin config src rules utils --skip-suffix "Rector" --skip-type="Rector\\Utils\\Compiler\\Unprefixer" --skip-type="Rector\\NodeCollector\\BinaryOpConditionsCollector" --skip-type="Rector\\DependencyInjection\\PHPStan\\RichParserFactory" --skip-path="Source"
vendor/bin/class-leak check bin config src rules utils --skip-suffix "Rector" --skip-type="Rector\\Utils\\Compiler\\Unprefixer" --skip-type="Rector\\NodeCollector\\BinaryOpConditionsCollector" --skip-type="Rector\\DependencyInjection\\PHPStan\\RichParserFactory" --skip-type="Rector\\NodeTypeResolver\\PhpDoc\\NodeAnalyzer\\DocBlockTagReplacer" --skip-path="Source"

-
name: 'Compatible PHPStan versions'
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 3 additions & 0 deletions rules/Renaming/Contract/RenameAnnotationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Rector\Renaming\Contract;

/**
* @api used in deprecated RenameAnnotationRector configs
*/
interface RenameAnnotationInterface
{
public function getOldAnnotation(): string;
Expand Down
99 changes: 8 additions & 91 deletions rules/Renaming/Rector/ClassMethod/RenameAnnotationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,20 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockTagReplacer;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\Renaming\Contract\RenameAnnotationInterface;
use Rector\Renaming\ValueObject\RenameAnnotationByType;
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
use Rector\RuleDoc\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Renaming\Rector\ClassMethod\RenameAnnotationRector\RenameAnnotationRectorTest
* @deprecated This rule is deprecated, as too niche, never used and needs project context. Write a custom rule for the exact project case instead.
*/
final class RenameAnnotationRector extends AbstractRector implements ConfigurableRectorInterface
final class RenameAnnotationRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
{
/**
* @var RenameAnnotationInterface[]
*/
private array $renameAnnotations = [];

public function __construct(
private readonly DocBlockTagReplacer $docBlockTagReplacer,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -91,81 +73,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof Expression) {
return $this->refactorExpression($node);
}

$hasChanged = false;
foreach ($node->stmts as $stmt) {
if (! $stmt instanceof ClassMethod && ! $stmt instanceof Property) {
continue;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNode($stmt);
if (! $phpDocInfo instanceof PhpDocInfo) {
continue;
}

foreach ($this->renameAnnotations as $renameAnnotation) {
if ($renameAnnotation instanceof RenameAnnotationByType && ! $this->isObjectType(
$node,
$renameAnnotation->getObjectType()
)) {
continue;
}

$hasDocBlockChanged = $this->docBlockTagReplacer->replaceTagByAnother(
$phpDocInfo,
$renameAnnotation->getOldAnnotation(),
$renameAnnotation->getNewAnnotation()
);

if ($hasDocBlockChanged) {
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($stmt);
$hasChanged = true;
}
}
}

if (! $hasChanged) {
return null;
}

return $node;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as too niche and part of no set; write a custom rule for the exact project case instead',
self::class
));
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, RenameAnnotationInterface::class);
$this->renameAnnotations = $configuration;
}

private function refactorExpression(Expression $expression): ?Expression
{
$hasChanged = false;

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($expression);

foreach ($this->renameAnnotations as $renameAnnotation) {
$hasDocBlockChanged = $this->docBlockTagReplacer->replaceTagByAnother(
$phpDocInfo,
$renameAnnotation->getOldAnnotation(),
$renameAnnotation->getNewAnnotation()
);

if ($hasDocBlockChanged) {
$hasChanged = true;
}
}

if ($hasChanged) {
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($expression);
return $expression;
}

return null;
}
}
3 changes: 3 additions & 0 deletions rules/Renaming/ValueObject/RenameAnnotationByType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Rector\Renaming\Contract\RenameAnnotationInterface;
use Rector\Validation\RectorAssert;

/**
* @api used in deprecated RenameAnnotationRector configs
*/
final readonly class RenameAnnotationByType implements RenameAnnotationInterface
{
public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Rector\BetterPhpDocParser\Annotation\AnnotationNaming;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;

/**
* @api used by extension packages to rename doc tags
*/
final readonly class DocBlockTagReplacer
{
public function __construct(
Expand Down
Loading