Skip to content

Commit 1fc0843

Browse files
author
Vitaliy Boyko
committed
Web api checks
1 parent d5282e3 commit 1fc0843

File tree

11 files changed

+173
-17
lines changed

11 files changed

+173
-17
lines changed

resources/META-INF/plugin.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@
204204
enabledByDefault="true" level="ERROR"
205205
implementationClass="com.magento.idea.magento2plugin.inspections.xml.AclResourceXmlInspection"/>
206206

207+
<localInspection language="XML" groupPath="XML"
208+
shortName="WebApiServiceInspection"
209+
displayName="Inspection for the Web API XML service declaration"
210+
groupName="Magento 2"
211+
enabledByDefault="true" level="WARNING"
212+
implementationClass="com.magento.idea.magento2plugin.inspections.xml.WebApiServiceInspection"/>
213+
207214
<internalFileTemplate name="Magento Composer JSON"/>
208215
<internalFileTemplate name="Magento Registration PHP"/>
209216
<internalFileTemplate name="Magento Module XML"/>

resources/magento2/inspection.properties

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ inspection.observer.disabledObserverDoesNotExist=This observer does not exist to
2020
inspection.cache.disabledCache=Cacheable false attribute on the default layout will disable cache site-wide
2121
inspection.moduleDeclaration.warning.wrongModuleName=Provided module name "{0}" does not match expected "{1}"
2222
inspection.moduleDeclaration.fix=Fix module name
23-
inspection.aclResource.error.missingAttribute=Attribute "{0}" is required
24-
inspection.aclResource.error.idAttributeCanNotBeEmpty=Attribute value "{0}" can not be empty
23+
inspection.error.missingAttribute=Attribute "{0}" is required
24+
inspection.error.idAttributeCanNotBeEmpty=Attribute value "{0}" can not be empty
25+
inspection.warning.class.does.not.exist=The class "{0}" doesn't exist
26+
inspection.warning.method.does.not.exist=The method "{0}" doesn't exist in the service class
27+
inspection.warning.method.should.have.public.access=The method "{0}" should have public access
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
/**
1+
/*
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin;
67

78
import com.intellij.openapi.util.IconLoader;
8-
import javax.swing.*;
9+
import javax.swing.Icon;
910

1011
public class MagentoIcons {
11-
public static final Icon WEB_API = IconLoader.getIcon("/icons/webapi.png");
12-
public static final Icon MODULE = IconLoader.getIcon("/icons/module.png");
12+
public static final Icon WEB_API = IconLoader.getIcon("/icons/webapi.png", MagentoIcons.class);
13+
public static final Icon MODULE = IconLoader.getIcon("/icons/module.png", MagentoIcons.class);
1314
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
2222
import com.magento.idea.magento2plugin.inspections.php.util.PhpClassImplementsInterfaceUtil;
2323
import com.magento.idea.magento2plugin.inspections.php.util.PhpClassImplementsNoninterceptableInterfaceUtil;
24+
import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile;
2425
import com.magento.idea.magento2plugin.magento.files.Plugin;
2526
import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass;
2627
import com.magento.idea.magento2plugin.magento.packages.Package;
@@ -166,7 +167,7 @@ private void checkTargetMethod(
166167
ProblemHighlightType.ERROR
167168
);
168169
}
169-
if (!targetMethod.getAccess().toString().equals(Plugin.PUBLIC_ACCESS)) {
170+
if (!targetMethod.getAccess().toString().equals(AbstractPhpFile.PUBLIC_ACCESS)) {
170171
problemsHolder.registerProblem(
171172
pluginMethod.getNameIdentifier(),
172173
inspectionBundle.message("inspection.plugin.error.nonPublicMethod"),

src/com/magento/idea/magento2plugin/inspections/xml/AclResourceXmlInspection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void visitXmlTag(final XmlTag xmlTag) {
5555
problemsHolder.registerProblem(
5656
identifier,
5757
inspectionBundle.message(
58-
"inspection.aclResource.error.idAttributeCanNotBeEmpty",
58+
"inspection.error.idAttributeCanNotBeEmpty",
5959
"id"
6060
),
6161
ProblemHighlightType.WARNING
@@ -80,7 +80,7 @@ public void visitXmlTag(final XmlTag xmlTag) {
8080
problemsHolder.registerProblem(
8181
identifier,
8282
inspectionBundle.message(
83-
"inspection.aclResource.error.missingAttribute",
83+
"inspection.error.missingAttribute",
8484
"title"
8585
),
8686
ProblemHighlightType.WARNING
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.inspections.xml;
7+
8+
import com.intellij.codeInspection.ProblemHighlightType;
9+
import com.intellij.codeInspection.ProblemsHolder;
10+
import com.intellij.codeInspection.XmlSuppressableInspectionTool;
11+
import com.intellij.psi.PsiElementVisitor;
12+
import com.intellij.psi.PsiFile;
13+
import com.intellij.psi.XmlElementVisitor;
14+
import com.intellij.psi.xml.XmlAttribute;
15+
import com.intellij.psi.xml.XmlTag;
16+
import com.jetbrains.php.PhpIndex;
17+
import com.jetbrains.php.lang.psi.elements.Method;
18+
import com.jetbrains.php.lang.psi.elements.PhpClass;
19+
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
20+
import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile;
21+
import com.magento.idea.magento2plugin.magento.files.ModuleWebApiXmlFile;
22+
23+
import java.util.Collection;
24+
25+
import org.jetbrains.annotations.NotNull;
26+
27+
public class WebApiServiceInspection extends XmlSuppressableInspectionTool {
28+
29+
@NotNull
30+
@Override
31+
public PsiElementVisitor buildVisitor(
32+
final @NotNull ProblemsHolder problemsHolder,
33+
final boolean isOnTheFly
34+
) {
35+
return new XmlElementVisitor() {
36+
private final InspectionBundle inspectionBundle = new InspectionBundle();
37+
38+
@Override
39+
public void visitXmlTag(final XmlTag xmlTag) {
40+
final PsiFile file = xmlTag.getContainingFile();
41+
final String filename = file.getName();
42+
if (!filename.equals(ModuleWebApiXmlFile.FILE_NAME)) {
43+
return;
44+
}
45+
46+
if (!xmlTag.getName().equals(ModuleWebApiXmlFile.SERVICE_TAG_NAME)) {
47+
return;
48+
}
49+
50+
//Check whether the class attribute is not empty
51+
final XmlAttribute classAttribute = xmlTag.getAttribute(ModuleWebApiXmlFile.CLASS_ATTR);
52+
if (classAttribute == null) {
53+
return;
54+
}
55+
final String classFqn = classAttribute.getValue();
56+
if (classFqn == null || classFqn.isEmpty()) {
57+
problemsHolder.registerProblem(
58+
classAttribute,
59+
inspectionBundle.message(
60+
"inspection.error.idAttributeCanNotBeEmpty",
61+
ModuleWebApiXmlFile.CLASS_ATTR
62+
),
63+
ProblemHighlightType.WARNING
64+
);
65+
66+
return;
67+
}
68+
69+
//Check whether the class exists
70+
final PhpIndex phpIndex = PhpIndex.getInstance(
71+
problemsHolder.getProject()
72+
);
73+
@NotNull Collection<PhpClass> classes = phpIndex.getClassesByFQN(classFqn);
74+
if (classes.isEmpty()) {
75+
problemsHolder.registerProblem(
76+
classAttribute,
77+
inspectionBundle.message(
78+
"inspection.warning.class.does.not.exist",
79+
classFqn
80+
),
81+
ProblemHighlightType.WARNING
82+
);
83+
}
84+
85+
//Check whether the method attribute is not empty
86+
final XmlAttribute methodAttribute = xmlTag.getAttribute(ModuleWebApiXmlFile.METHOD_ATTR);
87+
if (methodAttribute == null) {
88+
return;
89+
}
90+
final String methodName = methodAttribute.getValue();
91+
if (methodName == null || methodName.isEmpty()) {
92+
problemsHolder.registerProblem(
93+
classAttribute,
94+
inspectionBundle.message(
95+
"inspection.error.idAttributeCanNotBeEmpty",
96+
ModuleWebApiXmlFile.METHOD_ATTR
97+
),
98+
ProblemHighlightType.WARNING
99+
);
100+
101+
return;
102+
}
103+
104+
//Check whether method exists
105+
Method targetMethod = null;
106+
for (PhpClass phpClass: classes) {
107+
for (Method method: phpClass.getMethods()) {
108+
if (method.getName().equals(methodName)) {
109+
targetMethod = method;
110+
}
111+
break;
112+
}
113+
}
114+
if (targetMethod == null) {
115+
problemsHolder.registerProblem(
116+
methodAttribute,
117+
inspectionBundle.message(
118+
"inspection.warning.method.does.not.exist",
119+
methodName
120+
),
121+
ProblemHighlightType.WARNING
122+
);
123+
return;
124+
}
125+
126+
//API method should have public access
127+
if (targetMethod.getAccess() != null && !targetMethod.getAccess().toString()
128+
.equals(AbstractPhpFile.PUBLIC_ACCESS)) {
129+
problemsHolder.registerProblem(
130+
methodAttribute,
131+
inspectionBundle.message(
132+
"inspection.warning.method.should.have.public.access",
133+
methodName
134+
),
135+
ProblemHighlightType.WARNING
136+
);
137+
}
138+
}
139+
};
140+
}
141+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
public abstract class AbstractPhpFile implements ModuleFileInterface {
1414

1515
public static final String FILE_EXTENSION = "php";
16+
17+
public static final String PUBLIC_ACCESS = "public";
18+
1619
private final String moduleName;
1720
private final String className;
1821
private NamespaceBuilder namespaceBuilder;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public final class ModuleWebApiXmlFile implements ModuleFileInterface {
1313
public static final String FILE_NAME = "webapi.xml";
1414
public static final String TEMPLATE = "Magento Web Api XML";
1515
public static final String DECLARATION_TEMPLATE = "Magento Web Api Declaration";
16+
public static final String SERVICE_TAG_NAME = "service";
17+
public static final String CLASS_ATTR = "class";
18+
public static final String METHOD_ATTR = "method";
1619

1720
@Override
1821
public String getFileName() {

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-
//allowed methods access type
28-
public static final String PUBLIC_ACCESS = "public";
29-
3027
private String fileName;
3128

3229
/**

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

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

88
import com.jetbrains.php.lang.psi.elements.Method;
9-
import com.magento.idea.magento2plugin.magento.files.Plugin;
9+
import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile;
1010
import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass;
1111

1212
public final class IsPluginAllowedForMethodUtil {
@@ -30,6 +30,6 @@ public static boolean check(final Method targetMethod) {
3030
if (targetMethod.isStatic()) {
3131
return false;
3232
}
33-
return targetMethod.getAccess().toString().equals(Plugin.PUBLIC_ACCESS);
33+
return targetMethod.getAccess().toString().equals(AbstractPhpFile.PUBLIC_ACCESS);
3434
}
3535
}

0 commit comments

Comments
 (0)