docBlockTagReplacer = $docBlockTagReplacer;
$this->docBlockUpdater = $docBlockUpdater;
$this->phpDocInfoFactory = $phpDocInfoFactory;
}
public function getRuleDefinition() : RuleDefinition
{
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
{
/**
* @test
*/
public function someMethod()
{
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
/**
* @scenario
*/
public function someMethod()
{
}
}
CODE_SAMPLE
, [new RenameAnnotationByType('PHPUnit\\Framework\\TestCase', 'test', 'scenario')])]);
}
/**
* @return array>
*/
public function getNodeTypes() : array
{
return [Class_::class, Expression::class];
}
/**
* @param Class_|Expression $node
*/
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;
}
/**
* @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;
}
}