rector/src/Rector/Annotation/AnnotationReplacerRector.php

155 lines
3.8 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;
2018-08-14 11:43:32 +00:00
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockAnalyzer;
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
/**
* @var DocBlockAnalyzer
*/
private $docBlockAnalyzer;
2018-02-09 09:27:48 +00:00
/**
* @var string[]
*/
private $activeAnnotationMap = [];
/**
* @var NodeTypeResolver
*/
private $nodeTypeResolver;
2018-02-09 00:14:45 +00:00
/**
* @param string[][] $classToAnnotationMap
*/
public function __construct(
array $classToAnnotationMap,
DocBlockAnalyzer $docBlockAnalyzer,
NodeTypeResolver $nodeTypeResolver
) {
2018-02-09 00:14:45 +00:00
$this->docBlockAnalyzer = $docBlockAnalyzer;
$this->classToAnnotationMap = $classToAnnotationMap;
$this->nodeTypeResolver = $nodeTypeResolver;
2018-02-09 00:14:45 +00:00
}
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
,
[
'$classToAnnotationMap' => [
'PHPUnit\Framework\TestCase' => [
2018-08-01 19:52:44 +00:00
'test' => 'scenario',
],
],
]
2018-04-08 14:05:11 +00:00
),
]
);
2018-04-08 11:51:26 +00:00
}
2018-02-09 00:14:45 +00:00
public function isCandidate(Node $node): bool
{
2018-02-09 09:27:48 +00:00
if ($this->shouldSkip($node)) {
2018-02-09 00:14:45 +00:00
return false;
}
/** @var Node $parentNode */
2018-08-14 11:43:32 +00:00
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
$parentNodeTypes = $this->nodeTypeResolver->resolve($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;
if ($this->hasAnyAnnotation($node)) {
return true;
}
2018-02-09 00:14:45 +00:00
}
return false;
}
/**
2018-02-09 09:27:48 +00:00
* @param ClassMethod|Property $node
2018-02-09 00:14:45 +00:00
*/
2018-02-09 09:27:48 +00:00
public function refactor(Node $node): ?Node
2018-02-09 00:14:45 +00:00
{
2018-02-09 09:27:48 +00:00
foreach ($this->activeAnnotationMap as $oldAnnotation => $newAnnotation) {
$this->docBlockAnalyzer->replaceAnnotationInNode($node, $oldAnnotation, $newAnnotation);
}
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;
}
/** @var Node|null $parentNode */
2018-08-14 11:43:32 +00:00
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
2018-02-09 09:27:48 +00:00
if (! $parentNode) {
return true;
}
return false;
}
private function hasAnyAnnotation(Node $node): bool
{
foreach ($this->activeAnnotationMap as $oldAnnotation => $newAnnotation) {
if ($this->docBlockAnalyzer->hasTag($node, $oldAnnotation)) {
2018-02-09 09:27:48 +00:00
return true;
}
}
return false;
2018-02-09 00:14:45 +00:00
}
}