4
4
5
5
namespace Rector \Doctrine \Bundle230 \Rector \Class_ ;
6
6
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 ;
13
7
use PhpParser \Node ;
8
+ use PhpParser \Node \Arg ;
14
9
use PhpParser \Node \Expr \ClassConstFetch ;
10
+ use PhpParser \Node \Expr \StaticCall ;
11
+ use PhpParser \Node \Name ;
15
12
use PhpParser \Node \Stmt \Class_ ;
13
+ use PhpParser \Node \Stmt \ClassMethod ;
14
+ use PhpParser \Node \Stmt \Expression ;
16
15
use PHPStan \PhpDocParser \Ast \PhpDoc \GenericTagValueNode ;
17
16
use PHPStan \PhpDocParser \Ast \PhpDoc \PhpDocTagNode ;
18
17
use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfoFactory ;
19
18
use Rector \Comments \NodeDocBlock \DocBlockUpdater ;
19
+ use Rector \Doctrine \CodeQuality \Enum \DoctrineClass ;
20
20
use Rector \Rector \AbstractRector ;
21
21
use Rector \ValueObject \MethodName ;
22
22
use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
27
27
*/
28
28
final class AddAnnotationToRepositoryRector extends AbstractRector
29
29
{
30
- public function __construct (private readonly DocBlockUpdater $ docBlockUpdater , private readonly PhpDocInfoFactory $ phpDocInfoFactory )
31
- {
30
+ public function __construct (
31
+ private readonly DocBlockUpdater $ docBlockUpdater ,
32
+ private readonly PhpDocInfoFactory $ phpDocInfoFactory
33
+ ) {
32
34
}
33
35
34
36
public function getRuleDefinition (): RuleDefinition
35
37
{
36
38
return new RuleDefinition ('Add @extends ServiceEntityRepository<T> annotation to repository classes ' , [
37
39
new CodeSample (
38
40
<<<'CODE_SAMPLE'
41
+ use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
42
+
39
43
final class SomeRepository extends ServiceEntityRepository
40
44
{
41
45
public function __construct(ManagerRegistry $registry)
@@ -46,7 +50,11 @@ public function __construct(ManagerRegistry $registry)
46
50
CODE_SAMPLE
47
51
,
48
52
<<<'CODE_SAMPLE'
49
- /** @extends \Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository<\SomeEntity> */
53
+ use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
54
+
55
+ /**
56
+ * @extends ServiceEntityRepository<\SomeEntity>
57
+ */
50
58
final class SomeRepository extends ServiceEntityRepository
51
59
{
52
60
public function __construct(ManagerRegistry $registry)
@@ -88,23 +96,21 @@ public function refactor(Node $node): ?Node
88
96
89
97
private function isRepositoryClass (Class_ $ class ): bool
90
98
{
91
- if ($ class ->extends instanceof Name) {
92
- return $ this ->getName (
93
- $ class ->extends
94
- ) === 'Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository ' ;
99
+ if (! $ class ->extends instanceof Name) {
100
+ return false ;
95
101
}
96
102
97
- return false ;
103
+ return $ this -> isName ( $ class -> extends , DoctrineClass:: SERVICE_ENTITY_REPOSITORY ) ;
98
104
}
99
105
100
106
private function getEntityClassFromConstructor (Class_ $ class ): ?string
101
107
{
102
- $ method = $ class ->getMethod (MethodName::CONSTRUCT );
103
- if (!$ method instanceof ClassMethod || $ method ->stmts === null ) {
108
+ $ classMethod = $ class ->getMethod (MethodName::CONSTRUCT );
109
+ if (! $ classMethod instanceof ClassMethod || $ classMethod ->stmts === null ) {
104
110
return null ;
105
111
}
106
112
107
- foreach ($ method ->stmts as $ stmt ) {
113
+ foreach ($ classMethod ->stmts as $ stmt ) {
108
114
if (! $ stmt instanceof Expression) {
109
115
continue ;
110
116
}
@@ -136,8 +142,9 @@ private function getEntityClassFromConstructor(Class_ $class): ?string
136
142
private function addAnnotationToNode (Class_ $ class , string $ entityClass ): void
137
143
{
138
144
$ phpDocInfo = $ this ->phpDocInfoFactory ->createFromNodeOrEmpty ($ class );
139
- $ annotation = sprintf ('\Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository<\%s> ' , $ entityClass );
140
- $ phpDocInfo ->addPhpDocTagNode (new PhpDocTagNode ('@extends ' , new GenericTagValueNode ($ annotation )));
145
+
146
+ $ genericsAnnotation = sprintf ('\%s<\%s> ' , DoctrineClass::SERVICE_ENTITY_REPOSITORY , $ entityClass );
147
+ $ phpDocInfo ->addPhpDocTagNode (new PhpDocTagNode ('@extends ' , new GenericTagValueNode ($ genericsAnnotation )));
141
148
142
149
$ this ->docBlockUpdater ->updateRefactoredNodeWithPhpDocInfo ($ class );
143
150
}
@@ -150,12 +157,10 @@ private function hasExtendsAnnotation(Class_ $class): bool
150
157
151
158
private function isParentConstructorCall (StaticCall $ staticCall ): bool
152
159
{
153
- return $ staticCall ->class instanceof Name
154
- && $ staticCall ->class ->toString () === 'parent '
155
- && $ staticCall ->name instanceof Identifier
156
- && $ staticCall ->name ->toString () === '__construct '
160
+ return $ this ->isName ($ staticCall ->class , 'parent ' )
161
+ && $ this ->isName ($ staticCall ->name , '__construct ' )
157
162
&& isset ($ staticCall ->args [1 ])
158
- && $ staticCall ->args [1 ] instanceof Arg // Controleer of args[1] een Node\Arg is
163
+ && $ staticCall ->args [1 ] instanceof Arg // Controller of args[1] een Node\Arg is
159
164
&& $ staticCall ->args [1 ]->value instanceof ClassConstFetch;
160
165
}
161
166
}
0 commit comments