rector/src/Rector/Annotation/AnnotationReplacerRector.php

143 lines
3.5 KiB
PHP
Raw Normal View History

2018-02-09 00:14:45 +00:00
<?php declare(strict_types=1);
namespace Rector\Rector\Annotation;
2018-02-09 00:14:45 +00:00
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
2019-01-25 00:49:26 +00:00
use Rector\Exception\ShouldNotHappenException;
2018-08-14 11:43:32 +00:00
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
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
final class AnnotationReplacerRector extends AbstractPHPUnitRector
{
/**
* @var string[][]
*/
2018-02-09 00:16:54 +00:00
private $classToAnnotationMap = [];
2018-02-09 00:14:45 +00:00
/**
2018-10-31 11:53:59 +00:00
* @var string[]
2018-02-09 00:14:45 +00:00
*/
2018-10-31 11:53:59 +00:00
private $activeAnnotationMap = [];
2018-02-09 00:14:45 +00:00
2018-02-09 09:27:48 +00:00
/**
* @var DocBlockManipulator
2018-02-09 09:27:48 +00:00
*/
private $docBlockManipulator;
2018-02-09 09:27:48 +00:00
2018-02-09 00:14:45 +00:00
/**
* @param string[][] $classToAnnotationMap
*/
public function __construct(array $classToAnnotationMap, DocBlockManipulator $docBlockManipulator)
2018-10-14 10:48:21 +00:00
{
$this->docBlockManipulator = $docBlockManipulator;
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(
2018-04-29 17:25:44 +00:00
<<<'CODE_SAMPLE'
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()
{
}
}
2018-04-29 17:25:44 +00:00
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
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()
{
}
}
2018-04-29 17:25:44 +00:00
CODE_SAMPLE
2018-08-01 19:52:44 +00:00
,
[
'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
{
2018-02-09 09:27:48 +00:00
if ($this->shouldSkip($node)) {
return null;
2018-02-09 00:14:45 +00:00
}
2018-08-14 22:12:41 +00:00
2018-08-14 11:43:32 +00:00
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
2019-01-25 00:49:26 +00:00
if ($parentNode === null) {
throw new ShouldNotHappenException();
}
2018-10-14 10:48:21 +00:00
$parentNodeTypes = $this->getTypes($parentNode);
2018-02-09 00:14:45 +00:00
foreach ($this->classToAnnotationMap as $type => $annotationMap) {
if (! in_array($type, $parentNodeTypes, true)) {
2018-02-09 00:14:45 +00:00
continue;
}
2018-02-09 09:27:48 +00:00
$this->activeAnnotationMap = $annotationMap;
2018-08-14 22:12:41 +00:00
if (! $this->hasAnyAnnotation($node)) {
return null;
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
foreach ($this->activeAnnotationMap as $oldAnnotation => $newAnnotation) {
$this->docBlockManipulator->replaceAnnotationInNode($node, $oldAnnotation, $newAnnotation);
2018-02-09 09:27:48 +00:00
}
2018-02-09 00:14:45 +00:00
2018-02-09 09:27:48 +00:00
return $node;
}
private function shouldSkip(Node $node): bool
{
if (! $node instanceof ClassMethod && ! $node instanceof Property) {
return true;
}
return $node->getAttribute(Attribute::PARENT_NODE) === null;
2018-02-09 09:27:48 +00:00
}
private function hasAnyAnnotation(Node $node): bool
{
foreach (array_keys($this->activeAnnotationMap) as $oldAnnotation) {
if ($this->docBlockManipulator->hasTag($node, $oldAnnotation)) {
2018-02-09 09:27:48 +00:00
return true;
}
}
return false;
2018-02-09 00:14:45 +00:00
}
}