Skip to content

Commit 7189573

Browse files
committed
#1366 mark EntityRepository classes as used code, if any metadata exists with them
1 parent febe751 commit 7189573

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/codeInsight/SymfonyImplicitUsageProvider.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.jetbrains.php.lang.psi.elements.*;
88
import de.espend.idea.php.annotation.dict.PhpDocCommentAnnotation;
99
import de.espend.idea.php.annotation.util.AnnotationUtil;
10+
import fr.adrienbrault.idea.symfony2plugin.doctrine.metadata.util.DoctrineMetadataUtil;
1011
import fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper;
1112
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
1213
import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil;
@@ -38,12 +39,18 @@ public boolean isImplicitUsage(@NotNull PsiElement element) {
3839
|| isCommandAndService((PhpClass) element)
3940
|| isSubscribedEvent((PhpClass) element)
4041
|| isVoter((PhpClass) element)
41-
|| isTwigExtension((PhpClass) element);
42+
|| isTwigExtension((PhpClass) element)
43+
|| isEntityRepository((PhpClass) element);
4244
}
4345

4446
return false;
4547
}
4648

49+
private boolean isEntityRepository(@NotNull PhpClass phpClass) {
50+
return PhpElementsUtil.isInstanceOf(phpClass, "\\Doctrine\\ORM\\EntityRepository")
51+
&& DoctrineMetadataUtil.findMetadataForRepositoryClass(phpClass).size() > 0;
52+
}
53+
4754
private boolean isTwigExtension(PhpClass phpClass) {
4855
if ((PhpElementsUtil.isInstanceOf(phpClass, "\\Twig\\Extension\\ExtensionInterface") || PhpElementsUtil.isInstanceOf(phpClass,"\\Twig_ExtensionInterface")) && ServiceUtil.isPhpClassAService(phpClass)) {
4956
Set<String> methods = new HashSet<>();

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/codeInsight/SymfonyImplicitUsageProviderTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ public void testVoterRegisteredAsServiceAreMarkedUsed() {
147147
assertTrue(new SymfonyImplicitUsageProvider().isImplicitUsage(firstClassFromFile));
148148
}
149149

150+
public void testEntityRepositoryInsideDoctrineMetadataIsMarkedAsUsed() {
151+
PsiFile psiFile = myFixture.configureByText(PhpFileType.INSTANCE, "<?php\n" +
152+
"namespace App\\Repository;\n" +
153+
"class MyFoobarEntityRepository extends \\Doctrine\\ORM\\EntityRepository {}\n"
154+
);
155+
156+
PhpClass firstClassFromFile = PhpElementsUtil.getFirstClassFromFile((PhpFile) psiFile.getContainingFile());
157+
assertTrue(new SymfonyImplicitUsageProvider().isImplicitUsage(firstClassFromFile));
158+
}
159+
150160
public void testEventSubscriberGetSubscribedEventsArray() {
151161
PsiFile psiFile = myFixture.configureByText(PhpFileType.INSTANCE, "<?php\n" +
152162
"<?php\n" +

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/codeInsight/fixtures/classes.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,29 @@ interface ExtensionInterface
3838
public function getFilters();
3939
public function getFunctions();
4040
}
41-
}
41+
}
42+
43+
namespace Doctrine\ORM
44+
{
45+
class EntityRepository
46+
{
47+
}
48+
}
49+
50+
namespace Doctrine\ORM\Mapping
51+
{
52+
class Entity {};
53+
}
54+
55+
namespace App\Entity
56+
{
57+
use Doctrine\ORM\Mapping AS ORM;
58+
59+
/**
60+
* @ORM\Entity(repositoryClass="App\Repository\MyFoobarEntityRepository")
61+
*/
62+
class FooEntity
63+
{
64+
}
65+
}
66+

0 commit comments

Comments
 (0)