5
5
import com .intellij .patterns .ElementPattern ;
6
6
import com .intellij .patterns .PlatformPatterns ;
7
7
import com .intellij .patterns .PsiElementPattern ;
8
- import com .intellij .psi .PsiElement ;
9
- import com .intellij .psi .PsiElementResolveResult ;
10
- import com .intellij .psi .PsiWhiteSpace ;
11
- import com .intellij .psi .ResolveResult ;
8
+ import com .intellij .psi .*;
12
9
import com .intellij .psi .util .PsiTreeUtil ;
13
10
import com .jetbrains .php .PhpIndex ;
14
11
import com .jetbrains .php .completion .PhpLookupElement ;
@@ -190,6 +187,33 @@ static public boolean isMethodWithFirstString(PsiElement psiElement, String... m
190
187
return null != methodRefName && Arrays .asList (methodName ).contains (methodRefName );
191
188
}
192
189
190
+ /**
191
+ * $this->methodName('service_name')
192
+ * $this->methodName(SERVICE::NAME)
193
+ * $this->methodName($this->name)
194
+ */
195
+ static public boolean isMethodWithFirstStringOrFieldReference (PsiElement psiElement , String ... methodName ) {
196
+
197
+ if (!PlatformPatterns
198
+ .psiElement (PhpElementTypes .METHOD_REFERENCE )
199
+ .withChild (PlatformPatterns
200
+ .psiElement (PhpElementTypes .PARAMETER_LIST )
201
+ .withFirstChild (PlatformPatterns .or (
202
+ PlatformPatterns .psiElement (PhpElementTypes .STRING ),
203
+ PlatformPatterns .psiElement (PhpElementTypes .FIELD_REFERENCE ),
204
+ PlatformPatterns .psiElement (PhpElementTypes .CLASS_CONSTANT_REFERENCE )
205
+ ))
206
+ ).accepts (psiElement )) {
207
+
208
+ return false ;
209
+ }
210
+
211
+ // cant we move it up to PlatformPatterns? withName condition dont looks working
212
+ String methodRefName = ((MethodReference ) psiElement ).getName ();
213
+
214
+ return null != methodRefName && Arrays .asList (methodName ).contains (methodRefName );
215
+ }
216
+
193
217
static public PsiElementPattern .Capture <StringLiteralExpression > methodWithFirstStringPattern () {
194
218
return PlatformPatterns
195
219
.psiElement (StringLiteralExpression .class )
@@ -426,19 +450,28 @@ public static String getStringValue(PsiElement psiElement) {
426
450
}
427
451
428
452
@ Nullable
429
- private static String getStringValue (PsiElement psiElement , int depth ) {
453
+ private static String getStringValue (@ Nullable PsiElement psiElement , int depth ) {
430
454
431
- if (++depth > 5 ) {
455
+ if (psiElement == null || ++depth > 5 ) {
432
456
return null ;
433
457
}
434
458
435
459
if (psiElement instanceof StringLiteralExpression ) {
436
460
return ((StringLiteralExpression ) psiElement ).getContents ();
437
461
}
438
462
463
+ if (psiElement instanceof Field ) {
464
+ return getStringValue (((Field ) psiElement ).getDefaultValue (), depth );
465
+ }
466
+
439
467
if (psiElement instanceof PhpReference ) {
440
- PsiElement ref = psiElement .getReference ().resolve ();
441
468
469
+ PsiReference psiReference = psiElement .getReference ();
470
+ if (psiReference == null ) {
471
+ return null ;
472
+ }
473
+
474
+ PsiElement ref = psiReference .resolve ();
442
475
if (ref instanceof PhpReference ) {
443
476
return getStringValue (psiElement , depth );
444
477
}
@@ -451,8 +484,6 @@ private static String getStringValue(PsiElement psiElement, int depth) {
451
484
}
452
485
}
453
486
454
-
455
-
456
487
}
457
488
458
489
return null ;
0 commit comments