10
10
import com .jetbrains .php .lang .psi .elements .impl .StringLiteralExpressionImpl ;
11
11
import fr .adrienbrault .idea .symfony2plugin .Symfony2ProjectComponent ;
12
12
import fr .adrienbrault .idea .symfony2plugin .util .MethodMatcher ;
13
- import fr .adrienbrault .idea .symfony2plugin .util .PhpElementsUtil ;
13
+ import fr .adrienbrault .idea .symfony2plugin .util .PsiElementUtils ;
14
14
import org .jetbrains .annotations .NotNull ;
15
15
16
16
import java .util .Collections ;
17
17
import java .util .List ;
18
18
19
19
public class ParameterLanguageInjector implements MultiHostInjector {
20
20
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 ),
25
25
};
26
26
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 ),
30
30
};
31
31
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 ),
36
36
};
37
37
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 ),
41
71
};
42
72
43
73
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 ),
48
79
};
49
80
50
81
public static final String LANGUAGE_ID_CSS = "CSS" ;
51
82
public static final String LANGUAGE_ID_XPATH = "XPath" ;
52
83
public static final String LANGUAGE_ID_JSON = "JSON" ;
53
84
public static final String LANGUAGE_ID_DQL = "DQL" ;
85
+ public static final String LANGUAGE_ID_PHPREGEXP = "PhpRegExp" ;
54
86
55
87
private static final String DQL_VARIABLE_NAME = "dql" ;
56
88
@@ -83,7 +115,7 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
83
115
return ;
84
116
}
85
117
86
- if (isParameter ) {
118
+ if (isParameter ) {
87
119
parent = parent .getParent ();
88
120
}
89
121
@@ -96,9 +128,19 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
96
128
// $em->createQuery('...')
97
129
// JsonResponse::fromJsonString('...')
98
130
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
+ }
102
144
}
103
145
}
104
146
// $dql = "...";
@@ -122,17 +164,17 @@ private void injectLanguage(@NotNull MultiHostRegistrar registrar, @NotNull Stri
122
164
final TextRange range = TextRange .create (1 , length + 1 );
123
165
124
166
registrar .startInjecting (language )
125
- .addPlace (languageInjection .getPrefix (), languageInjection .getSuffix (), element , range )
126
- .doneInjecting ();
167
+ .addPlace (languageInjection .getPrefix (), languageInjection .getSuffix (), element , range )
168
+ .doneInjecting ();
127
169
}
128
170
129
171
private class MethodLanguageInjection {
130
172
private final Language language ;
131
173
private final String prefix ;
132
174
private final String suffix ;
133
- private final MethodMatcher . CallToSignature [] signatures ;
175
+ private final MethodParameterSignature [] signatures ;
134
176
135
- MethodLanguageInjection (@ NotNull String languageId , String prefix , String suffix , MethodMatcher . CallToSignature [] signatures ) {
177
+ MethodLanguageInjection (@ NotNull String languageId , String prefix , String suffix , MethodParameterSignature [] signatures ) {
136
178
137
179
this .language = Language .findLanguageByID (languageId );
138
180
this .prefix = prefix ;
@@ -152,8 +194,32 @@ public String getSuffix() {
152
194
return suffix ;
153
195
}
154
196
155
- public MethodMatcher . CallToSignature [] getSignatures () {
197
+ public MethodParameterSignature [] getSignatures () {
156
198
return signatures ;
157
199
}
158
200
}
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
+ }
159
225
}
0 commit comments