rector/packages/Renaming/src/Rector/Annotation/RenameAnnotationRector.php

108 lines
2.6 KiB
PHP
Raw Normal View History

2018-02-09 00:14:45 +00:00
<?php declare(strict_types=1);
namespace Rector\Renaming\Rector\Annotation;
2018-02-09 00:14:45 +00:00
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
2018-02-09 00:14:45 +00:00
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use Rector\NodeTypeResolver\Node\AttributeKey;
2018-02-09 00:14:45 +00:00
use Rector\Rector\AbstractPHPUnitRector;
2018-08-01 19:52:44 +00:00
use Rector\RectorDefinition\ConfiguredCodeSample;
2018-04-08 11:51:26 +00:00
use Rector\RectorDefinition\RectorDefinition;
2018-02-09 00:14:45 +00:00
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Tests\Rector\Annotation\RenameAnnotationRector\RenameAnnotationRectorTest
*/
final class RenameAnnotationRector extends AbstractPHPUnitRector
2018-02-09 00:14:45 +00:00
{
/**
* @var string[][]
*/
2018-02-09 00:16:54 +00:00
private $classToAnnotationMap = [];
2018-02-09 00:14:45 +00:00
/**
* @param string[][] $classToAnnotationMap
*/
public function __construct(array $classToAnnotationMap = [])
2018-10-14 10:48:21 +00:00
{
2018-02-09 00:14:45 +00:00
$this->classToAnnotationMap = $classToAnnotationMap;
}
2018-04-08 11:51:26 +00:00
public function getDefinition(): RectorDefinition
{
2018-04-08 14:05:11 +00:00
return new RectorDefinition(
'Turns defined annotations above properties and methods to their new values.',
2018-04-08 14:05:11 +00:00
[
2018-08-01 19:52:44 +00:00
new ConfiguredCodeSample(
2019-09-18 06:14:35 +00:00
<<<'PHP'
2018-08-01 19:52:44 +00:00
class SomeTest extends PHPUnit\Framework\TestCase
{
/**
* @test
*/
2018-08-01 19:52:44 +00:00
public function someMethod()
{
}
}
2019-09-18 06:14:35 +00:00
PHP
2018-04-29 17:25:44 +00:00
,
2019-09-18 06:14:35 +00:00
<<<'PHP'
2018-08-01 19:52:44 +00:00
class SomeTest extends PHPUnit\Framework\TestCase
{
/**
* @scenario
*/
2018-08-01 19:52:44 +00:00
public function someMethod()
{
}
}
2019-09-18 06:14:35 +00:00
PHP
2018-08-01 19:52:44 +00:00
,
[
2019-09-04 12:10:29 +00:00
'$classToAnnotationMap' => [
'PHPUnit\Framework\TestCase' => [
'test' => 'scenario',
],
2018-08-01 19:52:44 +00:00
],
]
2018-04-08 14:05:11 +00:00
),
]
);
2018-04-08 11:51:26 +00:00
}
2018-08-14 22:12:41 +00:00
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [ClassMethod::class, Property::class];
}
/**
* @param ClassMethod|Property $node
*/
public function refactor(Node $node): ?Node
2018-02-09 00:14:45 +00:00
{
2019-09-04 12:10:29 +00:00
/** @var Class_ $class */
$class = $node->getAttribute(AttributeKey::CLASS_NODE);
2019-01-25 00:49:26 +00:00
2018-02-09 00:14:45 +00:00
foreach ($this->classToAnnotationMap as $type => $annotationMap) {
2019-09-04 12:10:29 +00:00
/** @var string $type */
if (! $this->isObjectType($class, $type)) {
2018-02-09 00:14:45 +00:00
continue;
}
2019-09-04 12:10:29 +00:00
foreach ($annotationMap as $oldAnnotation => $newAnnotation) {
if (! $this->docBlockManipulator->hasTag($node, $oldAnnotation)) {
continue;
}
2018-02-09 09:27:48 +00:00
2019-09-04 12:10:29 +00:00
$this->docBlockManipulator->replaceAnnotationInNode($node, $oldAnnotation, $newAnnotation);
2018-02-09 09:27:48 +00:00
}
2018-02-09 00:14:45 +00:00
}
2018-08-14 22:12:41 +00:00
2018-02-09 09:27:48 +00:00
return $node;
}
2018-02-09 00:14:45 +00:00
}