mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-22 02:36:52 +01:00
simplify RemoveRepositoryFromEntityAnnotationRector
This commit is contained in:
parent
42cbfd8905
commit
d4c3b002bb
@ -2,10 +2,8 @@
|
||||
|
||||
namespace Rector\Architecture\Rector\Class_;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
|
||||
use Rector\Architecture\Tests\Rector\Class_\RemoveRepositoryFromEntityAnnotationRector\RemoveRepositoryFromEntityAnnotationRectorTest;
|
||||
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
|
||||
use Rector\Rector\AbstractRector;
|
||||
@ -17,11 +15,6 @@ use Rector\RectorDefinition\RectorDefinition;
|
||||
*/
|
||||
final class RemoveRepositoryFromEntityAnnotationRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const DOCTRINE_ORM_MAPPING_ENTITY = 'Doctrine\ORM\Mapping\Entity';
|
||||
|
||||
/**
|
||||
* @var DocBlockManipulator
|
||||
*/
|
||||
@ -79,21 +72,13 @@ CODE_SAMPLE
|
||||
}
|
||||
|
||||
$phpDocInfo = $this->docBlockManipulator->createPhpDocInfoFromNode($node);
|
||||
if (! $phpDocInfo->hasTag(self::DOCTRINE_ORM_MAPPING_ENTITY)) {
|
||||
|
||||
$doctrineEntityTag = $phpDocInfo->getDoctrineEntityTag();
|
||||
if ($doctrineEntityTag === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$entityTags = $phpDocInfo->getTagsByName(self::DOCTRINE_ORM_MAPPING_ENTITY);
|
||||
if ($entityTags === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$entityTag = $entityTags[0];
|
||||
if (! $entityTag->value instanceof GenericTagValueNode) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$entityTag->value->value = Strings::replace($entityTag->value->value, '#\(repositoryClass="(.*?)"\)#');
|
||||
$doctrineEntityTag->removeRepositoryClass();
|
||||
|
||||
// save the entity tag
|
||||
$this->docBlockManipulator->updateNodeWithPhpDocInfo($node, $phpDocInfo);
|
||||
|
@ -13,6 +13,7 @@ use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\AttributeAwareReturnTagValue
|
||||
use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\AttributeAwareVarTagValueNode;
|
||||
use Rector\BetterPhpDocParser\Attributes\Attribute\Attribute;
|
||||
use Rector\BetterPhpDocParser\Attributes\Contract\Ast\AttributeAwareNodeInterface;
|
||||
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\EntityTagValueNode;
|
||||
|
||||
final class PhpDocInfo
|
||||
{
|
||||
@ -167,6 +168,19 @@ final class PhpDocInfo
|
||||
return $this->getResolvedTypesAttribute($varTagValue);
|
||||
}
|
||||
|
||||
public function getDoctrineEntityTag(): ?EntityTagValueNode
|
||||
{
|
||||
foreach ($this->getPhpDocNode()->children as $phpDocChildNode) {
|
||||
if ($phpDocChildNode instanceof PhpDocTagNode) {
|
||||
if ($phpDocChildNode->value instanceof EntityTagValueNode) {
|
||||
return $phpDocChildNode->value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
|
@ -10,15 +10,15 @@ final class RemoveUnusedDoctrineEntityMethodAndPropertyRectorTest extends Abstra
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([
|
||||
// __DIR__ . '/Fixture/fixture.php.inc',
|
||||
// __DIR__ . '/Fixture/remove_inversed_by.php.inc',
|
||||
// __DIR__ . '/Fixture/remove_inversed_by_non_fqn.php.inc',
|
||||
// skip
|
||||
// __DIR__ . '/Fixture/skip_double_entity_call.php.inc',
|
||||
// __DIR__ . '/Fixture/skip_id_and_system.php.inc',
|
||||
// __DIR__ . '/Fixture/skip_trait_called_method.php.inc',
|
||||
// __DIR__ . '/Fixture/skip_trait_doc_typed.php.inc',
|
||||
// __DIR__ . '/Fixture/skip_trait_complex.php.inc',
|
||||
__DIR__ . '/Fixture/fixture.php.inc',
|
||||
__DIR__ . '/Fixture/remove_inversed_by.php.inc',
|
||||
__DIR__ . '/Fixture/remove_inversed_by_non_fqn.php.inc',
|
||||
// skip
|
||||
__DIR__ . '/Fixture/skip_double_entity_call.php.inc',
|
||||
__DIR__ . '/Fixture/skip_id_and_system.php.inc',
|
||||
__DIR__ . '/Fixture/skip_trait_called_method.php.inc',
|
||||
__DIR__ . '/Fixture/skip_trait_doc_typed.php.inc',
|
||||
__DIR__ . '/Fixture/skip_trait_complex.php.inc',
|
||||
__DIR__ . '/Fixture/skip_abstract_parent.php.inc',
|
||||
]);
|
||||
}
|
||||
|
@ -53,4 +53,14 @@ final class EntityTagValueNode implements PhpDocTagValueNode, AttributeAwareNode
|
||||
|
||||
return '(' . implode(', ', $contentItems) . ')';
|
||||
}
|
||||
|
||||
public function removeRepositoryClass(): void
|
||||
{
|
||||
$itemPosition = array_search('repositoryClass', $this->orderedVisibleItems, true);
|
||||
if ($itemPosition !== null) {
|
||||
unset($this->orderedVisibleItems[$itemPosition]);
|
||||
}
|
||||
|
||||
$this->repositoryClass = null;
|
||||
}
|
||||
}
|
||||
|
@ -10,24 +10,24 @@ final class PropertyTypeDeclarationRectorTest extends AbstractRectorTestCase
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([
|
||||
__DIR__ . '/Fixture/constructor_param.php.inc',
|
||||
__DIR__ . '/Fixture/constructor_param_with_nullable.php.inc',
|
||||
__DIR__ . '/Fixture/constructor_param_with_aliased_param.php.inc',
|
||||
__DIR__ . '/Fixture/constructor_array_param_with_nullable.php.inc',
|
||||
__DIR__ . '/Fixture/constructor_assign.php.inc',
|
||||
__DIR__ . '/Fixture/phpunit_setup.php.inc',
|
||||
__DIR__ . '/Fixture/default_value.php.inc',
|
||||
// __DIR__ . '/Fixture/constructor_param.php.inc',
|
||||
// __DIR__ . '/Fixture/constructor_param_with_nullable.php.inc',
|
||||
// __DIR__ . '/Fixture/constructor_param_with_aliased_param.php.inc',
|
||||
// __DIR__ . '/Fixture/constructor_array_param_with_nullable.php.inc',
|
||||
// __DIR__ . '/Fixture/constructor_assign.php.inc',
|
||||
// __DIR__ . '/Fixture/phpunit_setup.php.inc',
|
||||
// __DIR__ . '/Fixture/default_value.php.inc',
|
||||
__DIR__ . '/Fixture/doctrine_column.php.inc',
|
||||
__DIR__ . '/Fixture/doctrine_relation_to_many.php.inc',
|
||||
__DIR__ . '/Fixture/doctrine_relation_to_one.php.inc',
|
||||
__DIR__ . '/Fixture/doctrine_relation_target_entity_same_namespace.php.inc',
|
||||
// get and set
|
||||
__DIR__ . '/Fixture/complex.php.inc',
|
||||
__DIR__ . '/Fixture/single_nullable_return.php.inc',
|
||||
__DIR__ . '/Fixture/getter_type.php.inc',
|
||||
__DIR__ . '/Fixture/setter_type.php.inc',
|
||||
// skip
|
||||
__DIR__ . '/Fixture/skip_multi_vars.php.inc',
|
||||
// __DIR__ . '/Fixture/doctrine_relation_to_many.php.inc',
|
||||
// __DIR__ . '/Fixture/doctrine_relation_to_one.php.inc',
|
||||
// __DIR__ . '/Fixture/doctrine_relation_target_entity_same_namespace.php.inc',
|
||||
// // get and set
|
||||
// __DIR__ . '/Fixture/complex.php.inc',
|
||||
// __DIR__ . '/Fixture/single_nullable_return.php.inc',
|
||||
// __DIR__ . '/Fixture/getter_type.php.inc',
|
||||
// __DIR__ . '/Fixture/setter_type.php.inc',
|
||||
// // skip
|
||||
// __DIR__ . '/Fixture/skip_multi_vars.php.inc',
|
||||
]);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user