Skip to content

Commit 519311b

Browse files
Code refactoring after merging
1 parent 3722101 commit 519311b

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

resources/META-INF/plugin.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@
207207

208208
<localInspection language="XML" groupPath="XML"
209209
shortName="WebApiServiceInspection"
210-
displayName="Inspection for the Web API XML service declaration"
211-
groupName="Magento 2"
210+
bundle="magento2.inspection" key="inspection.displayName.WebApiServiceInspection"
211+
groupBundle="magento2.inspection" groupKey="inspection.group.name"
212212
enabledByDefault="true" level="WARNING"
213213
implementationClass="com.magento.idea.magento2plugin.inspections.xml.WebApiServiceInspection"/>
214214

resources/magento2/inspection.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ inspection.displayName.PluginDeclarationInspection=Duplicated Plugin Usage in di
66
inspection.displayName.CacheableFalseInDefaultLayoutInspection=Inspection for disabled cache site-wide
77
inspection.displayName.ModuleDeclarationInModuleXmlInspection=Inspection for the Module declaration in the `etc/module.xml` file
88
inspection.displayName.AclResourceXmlInspection=Inspection for the Title XML required attribute in the `etc/acl.xml` file
9+
inspection.displayName.WebApiServiceInspection=Inspection for the Web API XML service declaration
910
inspection.displayName.InvalidDiTypeInspection=Invalid type configuration in the `etc/di.xml` file
1011
inspection.plugin.duplicateInSameFile=The plugin name already used in this file. For more details see Inspection Description.
1112
inspection.plugin.duplicateInOtherPlaces=The plugin name "{0}" for targeted "{1}" class is already used in the module "{2}" ({3} scope). For more details see Inspection Description.

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import com.jetbrains.php.lang.psi.elements.Method;
1818
import com.jetbrains.php.lang.psi.elements.PhpClass;
1919
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
20+
import com.magento.idea.magento2plugin.inspections.validator.InspectionValidator;
21+
import com.magento.idea.magento2plugin.inspections.validator.NotEmptyValidator;
22+
import com.magento.idea.magento2plugin.inspections.validator.PhpClassExistenceValidator;
2023
import com.magento.idea.magento2plugin.inspections.xml.fix.MethodNotPublicAccessQuickFix;
2124
import com.magento.idea.magento2plugin.magento.files.ModuleWebApiXmlFile;
2225
import java.util.Collection;
@@ -33,7 +36,12 @@ public PsiElementVisitor buildVisitor(
3336
final boolean isOnTheFly
3437
) {
3538
return new XmlElementVisitor() {
39+
3640
private final InspectionBundle inspectionBundle = new InspectionBundle();
41+
// Inspection validators
42+
private final InspectionValidator notEmptyValidator = new NotEmptyValidator();
43+
private final InspectionValidator phpClassExistenceValidator =
44+
new PhpClassExistenceValidator(problemsHolder.getProject());
3745

3846
@Override
3947
public void visitXmlTag(final XmlTag xmlTag) {
@@ -55,7 +63,8 @@ public void visitXmlTag(final XmlTag xmlTag) {
5563
return;
5664
}
5765
final String classFqn = classAttribute.getValue();
58-
if (classFqn == null || classFqn.isEmpty()) {
66+
67+
if (!notEmptyValidator.validate(classFqn)) {
5968
problemsHolder.registerProblem(
6069
classAttribute,
6170
inspectionBundle.message(
@@ -68,14 +77,7 @@ public void visitXmlTag(final XmlTag xmlTag) {
6877
return;
6978
}
7079

71-
//Check whether the class exists
72-
final PhpIndex phpIndex = PhpIndex.getInstance(
73-
problemsHolder.getProject()
74-
);
75-
@NotNull final Collection<PhpClass> classes = phpIndex.getClassesByFQN(classFqn);
76-
@NotNull final Collection<PhpClass> interfaces = phpIndex
77-
.getInterfacesByFQN(classFqn);
78-
if (classes.isEmpty() && interfaces.isEmpty()) {
80+
if (!phpClassExistenceValidator.validate(classFqn)) {
7981
problemsHolder.registerProblem(
8082
classAttribute,
8183
inspectionBundle.message(
@@ -95,7 +97,8 @@ public void visitXmlTag(final XmlTag xmlTag) {
9597
return;
9698
}
9799
final String methodName = methodAttribute.getValue();
98-
if (methodName == null || methodName.isEmpty()) {
100+
101+
if (!notEmptyValidator.validate(methodName)) {
99102
problemsHolder.registerProblem(
100103
classAttribute,
101104
inspectionBundle.message(
@@ -109,10 +112,16 @@ public void visitXmlTag(final XmlTag xmlTag) {
109112
}
110113

111114
//Check whether method exists
115+
final PhpIndex phpIndex = PhpIndex.getInstance(problemsHolder.getProject());
116+
final @NotNull Collection<PhpClass> classes = phpIndex.getClassesByFQN(classFqn);
117+
final @NotNull Collection<PhpClass> interfaces = phpIndex
118+
.getInterfacesByFQN(classFqn);
119+
112120
Method targetMethod = findTargetMethod(classes, methodName);
113121
if (targetMethod == null) {
114122
targetMethod = findTargetMethod(interfaces, methodName);
115123
}
124+
116125
if (targetMethod == null && methodAttribute.getValueElement() != null) {
117126
problemsHolder.registerProblem(
118127
methodAttribute.getValueElement(),
@@ -127,7 +136,9 @@ public void visitXmlTag(final XmlTag xmlTag) {
127136
}
128137

129138
//API method should have public access
130-
if (targetMethod.getAccess() != null && !targetMethod.getAccess().isPublic()) {
139+
if (targetMethod != null
140+
&& targetMethod.getAccess() != null
141+
&& !targetMethod.getAccess().isPublic()) {
131142
problemsHolder.registerProblem(
132143
methodAttribute,
133144
inspectionBundle.message(
@@ -140,8 +151,7 @@ public void visitXmlTag(final XmlTag xmlTag) {
140151
}
141152
}
142153

143-
@Nullable
144-
private Method findTargetMethod(
154+
private @Nullable Method findTargetMethod(
145155
final Collection<PhpClass> classes,
146156
final String methodName
147157
) {

0 commit comments

Comments
 (0)