14
14
import fr .adrienbrault .idea .symfony2plugin .codeInsight .GotoCompletionRegistrar ;
15
15
import fr .adrienbrault .idea .symfony2plugin .codeInsight .GotoCompletionRegistrarParameter ;
16
16
import fr .adrienbrault .idea .symfony2plugin .util .MethodMatcher ;
17
+ import fr .adrienbrault .idea .symfony2plugin .util .PhpElementsUtil ;
17
18
import org .apache .commons .lang .StringUtils ;
18
19
import org .jetbrains .annotations .NotNull ;
19
20
import org .jetbrains .annotations .Nullable ;
@@ -158,13 +159,7 @@ private Map<String, CommandArg> getCommandConfigurationMap(@NotNull PhpClass php
158
159
return Collections .emptyMap ();
159
160
}
160
161
161
- PsiElement [] psiElements = PsiTreeUtil .collectElements (configure , new PsiElementFilter () {
162
- @ Override
163
- public boolean isAccepted (PsiElement psiElement ) {
164
- return psiElement instanceof MethodReference && methodName .equals (((MethodReference ) psiElement ).getName ());
165
- }
166
- });
167
-
162
+ PsiElement [] psiElements = PsiTreeUtil .collectElements (configure , new CommandDefPsiElementFilter (methodName ));
168
163
if (psiElements .length == 0 ) {
169
164
return Collections .emptyMap ();
170
165
}
@@ -177,27 +172,79 @@ public boolean isAccepted(PsiElement psiElement) {
177
172
continue ;
178
173
}
179
174
180
- PsiElement [] parameters = ((MethodReference ) element ).getParameters ();
181
- if (parameters .length > 0 && parameters [0 ] instanceof StringLiteralExpression ) {
182
- String contents = ((StringLiteralExpression ) parameters [0 ]).getContents ();
183
- if (StringUtils .isNotBlank (contents )) {
184
-
185
- if (methodName .equals ("addOption" )) {
186
- targets .put (contents , new CommandArg (parameters [0 ], contents , getParameterStringValue (parameters , 3 ), getParameterStringValue (parameters , 4 )));
187
- } else if (methodName .equals ("addArgument" )) {
188
- targets .put (contents , new CommandArg (parameters [0 ], contents , getParameterStringValue (parameters , 2 ), getParameterStringValue (parameters , 3 )));
189
- } else {
190
- targets .put (contents , new CommandArg (parameters [0 ], contents ));
175
+ /*
176
+ ->setDefinition(new InputArgument())
177
+ ->setDefinition(array(
178
+ new InputArgument(),
179
+ new InputOption(),
180
+ ));
181
+ */
182
+ if ("setDefinition" .equals (((MethodReference ) element ).getName ())) {
183
+
184
+ Collection <NewExpression > newExpressions = PsiTreeUtil .collectElementsOfType (element , NewExpression .class );
185
+ for (NewExpression newExpression : newExpressions ) {
186
+ if (methodName .equals ("addOption" ) && PhpElementsUtil .getNewExpressionPhpClassWithInstance (newExpression , "Symfony\\ Component\\ Console\\ Input\\ InputOption" ) != null ) {
187
+
188
+ // new InputOption()
189
+ PsiElement [] parameters = newExpression .getParameters ();
190
+ String contents = getParameterStringValue (parameters , 0 );
191
+ if (contents != null && StringUtils .isNotBlank (contents )) {
192
+ targets .put (contents , new CommandArg (parameters [0 ], contents , getParameterStringValue (parameters , 2 ), getParameterStringValue (parameters , 3 )));
193
+ }
194
+
195
+ } else if (methodName .equals ("addArgument" ) && PhpElementsUtil .getNewExpressionPhpClassWithInstance (newExpression , "Symfony\\ Component\\ Console\\ Input\\ InputArgument" ) != null ) {
196
+
197
+ // new InputArgument()
198
+ PsiElement [] parameters = newExpression .getParameters ();
199
+ String contents = getParameterStringValue (parameters , 0 );
200
+ if (contents != null && StringUtils .isNotBlank (contents )) {
201
+ targets .put (contents , new CommandArg (parameters [0 ], contents , getParameterStringValue (parameters , 2 ), getParameterStringValue (parameters , 3 )));
202
+ }
191
203
}
192
204
193
205
}
206
+
207
+ } else {
208
+
209
+ /*
210
+ ->addArgument('arg3', null, 'desc')
211
+ ->addOption('opt1', null, null, 'desc', 'default')
212
+ */
213
+ PsiElement [] parameters = ((MethodReference ) element ).getParameters ();
214
+ if (parameters .length > 0 && parameters [0 ] instanceof StringLiteralExpression ) {
215
+ String contents = ((StringLiteralExpression ) parameters [0 ]).getContents ();
216
+ if (StringUtils .isNotBlank (contents )) {
217
+ if (methodName .equals ("addOption" )) {
218
+ targets .put (contents , new CommandArg (parameters [0 ], contents , getParameterStringValue (parameters , 3 ), getParameterStringValue (parameters , 4 )));
219
+ } else if (methodName .equals ("addArgument" )) {
220
+ targets .put (contents , new CommandArg (parameters [0 ], contents , getParameterStringValue (parameters , 2 ), getParameterStringValue (parameters , 3 )));
221
+ }
222
+ }
223
+ }
194
224
}
195
225
196
226
}
197
227
198
228
return targets ;
199
229
}
200
230
231
+ private static class CommandDefPsiElementFilter implements PsiElementFilter {
232
+ private final String methodName ;
233
+
234
+ public CommandDefPsiElementFilter (String methodName ) {
235
+ this .methodName = methodName ;
236
+ }
237
+
238
+ @ Override
239
+ public boolean isAccepted (PsiElement psiElement ) {
240
+ if (!(psiElement instanceof MethodReference )) {
241
+ return false ;
242
+ }
243
+
244
+ String name = ((MethodReference ) psiElement ).getName ();
245
+ return methodName .equals (name ) || "setDefinition" .equals (name );
246
+ }
247
+ }
201
248
}
202
249
203
250
private static class CommandArg {
0 commit comments