Skip to content

Commit 3293ea1

Browse files
409: Added target type suggestion
1 parent f8901e2 commit 3293ea1

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewArgumentInjectionDialog.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.intellij.ui.EditorTextField;
1616
import com.jetbrains.php.PhpIndex;
1717
import com.jetbrains.php.completion.PhpCompletionUtil;
18+
import com.jetbrains.php.lang.PhpLangUtil;
1819
import com.jetbrains.php.lang.psi.elements.Field;
1920
import com.jetbrains.php.lang.psi.elements.Parameter;
2021
import com.jetbrains.php.lang.psi.elements.PhpClass;
@@ -33,11 +34,13 @@
3334
import com.magento.idea.magento2plugin.magento.packages.Areas;
3435
import com.magento.idea.magento2plugin.magento.packages.DiArgumentType;
3536
import com.magento.idea.magento2plugin.ui.FilteredComboBox;
37+
import com.magento.idea.magento2plugin.util.php.PhpTypeMetadataParserUtil;
3638
import java.awt.event.ActionListener;
3739
import java.awt.event.KeyEvent;
3840
import java.awt.event.WindowAdapter;
3941
import java.awt.event.WindowEvent;
4042
import java.util.ArrayList;
43+
import java.util.Arrays;
4144
import java.util.Collection;
4245
import java.util.List;
4346
import javax.swing.JButton;
@@ -69,6 +72,7 @@ public class NewArgumentInjectionDialog extends AbstractDialog {
6972

7073
private final @NotNull Project project;
7174
private final PhpClass targetClass;
75+
private final Parameter targetParameter;
7276

7377
private JPanel contentPane;
7478
private JButton buttonCancel;
@@ -172,6 +176,7 @@ public NewArgumentInjectionDialog(
172176

173177
this.project = project;
174178
this.targetClass = targetClass;
179+
targetParameter = parameter;
175180
arrayValues = new DiArrayValueData();
176181

177182
setContentPane(contentPane);
@@ -333,6 +338,7 @@ public void documentChanged(final @NotNull DocumentEvent event) {
333338
}
334339
}
335340
});
341+
guessTargetType();
336342
}
337343

338344
/**
@@ -618,6 +624,39 @@ private void changeViewBySelectedArgumentType(final String itemValue) {
618624
return "";
619625
}
620626

627+
@SuppressWarnings("PMD.CyclomaticComplexity")
628+
private void guessTargetType() {
629+
final String mainType = PhpTypeMetadataParserUtil.getMainType(targetParameter);
630+
631+
if (mainType == null) {
632+
return;
633+
}
634+
String targetDiType = "";
635+
636+
if (Arrays.asList("int", "float").contains(mainType)) {
637+
targetDiType = DiArgumentType.NUMBER.getArgumentType();
638+
} else if (DiArgumentType.STRING.getArgumentType().equals(mainType)) {
639+
targetDiType = DiArgumentType.STRING.getArgumentType();
640+
} else if ("bool".equals(mainType)) {
641+
targetDiType = DiArgumentType.BOOLEAN.getArgumentType();
642+
} else if (PhpLangUtil.isFqn(mainType)) {
643+
targetDiType = DiArgumentType.OBJECT.getArgumentType();
644+
} else if ("array".equals(mainType)) {
645+
targetDiType = DiArgumentType.ARRAY.getArgumentType();
646+
}
647+
648+
if (targetDiType.isEmpty()) {
649+
return;
650+
}
651+
652+
for (int i = 0; i < argumentType.getItemCount(); i++) {
653+
if (targetDiType.equals(argumentType.getItemAt(i).getKey())) {
654+
argumentType.setSelectedIndex(i);
655+
break;
656+
}
657+
}
658+
}
659+
621660
private DiArgumentData getDialogDataObject() {
622661
if (targetArea.getSelectedItem() == null) {
623662
showErrorMessage(new ValidatorBundle().message(NotEmptyRule.MESSAGE, TARGET_AREA));

src/com/magento/idea/magento2plugin/util/php/PhpTypeMetadataParserUtil.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,12 @@ public static List<Method> getMethodsByNames(
135135
*
136136
* @return String
137137
*/
138-
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity", "PMD.ConfusingTernary"})
138+
@SuppressWarnings({
139+
"PMD.CyclomaticComplexity",
140+
"PMD.CognitiveComplexity",
141+
"PMD.NPathComplexity",
142+
"PMD.ConfusingTernary"
143+
})
139144
public static String getMethodDefinitionForInterface(
140145
final @NotNull Method method,
141146
final String defaultMethodDescription
@@ -269,6 +274,29 @@ public static List<String> getMethodParametersTypes(final @NotNull Method method
269274
return types;
270275
}
271276

277+
/**
278+
* Get main type for the specified parameter (?int -> main type is int).
279+
*
280+
* @param parameter Parameter
281+
*
282+
* @return String
283+
*/
284+
public static String getMainType(final @NotNull Parameter parameter) {
285+
final List<String> types = extractMultipleTypesFromString(
286+
parameter.getDeclaredType().toString()
287+
);
288+
289+
for (final String type : types) {
290+
if (PhpLangUtil.isFqn(type)) {
291+
return type;
292+
} else if (PhpLangUtil.isParameterTypeHint(type, parameter.getProject())) {
293+
return type;
294+
}
295+
}
296+
297+
return null;
298+
}
299+
272300
/**
273301
* Get method return type.
274302
*
@@ -309,6 +337,7 @@ public static String getMethodReturnType(final @NotNull Method method) {
309337
*
310338
* @return List[String]
311339
*/
340+
@SuppressWarnings("PMD.CognitiveComplexity")
312341
public static List<String> getMethodExceptionsTypes(final @NotNull Method method) {
313342
final List<String> types = new ArrayList<>();
314343

0 commit comments

Comments
 (0)