1010import com .jetbrains .php .lang .psi .elements .impl .StringLiteralExpressionImpl ;
1111import fr .adrienbrault .idea .symfony2plugin .Symfony2ProjectComponent ;
1212import fr .adrienbrault .idea .symfony2plugin .util .MethodMatcher ;
13- import fr .adrienbrault .idea .symfony2plugin .util .PhpElementsUtil ;
13+ import fr .adrienbrault .idea .symfony2plugin .util .PsiElementUtils ;
1414import org .jetbrains .annotations .NotNull ;
1515
1616import java .util .Collections ;
1717import java .util .List ;
1818
1919public class ParameterLanguageInjector implements MultiHostInjector {
2020
21- private static final MethodMatcher . CallToSignature [] CSS_SELECTOR_SIGNATURES = {
22- new MethodMatcher . CallToSignature ("\\ Symfony\\ Component\\ DomCrawler\\ Crawler" , "filter" ),
23- new MethodMatcher . CallToSignature ("\\ Symfony\\ Component\\ DomCrawler\\ Crawler" , "children" ),
24- new MethodMatcher . CallToSignature ("\\ Symfony\\ Component\\ CssSelector\\ CssSelectorConverter" , "toXPath" ),
21+ private static final MethodParameterSignature [] CSS_SELECTOR_SIGNATURES = {
22+ new MethodParameterSignature ("\\ Symfony\\ Component\\ DomCrawler\\ Crawler" , "filter" , 0 ),
23+ new MethodParameterSignature ("\\ Symfony\\ Component\\ DomCrawler\\ Crawler" , "children" , 0 ),
24+ new MethodParameterSignature ("\\ Symfony\\ Component\\ CssSelector\\ CssSelectorConverter" , "toXPath" , 0 ),
2525 };
2626
27- private static final MethodMatcher . CallToSignature [] XPATH_SIGNATURES = {
28- new MethodMatcher . CallToSignature ("\\ Symfony\\ Component\\ DomCrawler\\ Crawler" , "filterXPath" ),
29- new MethodMatcher . CallToSignature ("\\ Symfony\\ Component\\ DomCrawler\\ Crawler" , "evaluate" ),
27+ private static final MethodParameterSignature [] XPATH_SIGNATURES = {
28+ new MethodParameterSignature ("\\ Symfony\\ Component\\ DomCrawler\\ Crawler" , "filterXPath" , 0 ),
29+ new MethodParameterSignature ("\\ Symfony\\ Component\\ DomCrawler\\ Crawler" , "evaluate" , 0 ),
3030 };
3131
32- private static final MethodMatcher . CallToSignature [] JSON_SIGNATURES = {
33- //new MethodMatcher.CallToSignature ("\\Symfony\\Component\\HttpFoundation\\JsonResponse", "__construct"),
34- new MethodMatcher . CallToSignature ("\\ Symfony\\ Component\\ HttpFoundation\\ JsonResponse" , "fromJsonString" ),
35- new MethodMatcher . CallToSignature ("\\ Symfony\\ Component\\ HttpFoundation\\ JsonResponse" , "setJson" ),
32+ private static final MethodParameterSignature [] JSON_SIGNATURES = {
33+ //new MethodParameterSignature ("\\Symfony\\Component\\HttpFoundation\\JsonResponse", "__construct", 0 ),
34+ new MethodParameterSignature ("\\ Symfony\\ Component\\ HttpFoundation\\ JsonResponse" , "fromJsonString" , 0 ),
35+ new MethodParameterSignature ("\\ Symfony\\ Component\\ HttpFoundation\\ JsonResponse" , "setJson" , 0 ),
3636 };
3737
38- private static final MethodMatcher .CallToSignature [] DQL_SIGNATURES = {
39- new MethodMatcher .CallToSignature ("\\ Doctrine\\ ORM\\ EntityManager" , "createQuery" ),
40- new MethodMatcher .CallToSignature ("\\ Doctrine\\ ORM\\ Query" , "setDQL" ),
38+ private static final MethodParameterSignature [] DQL_SIGNATURES = {
39+ new MethodParameterSignature ("\\ Doctrine\\ ORM\\ EntityManager" , "createQuery" , 0 ),
40+ new MethodParameterSignature ("\\ Doctrine\\ ORM\\ Query" , "setDQL" , 0 ),
41+ };
42+
43+ private static final MethodParameterSignature [] PHPREGEXP_SIGNATURES = {
44+ new MethodParameterSignature ("\\ Symfony\\ Bridge\\ PhpUnit\\ DeprecationErrorHandler\\ Configuration" , "fromRegex" , 0 ),
45+ new MethodParameterSignature ("\\ Symfony\\ Bundle\\ FrameworkBundle\\ CacheWarmer\\ AnnotationsCacheWarmer" , "__construct" , 2 ),
46+ new MethodParameterSignature ("\\ Symfony\\ Component\\ Config\\ Resource\\ DirectoryResource" , "__construct" , 1 ),
47+ new MethodParameterSignature ("\\ Symfony\\ Component\\ Console\\ Question\\ ConfirmationQuestion" , "__construct" , 2 ),
48+ new MethodParameterSignature ("\\ Symfony\\ Component\\ CssSelector\\ Parser\\ Reader" , "findPattern" , 0 ),
49+ new MethodParameterSignature ("\\ Symfony\\ Component\\ HttpFoundation\\ AcceptHeader" , "filter" , 0 ),
50+ new MethodParameterSignature ("\\ Symfony\\ Component\\ Security\\ Http\\ HttpUtils" , "__construct" , 2 ),
51+ new MethodParameterSignature ("\\ Symfony\\ Component\\ Security\\ Http\\ HttpUtils" , "__construct" , 3 ),
52+ new MethodParameterSignature ("\\ Symfony\\ Component\\ Validator\\ Mapping\\ Loader\\ PropertyInfoLoader" , "__construct" , 2 ),
53+ new MethodParameterSignature ("\\ Monolog\\ Handler\\ TestHandler" , "hasRecordThatMatches" , 0 ),
54+ new MethodParameterSignature ("\\ Monolog\\ Handler\\ TestHandler" , "hasEmergencyThatMatches" , 0 ),
55+ new MethodParameterSignature ("\\ Monolog\\ Handler\\ TestHandler" , "hasAlertThatMatches" , 0 ),
56+ new MethodParameterSignature ("\\ Monolog\\ Handler\\ TestHandler" , "hasCriticalThatMatches" , 0 ),
57+ new MethodParameterSignature ("\\ Monolog\\ Handler\\ TestHandler" , "hasErrorThatMatches" , 0 ),
58+ new MethodParameterSignature ("\\ Monolog\\ Handler\\ TestHandler" , "hasWarningThatMatches" , 0 ),
59+ new MethodParameterSignature ("\\ Monolog\\ Handler\\ TestHandler" , "hasNoticeThatMatches" , 0 ),
60+ new MethodParameterSignature ("\\ Monolog\\ Handler\\ TestHandler" , "hasInfoThatMatches" , 0 ),
61+ new MethodParameterSignature ("\\ Monolog\\ Handler\\ TestHandler" , "hasDebugThatMatches" , 0 ),
62+ new MethodParameterSignature ("\\ Psr\\ Log\\ Test\\ TestLogger" , "hasRecordThatMatches" , 0 ),
63+ new MethodParameterSignature ("\\ Psr\\ Log\\ Test\\ TestLogger" , "hasEmergencyThatMatches" , 0 ),
64+ new MethodParameterSignature ("\\ Psr\\ Log\\ Test\\ TestLogger" , "hasAlertThatMatches" , 0 ),
65+ new MethodParameterSignature ("\\ Psr\\ Log\\ Test\\ TestLogger" , "hasCriticalThatMatches" , 0 ),
66+ new MethodParameterSignature ("\\ Psr\\ Log\\ Test\\ TestLogger" , "hasErrorThatMatches" , 0 ),
67+ new MethodParameterSignature ("\\ Psr\\ Log\\ Test\\ TestLogger" , "hasWarningThatMatches" , 0 ),
68+ new MethodParameterSignature ("\\ Psr\\ Log\\ Test\\ TestLogger" , "hasNoticeThatMatches" , 0 ),
69+ new MethodParameterSignature ("\\ Psr\\ Log\\ Test\\ TestLogger" , "hasInfoThatMatches" , 0 ),
70+ new MethodParameterSignature ("\\ Psr\\ Log\\ Test\\ TestLogger" , "hasDebugThatMatches" , 0 ),
4171 };
4272
4373 private final MethodLanguageInjection [] LANGUAGE_INJECTIONS = {
44- new MethodLanguageInjection (LANGUAGE_ID_CSS , "@media all { " , " }" , CSS_SELECTOR_SIGNATURES ),
45- new MethodLanguageInjection (LANGUAGE_ID_XPATH , null , null , XPATH_SIGNATURES ),
46- new MethodLanguageInjection (LANGUAGE_ID_JSON , null , null , JSON_SIGNATURES ),
47- new MethodLanguageInjection (LANGUAGE_ID_DQL , null , null , DQL_SIGNATURES ),
74+ new MethodLanguageInjection (LANGUAGE_ID_CSS , "@media all { " , " }" , CSS_SELECTOR_SIGNATURES ),
75+ new MethodLanguageInjection (LANGUAGE_ID_XPATH , null , null , XPATH_SIGNATURES ),
76+ new MethodLanguageInjection (LANGUAGE_ID_JSON , null , null , JSON_SIGNATURES ),
77+ new MethodLanguageInjection (LANGUAGE_ID_DQL , null , null , DQL_SIGNATURES ),
78+ new MethodLanguageInjection (LANGUAGE_ID_PHPREGEXP , null , null , PHPREGEXP_SIGNATURES ),
4879 };
4980
5081 public static final String LANGUAGE_ID_CSS = "CSS" ;
5182 public static final String LANGUAGE_ID_XPATH = "XPath" ;
5283 public static final String LANGUAGE_ID_JSON = "JSON" ;
5384 public static final String LANGUAGE_ID_DQL = "DQL" ;
85+ public static final String LANGUAGE_ID_PHPREGEXP = "PhpRegExp" ;
5486
5587 private static final String DQL_VARIABLE_NAME = "dql" ;
5688
@@ -83,7 +115,7 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
83115 return ;
84116 }
85117
86- if (isParameter ) {
118+ if (isParameter ) {
87119 parent = parent .getParent ();
88120 }
89121
@@ -96,9 +128,19 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
96128 // $em->createQuery('...')
97129 // JsonResponse::fromJsonString('...')
98130 if (parent instanceof MethodReference ) {
99- if (PhpElementsUtil .isMethodReferenceInstanceOf ((MethodReference ) parent , languageInjection .getSignatures ())) {
100- injectLanguage (registrar , expr , language , languageInjection );
101- return ;
131+ for (MethodParameterSignature signature : languageInjection .getSignatures ()) {
132+
133+ PsiElement psiNearestParameter = PsiElementUtils .getParentOfTypeFirstChild (expr , ParameterList .class );
134+ if (psiNearestParameter == null ) {
135+ continue ;
136+ }
137+
138+ MethodMatcher .CallToSignature callToSignature = new MethodMatcher .CallToSignature (signature .getClassName (), signature .getMethodName ());
139+ MethodMatcher .MethodMatchParameter matchParameter = MethodMatcher .getMatchedSignatureWithDepth (psiNearestParameter , new MethodMatcher .CallToSignature []{callToSignature }, signature .getParameterIndex ());
140+ if (matchParameter != null ) {
141+ injectLanguage (registrar , expr , language , languageInjection );
142+ return ;
143+ }
102144 }
103145 }
104146 // $dql = "...";
@@ -122,17 +164,17 @@ private void injectLanguage(@NotNull MultiHostRegistrar registrar, @NotNull Stri
122164 final TextRange range = TextRange .create (1 , length + 1 );
123165
124166 registrar .startInjecting (language )
125- .addPlace (languageInjection .getPrefix (), languageInjection .getSuffix (), element , range )
126- .doneInjecting ();
167+ .addPlace (languageInjection .getPrefix (), languageInjection .getSuffix (), element , range )
168+ .doneInjecting ();
127169 }
128170
129171 private class MethodLanguageInjection {
130172 private final Language language ;
131173 private final String prefix ;
132174 private final String suffix ;
133- private final MethodMatcher . CallToSignature [] signatures ;
175+ private final MethodParameterSignature [] signatures ;
134176
135- MethodLanguageInjection (@ NotNull String languageId , String prefix , String suffix , MethodMatcher . CallToSignature [] signatures ) {
177+ MethodLanguageInjection (@ NotNull String languageId , String prefix , String suffix , MethodParameterSignature [] signatures ) {
136178
137179 this .language = Language .findLanguageByID (languageId );
138180 this .prefix = prefix ;
@@ -152,8 +194,32 @@ public String getSuffix() {
152194 return suffix ;
153195 }
154196
155- public MethodMatcher . CallToSignature [] getSignatures () {
197+ public MethodParameterSignature [] getSignatures () {
156198 return signatures ;
157199 }
158200 }
201+
202+ private static class MethodParameterSignature {
203+ private final String className ;
204+ private final String methodName ;
205+ private final int parameterIndex ;
206+
207+ private MethodParameterSignature (String className , String methodName , int parameterIndex ) {
208+ this .className = className ;
209+ this .methodName = methodName ;
210+ this .parameterIndex = parameterIndex ;
211+ }
212+
213+ public String getClassName () {
214+ return className ;
215+ }
216+
217+ public String getMethodName () {
218+ return methodName ;
219+ }
220+
221+ public int getParameterIndex () {
222+ return parameterIndex ;
223+ }
224+ }
159225}
0 commit comments