@@ -87,37 +87,67 @@ private void visitMethodReference(@NotNull MethodReference methodReference) {
87
87
}
88
88
89
89
if (parameters [0 ] instanceof StringLiteralExpression ) {
90
- // foo('foo.html.twig')
91
- String contents = ((StringLiteralExpression ) parameters [0 ]).getContents ();
92
- if (StringUtils .isBlank (contents ) || !contents .endsWith (".twig" )) {
93
- return ;
94
- }
90
+ resolveString (methodReference , parameters [0 ]);
91
+ } else if (parameters [0 ] instanceof TernaryExpression ) {
92
+ // render(true === true ? 'foo.twig.html' : 'foobar.twig.html')
93
+ for (PhpPsiElement phpPsiElement : new PhpPsiElement []{((TernaryExpression ) parameters [0 ]).getTrueVariant (), ((TernaryExpression ) parameters [0 ]).getFalseVariant ()}) {
94
+ if (phpPsiElement == null ) {
95
+ continue ;
96
+ }
95
97
96
- Function parentOfType = PsiTreeUtil .getParentOfType (methodReference , Function .class );
97
- if (parentOfType == null ) {
98
- return ;
98
+ if (phpPsiElement instanceof StringLiteralExpression ) {
99
+ resolveString (methodReference , phpPsiElement );
100
+ } else if (phpPsiElement instanceof PhpReference ) {
101
+ resolvePhpReference (methodReference , phpPsiElement );
102
+ }
99
103
}
100
-
101
- addTemplateWithScope (contents , StringUtils .stripStart (parentOfType .getFQN (), "\\ " ));
102
104
} else if (parameters [0 ] instanceof PhpReference ) {
103
- for (PhpNamedElement phpNamedElement : ((PhpReference ) parameters [0 ]).resolveLocal ()) {
104
- // foo(self::foo)
105
- // foo($this->foo)
106
- if (phpNamedElement instanceof Field ) {
107
- PsiElement defaultValue = ((Field ) phpNamedElement ).getDefaultValue ();
108
- if (defaultValue instanceof StringLiteralExpression ) {
109
- addStringLiteralScope (methodReference , (StringLiteralExpression ) defaultValue );
110
- }
105
+ resolvePhpReference (methodReference , parameters [0 ]);
106
+ } else if (parameters [0 ] instanceof BinaryExpression ) {
107
+ // render($foo ?? 'foo.twig.html')
108
+ PsiElement phpPsiElement = ((BinaryExpression ) parameters [0 ]).getRightOperand ();
109
+
110
+ if (phpPsiElement instanceof StringLiteralExpression ) {
111
+ resolveString (methodReference , phpPsiElement );
112
+ } else if (phpPsiElement instanceof PhpReference ) {
113
+ resolvePhpReference (methodReference , phpPsiElement );
114
+ }
115
+ }
116
+ }
117
+
118
+ private void resolveString (@ NotNull MethodReference methodReference , PsiElement parameter ) {
119
+ // foo('foo.html.twig')
120
+ String contents = ((StringLiteralExpression ) parameter ).getContents ();
121
+ if (StringUtils .isBlank (contents ) || !contents .endsWith (".twig" )) {
122
+ return ;
123
+ }
124
+
125
+ Function parentOfType = PsiTreeUtil .getParentOfType (methodReference , Function .class );
126
+ if (parentOfType == null ) {
127
+ return ;
128
+ }
129
+
130
+ addTemplateWithScope (contents , StringUtils .stripStart (parentOfType .getFQN (), "\\ " ));
131
+ }
132
+
133
+ private void resolvePhpReference (@ NotNull MethodReference methodReference , PsiElement parameter ) {
134
+ for (PhpNamedElement phpNamedElement : ((PhpReference ) parameter ).resolveLocal ()) {
135
+ // foo(self::foo)
136
+ // foo($this->foo)
137
+ if (phpNamedElement instanceof Field ) {
138
+ PsiElement defaultValue = ((Field ) phpNamedElement ).getDefaultValue ();
139
+ if (defaultValue instanceof StringLiteralExpression ) {
140
+ addStringLiteralScope (methodReference , (StringLiteralExpression ) defaultValue );
111
141
}
142
+ }
112
143
113
- // foo($var) => $var = 'test.html.twig'
114
- if (phpNamedElement instanceof Variable ) {
115
- PsiElement assignmentExpression = phpNamedElement .getParent ();
116
- if (assignmentExpression instanceof AssignmentExpression ) {
117
- PhpPsiElement value = ((AssignmentExpression ) assignmentExpression ).getValue ();
118
- if (value instanceof StringLiteralExpression ) {
119
- addStringLiteralScope (methodReference , (StringLiteralExpression ) value );
120
- }
144
+ // foo($var) => $var = 'test.html.twig'
145
+ if (phpNamedElement instanceof Variable ) {
146
+ PsiElement assignmentExpression = phpNamedElement .getParent ();
147
+ if (assignmentExpression instanceof AssignmentExpression ) {
148
+ PhpPsiElement value = ((AssignmentExpression ) assignmentExpression ).getValue ();
149
+ if (value instanceof StringLiteralExpression ) {
150
+ addStringLiteralScope (methodReference , (StringLiteralExpression ) value );
121
151
}
122
152
}
123
153
}
0 commit comments