Skip to content

Commit c2dac7b

Browse files
improve add generics annotation to repository to code quality doctrine set (#380)
* improve add generics annotation to repository to code quality doctrine set * [rector] Rector fixes --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent c923389 commit c2dac7b

11 files changed

Lines changed: 59 additions & 28 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"phpunit/phpunit": "^11.5",
1818
"rector/rector-src": "dev-main",
1919
"rector/type-perfect": "^2.0",
20-
"symplify/easy-coding-standard": "^12.5",
20+
"phpecs/phpecs": "^2.1",
2121
"symplify/phpstan-rules": "^14.0",
2222
"symplify/vendor-patches": "^11.3",
2323
"tomasvotruba/class-leak": "^2.0",

config/sets/doctrine-code-quality.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6+
use Rector\Doctrine\Bundle230\Rector\Class_\AddAnnotationToRepositoryRector;
67
use Rector\Doctrine\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector;
78
use Rector\Doctrine\CodeQuality\Rector\Class_\MoveCurrentDateTimeDefaultInEntityToConstructorRector;
89
use Rector\Doctrine\CodeQuality\Rector\Class_\RemoveEmptyTableAttributeRector;
@@ -28,6 +29,9 @@
2829
TypedPropertyFromColumnTypeRector::class,
2930
TypedPropertyFromToOneRelationTypeRector::class,
3031
TypedPropertyFromToManyRelationTypeRector::class,
32+
33+
// annotations generics
34+
AddAnnotationToRepositoryRector::class,
3135
]);
3236

3337
$rectorConfig->ruleWithConfiguration(AttributeKeyToClassConstFetchRector::class, [

config/sets/doctrine-collection-22.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6+
use Rector\Doctrine\Collection22\Rector\CriteriaOrderingConstantsDeprecationRector;
67

78
return static function (RectorConfig $rectorConfig): void {
8-
$rectorConfig->rules([\Rector\Doctrine\Collection22\Rector\CriteriaOrderingConstantsDeprecationRector::class]);
9+
$rectorConfig->rules([CriteriaOrderingConstantsDeprecationRector::class]);
910
};

rector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
return RectorConfig::configure()
99
->withImportNames(removeUnusedImports: true)
10-
->withPaths([__DIR__ . '/src', __DIR__ . '/rules', __DIR__ . '/tests'])
10+
->withPaths([__DIR__ . '/config', __DIR__ . '/src', __DIR__ . '/rules', __DIR__ . '/tests'])
1111
->withSkip(['*/Source/*', '*/Fixture/*'])
1212
->withRootFiles()
1313
->withPhpSets()

rules/Bundle230/Rector/Class_/AddAnnotationToRepositoryRector.php

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,44 @@
44

55
namespace Rector\Doctrine\Bundle230\Rector\Class_;
66

7-
use PhpParser\Node\Name;
8-
use PhpParser\Node\Stmt\ClassMethod;
9-
use PhpParser\Node\Stmt\Expression;
10-
use PhpParser\Node\Expr\StaticCall;
11-
use PhpParser\Node\Identifier;
12-
use PhpParser\Node\Arg;
137
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
149
use PhpParser\Node\Expr\ClassConstFetch;
10+
use PhpParser\Node\Expr\StaticCall;
11+
use PhpParser\Node\Name;
1512
use PhpParser\Node\Stmt\Class_;
13+
use PhpParser\Node\Stmt\ClassMethod;
14+
use PhpParser\Node\Stmt\Expression;
1615
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
1716
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
1817
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1918
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
19+
use Rector\Doctrine\CodeQuality\Enum\DoctrineClass;
2020
use Rector\Rector\AbstractRector;
2121
use Rector\ValueObject\MethodName;
2222
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2323
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2424

2525
/**
2626
* @see https://github.com/doctrine/DoctrineBundle/commit/2f12b5302bafac39c70b024e1686119be28b79ab
27+
*
28+
* @see \Rector\Doctrine\Tests\Bundle230\Rector\Class_\AddAnnotationToRepositoryRector\AddAnnotationToRepositoryRectorTest
2729
*/
2830
final class AddAnnotationToRepositoryRector extends AbstractRector
2931
{
30-
public function __construct(private readonly DocBlockUpdater $docBlockUpdater, private readonly PhpDocInfoFactory $phpDocInfoFactory)
31-
{
32+
public function __construct(
33+
private readonly DocBlockUpdater $docBlockUpdater,
34+
private readonly PhpDocInfoFactory $phpDocInfoFactory
35+
) {
3236
}
3337

3438
public function getRuleDefinition(): RuleDefinition
3539
{
3640
return new RuleDefinition('Add @extends ServiceEntityRepository<T> annotation to repository classes', [
3741
new CodeSample(
3842
<<<'CODE_SAMPLE'
43+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
44+
3945
final class SomeRepository extends ServiceEntityRepository
4046
{
4147
public function __construct(ManagerRegistry $registry)
@@ -46,7 +52,11 @@ public function __construct(ManagerRegistry $registry)
4652
CODE_SAMPLE
4753
,
4854
<<<'CODE_SAMPLE'
49-
/** @extends \Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository<\SomeEntity> */
55+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
56+
57+
/**
58+
* @extends ServiceEntityRepository<\SomeEntity>
59+
*/
5060
final class SomeRepository extends ServiceEntityRepository
5161
{
5262
public function __construct(ManagerRegistry $registry)
@@ -88,23 +98,21 @@ public function refactor(Node $node): ?Node
8898

8999
private function isRepositoryClass(Class_ $class): bool
90100
{
91-
if ($class->extends instanceof Name) {
92-
return $this->getName(
93-
$class->extends
94-
) === 'Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository';
101+
if (! $class->extends instanceof Name) {
102+
return false;
95103
}
96104

97-
return false;
105+
return $this->isName($class->extends, DoctrineClass::SERVICE_ENTITY_REPOSITORY);
98106
}
99107

100108
private function getEntityClassFromConstructor(Class_ $class): ?string
101109
{
102-
$method = $class->getMethod(MethodName::CONSTRUCT);
103-
if (!$method instanceof ClassMethod || $method->stmts === null) {
110+
$classMethod = $class->getMethod(MethodName::CONSTRUCT);
111+
if (! $classMethod instanceof ClassMethod || $classMethod->stmts === null) {
104112
return null;
105113
}
106114

107-
foreach ($method->stmts as $stmt) {
115+
foreach ($classMethod->stmts as $stmt) {
108116
if (! $stmt instanceof Expression) {
109117
continue;
110118
}
@@ -136,8 +144,9 @@ private function getEntityClassFromConstructor(Class_ $class): ?string
136144
private function addAnnotationToNode(Class_ $class, string $entityClass): void
137145
{
138146
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($class);
139-
$annotation = sprintf('\Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository<\%s>', $entityClass);
140-
$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@extends', new GenericTagValueNode($annotation)));
147+
148+
$genericsAnnotation = sprintf('\%s<\%s>', DoctrineClass::SERVICE_ENTITY_REPOSITORY, $entityClass);
149+
$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@extends', new GenericTagValueNode($genericsAnnotation)));
141150

142151
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($class);
143152
}
@@ -150,12 +159,10 @@ private function hasExtendsAnnotation(Class_ $class): bool
150159

151160
private function isParentConstructorCall(StaticCall $staticCall): bool
152161
{
153-
return $staticCall->class instanceof Name
154-
&& $staticCall->class->toString() === 'parent'
155-
&& $staticCall->name instanceof Identifier
156-
&& $staticCall->name->toString() === '__construct'
162+
return $this->isName($staticCall->class, 'parent')
163+
&& $this->isName($staticCall->name, '__construct')
157164
&& isset($staticCall->args[1])
158-
&& $staticCall->args[1] instanceof Arg // Controleer of args[1] een Node\Arg is
165+
&& $staticCall->args[1] instanceof Arg // Controller of args[1] een Node\Arg is
159166
&& $staticCall->args[1]->value instanceof ClassConstFetch;
160167
}
161168
}

rules/CodeQuality/AttributeTransformer/ClassAttributeTransformer/InheritanceClassAttributeTransformer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function transform(EntityMapping $entityMapping, Class_ $class): bool
4545
if (isset($classMapping['discriminatorMap'])) {
4646
$this->addDiscriminatorMap($classMapping['discriminatorMap'], $class);
4747
}
48+
4849
return true;
4950
}
5051

rules/CodeQuality/AttributeTransformer/PropertyAttributeTransformer/InverseJoinColumnAttributeTransformer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function transform(EntityMapping $entityMapping, Property|Param $property
3535
foreach ($joinColumns as $columnName => $joinColumn) {
3636
$property->attrGroups[] = $this->createInverseJoinColumnAttrGroup($columnName, $joinColumn);
3737
}
38+
3839
return true;
3940
}
4041

rules/CodeQuality/AttributeTransformer/PropertyAttributeTransformer/JoinColumnAttributeTransformer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ private function transformMapping(Property|Param $property, ?array $mapping): bo
6464
$hasChanged = true;
6565
$property->attrGroups[] = $this->createJoinColumnAttrGroup($columnName, $joinColumn);
6666
}
67+
6768
return $hasChanged;
6869
}
6970

rules/CodeQuality/AttributeTransformer/YamlToAttributeTransformer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ private function transformClass(Class_ $class, EntityMapping $entityMapping): bo
4545
$hasChanged = true;
4646
}
4747
}
48+
4849
return $hasChanged;
4950
}
5051

@@ -87,6 +88,7 @@ private function transformProperties(Class_ $class, EntityMapping $entityMapping
8788
}
8889
}
8990
}
91+
9092
return $hasChanged;
9193
}
9294

rules/CodeQuality/Rector/Property/ImproveDoctrineCollectionDocTypeInEntityRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ private function refactorAttribute(Expr $expr, PhpDocInfo $phpDocInfo, Property
268268
$this->collectionTypeResolver->hasIndexBy($property),
269269
$property
270270
);
271-
$hasChanged = $this->phpDocTypeChanger->changeVarType($property, $phpDocInfo, $genericObjectType);
272271

272+
$hasChanged = $this->phpDocTypeChanger->changeVarType($property, $phpDocInfo, $genericObjectType);
273273
if (! $hasChanged) {
274274
return null;
275275
}

0 commit comments

Comments
 (0)