44namespace Rector \Privatization \Rector \Class_ ;
55
66use PhpParser \Node ;
7- use PhpParser \Node \Name \FullyQualified ;
87use PhpParser \Node \Stmt \Class_ ;
98use PHPStan \Reflection \ClassReflection ;
109use Rector \Core \NodeAnalyzer \ClassAnalyzer ;
10+ use Rector \Core \NodeAnalyzer \DoctrineEntityAnalyzer ;
1111use Rector \Core \Rector \AbstractRector ;
1212use Rector \Core \Reflection \ReflectionResolver ;
1313use Rector \FamilyTree \Reflection \FamilyRelationsAnalyzer ;
1919 */
2020final class FinalizeClassesWithoutChildrenRector extends AbstractRector
2121{
22- /**
23- * @var string[]
24- */
25- private const DOCTRINE_ORM_MAPPING_ANNOTATION = ['Doctrine \\ORM \\Mapping \\Entity ' , 'Doctrine \\ORM \\Mapping \\Embeddable ' ];
26- /**
27- * @var string[]
28- */
29- private const DOCTRINE_ODM_MAPPING_ANNOTATION = ['Doctrine \\ODM \\MongoDB \\Mapping \\Annotations \\Document ' , 'Doctrine \\ODM \\MongoDB \\Mapping \\Annotations \\EmbeddedDocument ' ];
3022 /**
3123 * @readonly
3224 * @var \Rector\Core\NodeAnalyzer\ClassAnalyzer
@@ -47,12 +39,18 @@ final class FinalizeClassesWithoutChildrenRector extends AbstractRector
4739 * @var \Rector\Core\Reflection\ReflectionResolver
4840 */
4941 private $ reflectionResolver ;
50- public function __construct (ClassAnalyzer $ classAnalyzer , FamilyRelationsAnalyzer $ familyRelationsAnalyzer , VisibilityManipulator $ visibilityManipulator , ReflectionResolver $ reflectionResolver )
42+ /**
43+ * @readonly
44+ * @var \Rector\Core\NodeAnalyzer\DoctrineEntityAnalyzer
45+ */
46+ private $ doctrineEntityAnalyzer ;
47+ public function __construct (ClassAnalyzer $ classAnalyzer , FamilyRelationsAnalyzer $ familyRelationsAnalyzer , VisibilityManipulator $ visibilityManipulator , ReflectionResolver $ reflectionResolver , DoctrineEntityAnalyzer $ doctrineEntityAnalyzer )
5148 {
5249 $ this ->classAnalyzer = $ classAnalyzer ;
5350 $ this ->familyRelationsAnalyzer = $ familyRelationsAnalyzer ;
5451 $ this ->visibilityManipulator = $ visibilityManipulator ;
5552 $ this ->reflectionResolver = $ reflectionResolver ;
53+ $ this ->doctrineEntityAnalyzer = $ doctrineEntityAnalyzer ;
5654 }
5755 public function getRuleDefinition () : RuleDefinition
5856 {
@@ -99,45 +97,23 @@ public function refactor(Node $node) : ?Node
9997 if ($ this ->shouldSkipClass ($ node )) {
10098 return null ;
10199 }
102- $ phpDocInfo = $ this ->phpDocInfoFactory ->createFromNodeOrEmpty ($ node );
103- if ($ phpDocInfo ->hasByAnnotationClasses (self ::DOCTRINE_ORM_MAPPING_ANNOTATION )) {
104- return null ;
105- }
106- if ($ phpDocInfo ->hasByAnnotationClasses (self ::DOCTRINE_ODM_MAPPING_ANNOTATION )) {
100+ if ($ this ->doctrineEntityAnalyzer ->hasClassAnnotation ($ node )) {
107101 return null ;
108102 }
109103 $ classReflection = $ this ->reflectionResolver ->resolveClassReflection ($ node );
110104 if (!$ classReflection instanceof ClassReflection) {
111105 return null ;
112106 }
113- $ childrenClassReflections = $ this ->familyRelationsAnalyzer ->getChildrenOfClassReflection ($ classReflection );
114- if ($ childrenClassReflections !== []) {
107+ if ($ this ->doctrineEntityAnalyzer ->hasClassReflectionAttribute ($ classReflection )) {
115108 return null ;
116109 }
117- if ($ this ->hasDoctrineAttr ($ node )) {
110+ $ childrenClassReflections = $ this ->familyRelationsAnalyzer ->getChildrenOfClassReflection ($ classReflection );
111+ if ($ childrenClassReflections !== []) {
118112 return null ;
119113 }
120114 $ this ->visibilityManipulator ->makeFinal ($ node );
121115 return $ node ;
122116 }
123- private function hasDoctrineAttr (Class_ $ class ) : bool
124- {
125- foreach ($ class ->attrGroups as $ attrGroup ) {
126- foreach ($ attrGroup ->attrs as $ attribute ) {
127- if (!$ attribute ->name instanceof FullyQualified) {
128- continue ;
129- }
130- $ className = $ this ->nodeNameResolver ->getName ($ attribute ->name );
131- if (\in_array ($ className , self ::DOCTRINE_ORM_MAPPING_ANNOTATION , \true)) {
132- return \true;
133- }
134- if (\in_array ($ className , self ::DOCTRINE_ODM_MAPPING_ANNOTATION , \true)) {
135- return \true;
136- }
137- }
138- }
139- return \false;
140- }
141117 private function shouldSkipClass (Class_ $ class ) : bool
142118 {
143119 if ($ class ->isFinal ()) {
0 commit comments