55
66package com .magento .idea .magento2plugin .reference .provider ;
77
8+ import com .intellij .openapi .project .Project ;
89import com .intellij .openapi .util .TextRange ;
910import com .intellij .psi .PsiElement ;
1011import com .intellij .psi .PsiReference ;
1112import com .intellij .psi .PsiReferenceProvider ;
13+ import com .intellij .psi .stubs .StubIndex ;
1214import com .intellij .util .ProcessingContext ;
1315import com .jetbrains .php .PhpIndex ;
1416import com .jetbrains .php .lang .psi .elements .PhpClass ;
1517import com .jetbrains .php .lang .psi .elements .PhpNamespace ;
18+ import com .jetbrains .php .lang .psi .stubs .indexes .PhpNamespaceIndex ;
1619import com .magento .idea .magento2plugin .reference .xml .PolyVariantReferenceBase ;
1720import com .magento .idea .magento2plugin .util .RegExUtil ;
1821import java .util .ArrayList ;
2629@ SuppressWarnings ({"PMD.AvoidInstantiatingObjectsInLoops" })
2730public class PhpClassReferenceProvider extends PsiReferenceProvider {
2831
29- @ NotNull
3032 @ Override
31- public PsiReference [] getReferencesByElement (
33+ public @ NotNull PsiReference [] getReferencesByElement (
3234 final @ NotNull PsiElement element ,
3335 final @ NotNull ProcessingContext context
3436 ) {
35-
3637 final String origValue = element .getText ();
3738
3839 final Pattern pattern = Pattern .compile (RegExUtil .PhpRegex .FQN );
@@ -49,15 +50,21 @@ public PsiReference[] getReferencesByElement(
4950 final StringBuilder namespace = new StringBuilder ();
5051 String namespacePart ;
5152 final List <PsiReference > psiReferences = new ArrayList <>();
53+
5254 for (int i = 0 ; i < fqnParts .length - 1 ; i ++) {
5355 namespacePart = fqnParts [i ];
56+ namespace .append ('\\' );
57+ namespace .append (namespacePart );
58+
59+ final String namespaceId = namespace
60+ .toString ()
61+ .toLowerCase (new Locale ("en" ,"EN" ));
62+
63+ final Collection <PhpNamespace > references = hasNamespaceInIndex (
64+ namespaceId ,
65+ element .getProject ()
66+ ) ? phpIndex .getNamespacesByName (namespaceId ) : new ArrayList <>();
5467
55- namespace .append ("\\ " );//NOPMD
56- namespace .append (namespacePart );//NOPMD
57- final Collection <PhpNamespace > references =
58- phpIndex .getNamespacesByName (namespace .toString ().toLowerCase (
59- new Locale ("en" ,"EN" ))
60- );
6168 if (!references .isEmpty ()) {
6269 final TextRange range = new TextRange (
6370 origValue .indexOf (classFQN ) + namespace .toString ().lastIndexOf (92 ),
@@ -70,6 +77,7 @@ public PsiReference[] getReferencesByElement(
7077
7178 final String className = classFQN .substring (classFQN .lastIndexOf (92 ) + 1 );
7279 final Collection <PhpClass > classes = phpIndex .getAnyByFQN (classFQN );
80+
7381 if (!classes .isEmpty ()) {
7482 final TextRange range = new TextRange (
7583 origValue .lastIndexOf (92 ) + 1 ,
@@ -80,4 +88,24 @@ public PsiReference[] getReferencesByElement(
8088
8189 return psiReferences .toArray (new PsiReference [0 ]);
8290 }
91+
92+ /**
93+ * Check if php namespace index has specified identifier.
94+ *
95+ * @param namespaceIdentifier String
96+ * @param project Project
97+ *
98+ * @return boolean
99+ */
100+ private boolean hasNamespaceInIndex (
101+ final @ NotNull String namespaceIdentifier ,
102+ final @ NotNull Project project
103+ ) {
104+ final Collection <String > keys = StubIndex .getInstance ().getAllKeys (
105+ PhpNamespaceIndex .KEY ,
106+ project
107+ );
108+
109+ return keys .contains (namespaceIdentifier );
110+ }
83111}
0 commit comments