rector/rules/Renaming/Rector/ClassMethod/RenameAnnotationRector.php

147 lines
4.9 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare (strict_types=1);
namespace Rector\Renaming\Rector\ClassMethod;
2018-02-09 00:14:45 +00:00
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\Contract\Rector\ConfigurableRectorInterface;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockTagReplacer;
use Rector\Rector\AbstractRector;
use Rector\Renaming\Contract\RenameAnnotationInterface;
use Rector\Renaming\ValueObject\RenameAnnotationByType;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202403\Webmozart\Assert\Assert;
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Tests\Renaming\Rector\ClassMethod\RenameAnnotationRector\RenameAnnotationRectorTest
2019-09-03 09:11:45 +00:00
*/
final class RenameAnnotationRector extends AbstractRector implements ConfigurableRectorInterface
2018-02-09 00:14:45 +00:00
{
/**
* @readonly
* @var \Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockTagReplacer
*/
private $docBlockTagReplacer;
/**
* @readonly
* @var \Rector\Comments\NodeDocBlock\DocBlockUpdater
*/
private $docBlockUpdater;
/**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
*/
private $phpDocInfoFactory;
/**
* @var RenameAnnotationInterface[]
*/
private $renameAnnotations = [];
public function __construct(DocBlockTagReplacer $docBlockTagReplacer, DocBlockUpdater $docBlockUpdater, PhpDocInfoFactory $phpDocInfoFactory)
{
$this->docBlockTagReplacer = $docBlockTagReplacer;
$this->docBlockUpdater = $docBlockUpdater;
$this->phpDocInfoFactory = $phpDocInfoFactory;
}
public function getRuleDefinition() : RuleDefinition
2018-04-08 11:51:26 +00:00
{
return new RuleDefinition('Turns defined annotations above properties and methods to their new values.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
2018-08-01 19:52:44 +00:00
{
/**
* @test
*/
2018-08-01 19:52:44 +00:00
public function someMethod()
{
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
2018-08-01 19:52:44 +00:00
{
2020-02-04 07:54:00 +00:00
/**
* @scenario
*/
2018-08-01 19:52:44 +00:00
public function someMethod()
{
}
}
CODE_SAMPLE
, [new RenameAnnotationByType('PHPUnit\\Framework\\TestCase', 'test', 'scenario')])]);
2018-04-08 11:51:26 +00:00
}
2018-08-14 22:12:41 +00:00
/**
* @return array<class-string<Node>>
2018-08-14 22:12:41 +00:00
*/
public function getNodeTypes() : array
{
return [Class_::class, Expression::class];
}
/**
* @param Class_|Expression $node
*/
public function refactor(Node $node) : ?Node
2018-02-09 00:14:45 +00:00
{
if ($node instanceof Expression) {
return $this->refactorExpression($node);
}
$hasChanged = \false;
foreach ($node->stmts as $stmt) {
if (!$stmt instanceof ClassMethod && !$stmt instanceof Property) {
continue;
2018-02-09 09:27:48 +00:00
}
$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;
2018-02-09 00:14:45 +00:00
}
2018-02-09 09:27:48 +00:00
return $node;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
2020-07-29 13:55:33 +00:00
{
Assert::allIsAOf($configuration, RenameAnnotationInterface::class);
$this->renameAnnotations = $configuration;
2020-07-29 13:55:33 +00:00
}
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;
}
2018-02-09 00:14:45 +00:00
}