@@ -107,37 +107,67 @@ private void visitMethodReference(@NotNull MethodReference methodReference) {
107
107
}
108
108
109
109
if (parameters [0 ] instanceof StringLiteralExpression ) {
110
- // foo('foo.html.twig')
111
- String contents = ((StringLiteralExpression ) parameters [0 ]).getContents ();
112
- if (StringUtils .isBlank (contents ) || !contents .endsWith (".twig" )) {
113
- return ;
114
- }
110
+ resolveString (methodReference , parameters [0 ]);
111
+ } else if (parameters [0 ] instanceof TernaryExpression ) {
112
+ // render(true === true ? 'foo.twig.html' : 'foobar.twig.html')
113
+ for (PhpPsiElement phpPsiElement : new PhpPsiElement []{((TernaryExpression ) parameters [0 ]).getTrueVariant (), ((TernaryExpression ) parameters [0 ]).getFalseVariant ()}) {
114
+ if (phpPsiElement == null ) {
115
+ continue ;
116
+ }
115
117
116
- Function parentOfType = PsiTreeUtil .getParentOfType (methodReference , Function .class );
117
- if (parentOfType == null ) {
118
- return ;
118
+ if (phpPsiElement instanceof StringLiteralExpression ) {
119
+ resolveString (methodReference , phpPsiElement );
120
+ } else if (phpPsiElement instanceof PhpReference ) {
121
+ resolvePhpReference (methodReference , phpPsiElement );
122
+ }
119
123
}
120
-
121
- addTemplateWithScope (contents , StringUtils .stripStart (parentOfType .getFQN (), "\\ " ));
122
124
} else if (parameters [0 ] instanceof PhpReference ) {
123
- for (PhpNamedElement phpNamedElement : ((PhpReference ) parameters [0 ]).resolveLocal ()) {
124
- // foo(self::foo)
125
- // foo($this->foo)
126
- if (phpNamedElement instanceof Field ) {
127
- PsiElement defaultValue = ((Field ) phpNamedElement ).getDefaultValue ();
128
- if (defaultValue instanceof StringLiteralExpression ) {
129
- addStringLiteralScope (methodReference , (StringLiteralExpression ) defaultValue );
130
- }
125
+ resolvePhpReference (methodReference , parameters [0 ]);
126
+ } else if (parameters [0 ] instanceof BinaryExpression ) {
127
+ // render($foo ?? 'foo.twig.html')
128
+ PsiElement phpPsiElement = ((BinaryExpression ) parameters [0 ]).getRightOperand ();
129
+
130
+ if (phpPsiElement instanceof StringLiteralExpression ) {
131
+ resolveString (methodReference , phpPsiElement );
132
+ } else if (phpPsiElement instanceof PhpReference ) {
133
+ resolvePhpReference (methodReference , phpPsiElement );
134
+ }
135
+ }
136
+ }
137
+
138
+ private void resolveString (@ NotNull MethodReference methodReference , PsiElement parameter ) {
139
+ // foo('foo.html.twig')
140
+ String contents = ((StringLiteralExpression ) parameter ).getContents ();
141
+ if (StringUtils .isBlank (contents ) || !contents .endsWith (".twig" )) {
142
+ return ;
143
+ }
144
+
145
+ Function parentOfType = PsiTreeUtil .getParentOfType (methodReference , Function .class );
146
+ if (parentOfType == null ) {
147
+ return ;
148
+ }
149
+
150
+ addTemplateWithScope (contents , StringUtils .stripStart (parentOfType .getFQN (), "\\ " ));
151
+ }
152
+
153
+ private void resolvePhpReference (@ NotNull MethodReference methodReference , PsiElement parameter ) {
154
+ for (PhpNamedElement phpNamedElement : ((PhpReference ) parameter ).resolveLocal ()) {
155
+ // foo(self::foo)
156
+ // foo($this->foo)
157
+ if (phpNamedElement instanceof Field ) {
158
+ PsiElement defaultValue = ((Field ) phpNamedElement ).getDefaultValue ();
159
+ if (defaultValue instanceof StringLiteralExpression ) {
160
+ addStringLiteralScope (methodReference , (StringLiteralExpression ) defaultValue );
131
161
}
162
+ }
132
163
133
- // foo($var) => $var = 'test.html.twig'
134
- if (phpNamedElement instanceof Variable ) {
135
- PsiElement assignmentExpression = phpNamedElement .getParent ();
136
- if (assignmentExpression instanceof AssignmentExpression ) {
137
- PhpPsiElement value = ((AssignmentExpression ) assignmentExpression ).getValue ();
138
- if (value instanceof StringLiteralExpression ) {
139
- addStringLiteralScope (methodReference , (StringLiteralExpression ) value );
140
- }
164
+ // foo($var) => $var = 'test.html.twig'
165
+ if (phpNamedElement instanceof Variable ) {
166
+ PsiElement assignmentExpression = phpNamedElement .getParent ();
167
+ if (assignmentExpression instanceof AssignmentExpression ) {
168
+ PhpPsiElement value = ((AssignmentExpression ) assignmentExpression ).getValue ();
169
+ if (value instanceof StringLiteralExpression ) {
170
+ addStringLiteralScope (methodReference , (StringLiteralExpression ) value );
141
171
}
142
172
}
143
173
}
0 commit comments