improve getByType()

This commit is contained in:
Tomas Votruba 2019-09-11 20:09:16 +02:00
parent 4dcdf4c890
commit 6f983ad302
5 changed files with 31 additions and 31 deletions

View File

@ -3,10 +3,10 @@
namespace Rector\DeadCode\Doctrine;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\InheritanceType;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Class_\EntityTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Class_\InheritanceTypeTagValueNode;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\DoctrineRelationTagValueNodeInterface;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\InversedByNodeInterface;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\MappedByNodeInterface;
@ -71,7 +71,7 @@ final class DoctrineEntityManipulator
}
// is parent entity
if ($this->docBlockManipulator->hasTag($class, InheritanceType::class)) {
if ($this->docBlockManipulator->hasTag($class, InheritanceTypeTagValueNode::class)) {
return false;
}

View File

@ -109,6 +109,11 @@ final class DocBlockManipulator
return true;
}
// allow only class nodes further
if (! class_exists($name)) {
return false;
}
// advanced check, e.g. for "Namespaced\Annotations\DI"
$phpDocInfo = $this->createPhpDocInfoFromNode($node);

View File

@ -9,7 +9,7 @@ use PHPStan\Type\UnionType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
@ -26,17 +26,16 @@ final class AnnotatedPropertyInjectToConstructorInjectionRector extends Abstract
/**
* @var string
*/
private $annotation;
private const INJECT_ANNOTATION = 'inject';
/**
* @var DocBlockManipulator
*/
private $docBlockManipulator;
public function __construct(DocBlockManipulator $docBlockManipulator, string $annotation = 'inject')
public function __construct(DocBlockManipulator $docBlockManipulator)
{
$this->docBlockManipulator = $docBlockManipulator;
$this->annotation = $annotation;
}
public function getDefinition(): RectorDefinition
@ -44,7 +43,7 @@ final class AnnotatedPropertyInjectToConstructorInjectionRector extends Abstract
return new RectorDefinition(
'Turns non-private properties with `@annotation` to private properties and constructor injection',
[
new ConfiguredCodeSample(
new CodeSample(
<<<'CODE_SAMPLE'
/**
* @var SomeService
@ -64,10 +63,6 @@ public function __construct(SomeService $someService)
$this->someService = $someService;
}
CODE_SAMPLE
,
[
'$annotation' => 'inject',
]
),
]
);
@ -86,16 +81,11 @@ CODE_SAMPLE
*/
public function refactor(Node $node): ?Node
{
if (! $this->docBlockManipulator->hasTag($node, $this->annotation)) {
if ($this->shouldSkipProperty($node)) {
return null;
}
// it needs @var tag as well, to get the type
if (! $this->docBlockManipulator->hasTag($node, 'var')) {
return null;
}
$this->docBlockManipulator->removeTagFromNode($node, $this->annotation);
$this->docBlockManipulator->removeTagFromNode($node, self::INJECT_ANNOTATION);
// set to private
$this->makePrivate($node);
@ -124,4 +114,18 @@ CODE_SAMPLE
$this->addPropertyToClass($classNode, $propertyType, $propertyName);
}
private function shouldSkipProperty(Node $node): bool
{
if (! $this->docBlockManipulator->hasTag($node, self::INJECT_ANNOTATION)) {
return true;
}
// it needs @var tag as well, to get the type
if (! $this->docBlockManipulator->hasTag($node, 'var')) {
return true;
}
return false;
}
}

View File

@ -142,6 +142,7 @@ CODE_SAMPLE
$tagClass = $this->annotationToTagClass[$annotationClass];
$injectTagValueNode = $phpDocInfo->getByType($tagClass);
if ($injectTagValueNode === null) {
continue;
}
@ -165,10 +166,7 @@ CODE_SAMPLE
return null;
}
if (! $this->docBlockManipulator->getVarType()) {
$this->docBlockManipulator->changeVarTag($property, $type);
}
$this->docBlockManipulator->changeVarTag($property, $type);
$this->docBlockManipulator->removeTagFromNode($property, $tagClass);
$classNode = $property->getAttribute(AttributeKey::CLASS_NODE);

View File

@ -24,15 +24,8 @@ final class AnnotatedPropertyInjectToConstructorInjectionRectorTest extends Abst
]);
}
/**
* @return mixed[]
*/
protected function getRectorsWithConfiguration(): array
protected function getRectorClass(): string
{
return [
AnnotatedPropertyInjectToConstructorInjectionRector::class => [
'$annotation' => 'inject',
],
];
return AnnotatedPropertyInjectToConstructorInjectionRector::class;
}
}