Skip to content

Added the possibility to create a plugin for a method in the parent class #981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/magento2/common.properties
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ common.template.type=Email Type
common.diagnostic.reportButtonText=Report Me
common.diagnostic.reportSubmittedTitle=The report is successfully submitted!
common.diagnostic.reportSubmittedMessage=Thank you for submitting your report! We will check it as soon as possible.
common.targetMethod=Target Method
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class CreateAPluginAction extends DumbAwareAction {

public static final String ACTION_NAME = "Create a new Plugin for this method";
public static final String ACTION_NAME = "Create a new Plugin";
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Plugin";
private final GetFirstClassOfFile getFirstClassOfFile;
private Method targetMethod;
Expand Down Expand Up @@ -58,7 +58,6 @@ public void update(final AnActionEvent event) {
|| !(psiFile instanceof PhpFile)
|| phpClass.isFinal()
|| PhpClassImplementsNoninterceptableInterfaceUtil.execute(phpClass)
|| this.targetMethod == null
) {
this.setStatus(event, false);
return;
Expand Down Expand Up @@ -103,6 +102,7 @@ private Pair<PsiFile, PhpClass> findPhpClass(final @NotNull AnActionEvent event)
return Pair.create(psiFile, phpClass);
}

@SuppressWarnings({"PMD.CyclomaticComplexity"})
private void fetchTargetMethod(
final @NotNull AnActionEvent event,
final PsiFile psiFile,
Expand All @@ -125,6 +125,11 @@ private void fetchTargetMethod(
this.targetMethod = (Method) element;
return;
}

if (element instanceof PhpClass) {
return;
}

final PsiElement parent = element.getParent();

if (parent instanceof Method && parent.getParent().equals(phpClass)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.CreateAPluginDialog">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="8" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="10" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="600" height="500"/>
</constraints>
<properties>
<minimumSize width="400" height="350"/>
<preferredSize width="400" height="350"/>
<requestFocusEnabled value="true"/>
</properties>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="7" column="0" row-span="1" col-span="3" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="9" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
Expand Down Expand Up @@ -56,7 +54,7 @@
<grid id="e3588" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="6" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
Expand Down Expand Up @@ -149,7 +147,7 @@
</grid>
<component id="5196a" class="javax.swing.JLabel" binding="selectPluginModule">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="bd654"/>
Expand All @@ -158,7 +156,7 @@
</component>
<component id="b0d15" class="javax.swing.JComboBox" binding="pluginType">
<constraints>
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<clientProperties>
Expand All @@ -167,7 +165,7 @@
</component>
<component id="d3b42" class="javax.swing.JLabel" binding="pluginTypeLabel">
<constraints>
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="magento2/common" key="common.methodType"/>
Expand All @@ -193,6 +191,20 @@
<text resource-bundle="magento2/common" key="common.area.target"/>
</properties>
</component>
<component id="d0a79" class="javax.swing.JLabel" binding="targetMethodLabel">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="magento2/common" key="common.targetMethod"/>
</properties>
</component>
<component id="678c2" class="com.magento.idea.magento2plugin.ui.FilteredComboBox" binding="targetMethodSelect" custom-create="true">
<constraints>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import com.magento.idea.magento2plugin.magento.packages.File;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.ui.FilteredComboBox;
import com.magento.idea.magento2plugin.util.php.PhpTypeMetadataParserUtil;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
Expand All @@ -50,7 +52,7 @@
public class CreateAPluginDialog extends AbstractDialog {
@NotNull
private final Project project;
private final Method targetMethod;
private Method targetMethod;
private final PhpClass targetClass;
private JPanel contentPane;
private JButton buttonOK;
Expand All @@ -63,13 +65,20 @@ public class CreateAPluginDialog extends AbstractDialog {
private static final String SORT_ORDER = "sort order";
private static final String PLUGIN_NAME = "plugin name";
private static final String TARGET_MODULE = "target module";
private static final String TARGET_METHOD = "target method";

@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, TARGET_MODULE})
@FieldValidation(rule = RuleRegistry.BOX_NOT_EMPTY,
message = {BoxNotEmptyRule.MESSAGE, TARGET_MODULE})
private FilteredComboBox pluginModule;

@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, TARGET_METHOD})
@FieldValidation(rule = RuleRegistry.BOX_NOT_EMPTY,
message = {BoxNotEmptyRule.MESSAGE, TARGET_METHOD})
private FilteredComboBox targetMethodSelect;

@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, CLASS_NAME})
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
Expand Down Expand Up @@ -99,6 +108,7 @@ public class CreateAPluginDialog extends AbstractDialog {
private JLabel pluginNameLabel;//NOPMD
private JLabel pluginClassNameLabel;//NOPMD
private JLabel pluginSortOrderLabel;//NOPMD
private JLabel targetMethodLabel;

/**
* Constructor.
Expand All @@ -124,6 +134,10 @@ public CreateAPluginDialog(
fillPluginTypeOptions();
fillTargetAreaOptions();

if (targetMethod != null) {
this.targetMethodLabel.setVisible(false);
}

buttonOK.addActionListener((final ActionEvent event) -> onOK());
buttonCancel.addActionListener((final ActionEvent event) -> onCancel());

Expand Down Expand Up @@ -157,6 +171,9 @@ private void fillTargetAreaOptions() {
}

protected void onOK() {
if (targetMethod == null) {
targetMethod = getSelectedTargetMethod();
}
if (validateFormFields()) {
new PluginClassGenerator(new PluginFileData(
getPluginDirectory(),
Expand Down Expand Up @@ -209,6 +226,25 @@ public String getPluginModule() {
return this.pluginModule.getSelectedItem().toString();
}

/**
* Searches and returns a selected target method.
*
* @return Method target method
*/
public Method getSelectedTargetMethod() {
final String selectedMethodString = this.targetMethodSelect.getSelectedItem().toString();
final List<Method> publicMethods = PhpTypeMetadataParserUtil.getPublicMethods(
this.targetClass
);
for (final Method method: publicMethods) {
if (method.getName().equals(selectedMethodString)) {
return method;
}
}

return null;
}

/**
* Open an action dialog.
*
Expand Down Expand Up @@ -236,6 +272,18 @@ private void createUIComponents() {
.getEditableModuleNames();

this.pluginModule = new FilteredComboBox(allModulesList);

final List<Method> publicMethods
= PhpTypeMetadataParserUtil.getPublicMethods(this.targetClass);
final List<String> methodList = new ArrayList<>();
for (final Method method: publicMethods) {
methodList.add(method.getName());
}

this.targetMethodSelect = new FilteredComboBox(methodList);
if (targetMethod != null) {
this.targetMethodSelect.setVisible(false);
}
}

private String getNamespace() {
Expand Down