7
7
import com .intellij .psi .util .PsiTreeUtil ;
8
8
import com .jetbrains .php .lang .lexer .PhpTokenTypes ;
9
9
import com .jetbrains .php .lang .psi .PhpPsiUtil ;
10
- import com .jetbrains .php .lang .psi .elements .ArrayCreationExpression ;
11
- import com .jetbrains .php .lang .psi .elements .ParameterList ;
12
- import com .jetbrains .php .lang .psi .elements .PhpAttribute ;
13
- import com .jetbrains .php .lang .psi .elements .StringLiteralExpression ;
10
+ import com .jetbrains .php .lang .psi .elements .*;
14
11
import com .jetbrains .php .lang .psi .stubs .indexes .expectedArguments .PhpExpectedFunctionArgument ;
15
12
import org .apache .commons .lang .StringUtils ;
16
13
import org .jetbrains .annotations .NotNull ;
@@ -34,6 +31,8 @@ public static String getAttributeValueByNameAsString(@NotNull PhpAttribute attri
34
31
if (StringUtils .isNotBlank (contents )) {
35
32
return contents ;
36
33
}
34
+ } else if (nextSibling instanceof ClassConstantReference ) {
35
+ return resolveLocalValue (attribute , (ClassConstantReference ) nextSibling );
37
36
}
38
37
39
38
return null ;
@@ -64,9 +63,45 @@ public static String getAttributeValueByNameAsStringWithDefaultParameterFallback
64
63
for (PhpAttribute .PhpAttributeArgument argument : attribute .getArguments ()) {
65
64
PhpExpectedFunctionArgument argument1 = argument .getArgument ();
66
65
if (argument1 .getArgumentIndex () == 0 ) {
67
- String value = PsiElementUtils .trimQuote (argument1 .getValue ());
68
- if (StringUtils .isNotBlank (value )) {
69
- return value ;
66
+
67
+ // hint: reference is a complete fake object lazily created; it is not reflect the real element :(
68
+ // PhpPsiElementFactory.createPhpPsiFromText => com.jetbrains.php.lang.psi.stubs.indexes.expectedArguments.PhpExpectedFunctionClassConstantArgument.createReference
69
+ @ NotNull PsiElement namedElement = argument1 .createReference (attribute .getProject ());
70
+
71
+ if (namedElement instanceof StringLiteralExpression ) {
72
+ // we can trust the string representation here, looks to be right implemented
73
+ String contents = ((StringLiteralExpression ) namedElement ).getContents ();
74
+ if (StringUtils .isNotBlank (contents )) {
75
+ return contents ;
76
+ }
77
+ } else if (namedElement instanceof ClassConstantReference ) {
78
+ // not working: we are in a dummy and temporary out-of-scope object
79
+ // resolveLocalValue(attribute, (ClassConstantReference) nextSibling);
80
+ PhpExpression classReference = ((ClassConstantReference ) namedElement ).getClassReference ();
81
+ if (classReference instanceof ClassReference ) {
82
+ PsiElement phpAttributesList = attribute .getParent ();
83
+ if (phpAttributesList instanceof PhpAttributesList ) {
84
+ PsiElement method = phpAttributesList .getParent ();
85
+ if (method instanceof Method ) {
86
+ PsiElement phpClass = method .getParent ();
87
+ // instead of normal "$this", "self", ... we are getting here the class name :(
88
+ // so to check the owning scope compare it via the class name
89
+ if (phpClass instanceof PhpClass && ((PhpClass ) phpClass ).getFQN ().equals (((ClassReference ) classReference ).getFQN ())) {
90
+ String fieldName = ((ClassConstantReference ) namedElement ).getName ();
91
+ Field ownFieldByName = ((PhpClass ) phpClass ).findOwnFieldByName (fieldName , true );
92
+ if (ownFieldByName != null ) {
93
+ PsiElement defaultValue = ownFieldByName .getDefaultValue ();
94
+ if (defaultValue instanceof StringLiteralExpression ) {
95
+ String contents = ((StringLiteralExpression ) defaultValue ).getContents ();
96
+ if (StringUtils .isNotBlank (contents )) {
97
+ return contents ;
98
+ }
99
+ }
100
+ }
101
+ }
102
+ }
103
+ }
104
+ }
70
105
}
71
106
}
72
107
}
@@ -104,4 +139,36 @@ private static PsiElementPattern.Capture<PsiElement> getAttributeColonPattern(St
104
139
PhpTokenTypes .opCOLON
105
140
).afterLeaf (PlatformPatterns .psiElement ().withElementType (PhpTokenTypes .IDENTIFIER ).withText (name ));
106
141
}
142
+
143
+ @ Nullable
144
+ private static String resolveLocalValue (@ NotNull PhpAttribute attribute , @ NotNull ClassConstantReference nextSibling ) {
145
+ PhpExpression classReference = nextSibling .getClassReference ();
146
+ if (classReference != null ) {
147
+ String name = classReference .getName ();
148
+ if (name != null && (name .equals ("self" ) || name .equals ("static" ))) {
149
+ PsiElement phpAttributesList = attribute .getParent ();
150
+ if (phpAttributesList instanceof PhpAttributesList ) {
151
+ PsiElement method = phpAttributesList .getParent ();
152
+ if (method instanceof Method ) {
153
+ PsiElement phpClass = method .getParent ();
154
+ if (phpClass instanceof PhpClass ) {
155
+ String fieldName = nextSibling .getName ();
156
+ Field ownFieldByName = ((PhpClass ) phpClass ).findOwnFieldByName (fieldName , true );
157
+ if (ownFieldByName != null ) {
158
+ PsiElement defaultValue = ownFieldByName .getDefaultValue ();
159
+ if (defaultValue instanceof StringLiteralExpression ) {
160
+ String contents = ((StringLiteralExpression ) defaultValue ).getContents ();
161
+ if (StringUtils .isNotBlank (contents )) {
162
+ return contents ;
163
+ }
164
+ }
165
+ }
166
+ }
167
+ }
168
+ }
169
+ }
170
+ }
171
+
172
+ return null ;
173
+ }
107
174
}
0 commit comments