Skip to content

Commit d5e0907

Browse files
Code refactoring after code review
1 parent 0761a8b commit d5e0907

File tree

9 files changed

+87
-30
lines changed

9 files changed

+87
-30
lines changed

src/com/magento/idea/magento2plugin/actions/generation/NewWebApiDeclarationAction.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
import com.jetbrains.php.lang.psi.elements.PhpClass;
1515
import com.magento.idea.magento2plugin.MagentoIcons;
1616
import com.magento.idea.magento2plugin.actions.generation.dialog.NewWebApiDeclarationDialog;
17+
import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass;
1718
import com.magento.idea.magento2plugin.project.Settings;
1819
import com.magento.idea.magento2plugin.util.php.PhpPsiElementsUtil;
1920
import org.jetbrains.annotations.NotNull;
2021

2122
public class NewWebApiDeclarationAction extends AnAction {
2223

23-
public static final String ACTION_NAME = "Create a new WebApi declaration for this method";
24-
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 WebApi XML declaration";
24+
public static final String ACTION_NAME = "Create a new Web API declaration for this method";
25+
public static final String ACTION_DESCRIPTION =
26+
"Create a new Magento 2 Web API XML declaration";
2527
private Method currentPhpMethod;
2628

2729
/**
@@ -48,7 +50,7 @@ public void update(final @NotNull AnActionEvent event) {
4850
final PhpDocComment classDocComment = method.getContainingClass().getDocComment();
4951

5052
if (!method.getAccess().isPublic()
51-
|| method.getName().equals("__construct")
53+
|| method.getName().equals(MagentoPhpClass.CONSTRUCT_METHOD_NAME)
5254
|| classDocComment == null
5355
|| !classDocComment.getText().contains("@api")) {
5456
return;

src/com/magento/idea/magento2plugin/actions/generation/data/xml/WebApiXmlRouteData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class WebApiXmlRouteData {
1717
private final String aclResource;
1818

1919
/**
20-
* Web Api xml declaration DTO constructor.
20+
* Web API XML declaration DTO constructor.
2121
*
2222
* @param moduleName String
2323
* @param url String

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
1818
import com.magento.idea.magento2plugin.actions.generation.generator.xml.WebApiDeclarationGenerator;
1919
import com.magento.idea.magento2plugin.magento.packages.HttpMethod;
20+
import com.magento.idea.magento2plugin.magento.packages.WebApiResource;
2021
import com.magento.idea.magento2plugin.ui.FilteredComboBox;
2122
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
2223
import java.awt.event.KeyEvent;
2324
import java.awt.event.WindowAdapter;
2425
import java.awt.event.WindowEvent;
25-
import java.util.Arrays;
26-
import java.util.LinkedList;
2726
import javax.swing.JButton;
2827
import javax.swing.JComboBox;
2928
import javax.swing.JComponent;
@@ -173,7 +172,7 @@ private void createUIComponents() {
173172
for (final String method : HttpMethod.getHttpMethodList()) {
174173
httpMethod.addItem(new ComboBoxItemData(method, method));
175174
}
176-
aclResource = new FilteredComboBox(new LinkedList<>(Arrays.asList("self", "anonymous")));
175+
aclResource = new FilteredComboBox(WebApiResource.getDefaultResourcesList());
177176
}
178177

179178
/**

src/com/magento/idea/magento2plugin/actions/generation/generator/xml/WebApiDeclarationGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public WebApiDeclarationGenerator(
6565
}
6666

6767
/**
68-
* Generate WEB API xml declaration.
68+
* Generate Web API XML declaration.
6969
*
7070
* @param actionName String
7171
*

src/com/magento/idea/magento2plugin/inspections/php/PluginInspection.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private void checkTargetMethod(
145145
final String targetClassMethodName,
146146
final Method targetMethod
147147
) {
148-
if (targetClassMethodName.equals(Plugin.CONSTRUCT_METHOD_NAME)) {
148+
if (targetClassMethodName.equals(MagentoPhpClass.CONSTRUCT_METHOD_NAME)) {
149149
problemsHolder.registerProblem(
150150
pluginMethod.getNameIdentifier(),
151151
inspectionBundle.message("inspection.plugin.error.constructMethod"),
@@ -200,10 +200,11 @@ private void checkParametersCompatibility(
200200
pluginMethodParameter,
201201
PhpBundle.message(
202202
WRONG_PARAM_TYPE,
203-
new Object[]{declaredType, targetClassFqn}
204-
),
203+
declaredType,
204+
targetClassFqn
205+
),
205206
ProblemHighlightType.ERROR
206-
);
207+
);
207208
}
208209
if (!checkPossibleTypeIncompatibility(targetClassFqn, declaredType)) {
209210
problemsHolder.registerProblem(
@@ -219,21 +220,24 @@ private void checkParametersCompatibility(
219220
if (index == 2 && pluginPrefix.equals(Plugin.PluginType.around.toString())) {
220221
if (!checkTypeIncompatibility(Plugin.CALLABLE_PARAM, declaredType)
221222
&& !checkTypeIncompatibility(
222-
Package.fqnSeparator.concat(Plugin.CLOSURE_PARAM),
223-
declaredType)
223+
Package.fqnSeparator.concat(Plugin.CLOSURE_PARAM),
224+
declaredType)
224225
) {
225226
problemsHolder.registerProblem(
226227
pluginMethodParameter,
227228
PhpBundle.message(
228-
WRONG_PARAM_TYPE,
229-
new Object[]{declaredType, "callable"}
229+
WRONG_PARAM_TYPE,
230+
declaredType,
231+
"callable"
230232
),
231233
ProblemHighlightType.ERROR);
232234
}
233235
continue;
234236
}
235237
if (index == 2 && pluginPrefix.equals(Plugin.PluginType.after.toString())
236-
&& !targetMethod.getDeclaredType().toString().equals("void")) {
238+
&& !MagentoPhpClass.VOID_RETURN_TYPE.equals(
239+
targetMethod.getDeclaredType().toString()
240+
)) {
237241
if (declaredType.isEmpty() || targetMethod.getDeclaredType()
238242
.toString().isEmpty()) {
239243
continue;
@@ -244,9 +248,9 @@ private void checkParametersCompatibility(
244248
pluginMethodParameter,
245249
PhpBundle.message(
246250
WRONG_PARAM_TYPE,
247-
new Object[]{declaredType,
248-
targetMethod.getDeclaredType().toString()}
249-
),
251+
declaredType,
252+
targetMethod.getDeclaredType().toString()
253+
),
250254
ProblemHighlightType.ERROR
251255
);
252256
}
@@ -265,7 +269,9 @@ private void checkParametersCompatibility(
265269
continue;
266270
}
267271
if (index == 2 && pluginPrefix.equals(Plugin.PluginType.after.toString())
268-
&& targetMethod.getDeclaredType().toString().equals("void")) {
272+
&& MagentoPhpClass.VOID_RETURN_TYPE.equals(
273+
targetMethod.getDeclaredType().toString()
274+
)) {
269275
if (declaredType.isEmpty()) {
270276
continue;
271277
}
@@ -274,8 +280,7 @@ private void checkParametersCompatibility(
274280
pluginMethodParameter,
275281
PhpBundle.message(
276282
WRONG_PARAM_TYPE,
277-
new Object[]{declaredType, "null"}
278-
),
283+
declaredType, "null"),
279284
ProblemHighlightType.ERROR);
280285
}
281286
continue;
@@ -306,9 +311,9 @@ private void checkParametersCompatibility(
306311
pluginMethodParameter,
307312
PhpBundle.message(
308313
WRONG_PARAM_TYPE,
309-
new Object[]{declaredType,
310-
targetMethodParameterDeclaredType}
311-
),
314+
declaredType,
315+
targetMethodParameterDeclaredType
316+
),
312317
ProblemHighlightType.ERROR);
313318
}
314319
if (!checkPossibleTypeIncompatibility(

src/com/magento/idea/magento2plugin/magento/files/Plugin.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ public enum PluginType {
2424
around//NOPMD
2525
}
2626

27-
//forbidden target method
28-
public static final String CONSTRUCT_METHOD_NAME = "__construct";
29-
3027
//allowed methods access type
3128
public static final String PUBLIC_ACCESS = "public";
3229

src/com/magento/idea/magento2plugin/magento/packages/MagentoPhpClass.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package com.magento.idea.magento2plugin.magento.packages;
77

88
public class MagentoPhpClass { //NOPMD
9+
10+
public static final String CONSTRUCT_METHOD_NAME = "__construct";
911
public static final String CLOSING_TAG = "}";
1012
public static final String VOID_RETURN_TYPE = "void";
1113
public static final String MIXED_RETURN_TYPE = "mixed";
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.magento.packages;
7+
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
public enum WebApiResource {
13+
14+
SELF("self"),
15+
ANONYMOUS("anonymous");
16+
17+
private final String resource;
18+
19+
/**
20+
* Web API resource ENUM constructor.
21+
*
22+
* @param resource String
23+
*/
24+
WebApiResource(final @NotNull String resource) {
25+
this.resource = resource;
26+
}
27+
28+
/**
29+
* Get Web API resource name.
30+
*
31+
* @return String
32+
*/
33+
public String getResource() {
34+
return resource;
35+
}
36+
37+
/**
38+
* Get default Web API resources.
39+
*
40+
* @return List[String]
41+
*/
42+
public static List<String> getDefaultResourcesList() {
43+
final List<String> resources = new LinkedList<>();
44+
45+
for (final WebApiResource resource : WebApiResource.values()) {
46+
resources.add(resource.getResource());
47+
}
48+
49+
return resources;
50+
}
51+
}

src/com/magento/idea/magento2plugin/util/magento/plugin/IsPluginAllowedForMethodUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.jetbrains.php.lang.psi.elements.Method;
99
import com.magento.idea.magento2plugin.magento.files.Plugin;
10+
import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass;
1011

1112
public final class IsPluginAllowedForMethodUtil {
1213

@@ -20,7 +21,7 @@ private IsPluginAllowedForMethodUtil() {}
2021
*/
2122
public static boolean check(final Method targetMethod) {
2223
final String targetMethodName = targetMethod.getName();
23-
if (targetMethodName.equals(Plugin.CONSTRUCT_METHOD_NAME)) {
24+
if (targetMethodName.equals(MagentoPhpClass.CONSTRUCT_METHOD_NAME)) {
2425
return false;
2526
}
2627
if (targetMethod.isFinal()) {

0 commit comments

Comments
 (0)