2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-02-09 00:14:45 +00:00
|
|
|
|
2019-09-23 14:58:54 +00:00
|
|
|
namespace Rector\Renaming\Rector\Annotation;
|
2018-02-09 00:14:45 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
2019-09-06 10:30:58 +00:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2018-02-09 00:14:45 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
2020-02-04 07:54:00 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractPHPUnitRector;
|
|
|
|
use Rector\Core\RectorDefinition\ConfiguredCodeSample;
|
|
|
|
use Rector\Core\RectorDefinition\RectorDefinition;
|
2019-04-13 09:20:27 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2018-02-09 00:14:45 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2020-02-06 21:48:18 +00:00
|
|
|
* @see \Rector\Core\Tests\Rector\Annotation\RenameAnnotationRector\RenameAnnotationRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2019-03-09 13:15:14 +00:00
|
|
|
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
|
|
|
|
*/
|
2019-09-19 15:29:30 +00:00
|
|
|
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(
|
2018-08-01 13:34:55 +00:00
|
|
|
'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
|
|
|
|
{
|
2018-08-04 07:33:54 +00:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
{
|
2020-02-04 07:54:00 +00:00
|
|
|
/**
|
2018-08-04 07:33:54 +00:00
|
|
|
* @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
|
2018-08-14 21:33:39 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2020-02-04 07:54:00 +00:00
|
|
|
/** @var PhpDocInfo $phpDocInfo */
|
|
|
|
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
|
|
|
|
|
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) {
|
2020-02-04 07:54:00 +00:00
|
|
|
if (! $phpDocInfo->hasByName($oldAnnotation)) {
|
2019-09-04 12:10:29 +00:00
|
|
|
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
|
|
|
}
|