Skip to content

Commit 5800333

Browse files
author
Vitaliy Boyko
committed
Added tests
1 parent 445727c commit 5800333

File tree

11 files changed

+241
-20
lines changed

11 files changed

+241
-20
lines changed

resources/magento2/inspection.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ inspection.error.idAttributeCanNotBeEmpty=Attribute value "{0}" can not be empty
2525
inspection.warning.class.does.not.exist=The class "{0}" doesn't exist
2626
inspection.warning.method.does.not.exist=The method "{0}" doesn't exist in the service class
2727
inspection.warning.method.should.have.public.access=The method "{0}" should have public access
28+
inspection.warning.method.should.have.public.access.fix=Change the method access

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

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
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.magento.files.AbstractPhpFile;
20+
import com.magento.idea.magento2plugin.inspections.xml.fix.MethodNotPublicAccessQuickFix;
2121
import com.magento.idea.magento2plugin.magento.files.ModuleWebApiXmlFile;
22-
2322
import java.util.Collection;
24-
2523
import org.jetbrains.annotations.NotNull;
24+
import org.jetbrains.annotations.Nullable;
2625

26+
@SuppressWarnings({"PMD.ExcessiveMethodLength", "PMD.NPathComplexity"})
2727
public class WebApiServiceInspection extends XmlSuppressableInspectionTool {
2828

2929
@NotNull
@@ -48,7 +48,9 @@ public void visitXmlTag(final XmlTag xmlTag) {
4848
}
4949

5050
//Check whether the class attribute is not empty
51-
final XmlAttribute classAttribute = xmlTag.getAttribute(ModuleWebApiXmlFile.CLASS_ATTR);
51+
final XmlAttribute classAttribute = xmlTag.getAttribute(
52+
ModuleWebApiXmlFile.CLASS_ATTR
53+
);
5254
if (classAttribute == null) {
5355
return;
5456
}
@@ -70,8 +72,9 @@ public void visitXmlTag(final XmlTag xmlTag) {
7072
final PhpIndex phpIndex = PhpIndex.getInstance(
7173
problemsHolder.getProject()
7274
);
73-
@NotNull Collection<PhpClass> classes = phpIndex.getClassesByFQN(classFqn);
74-
if (classes.isEmpty()) {
75+
@NotNull final Collection<PhpClass> classes = phpIndex.getClassesByFQN(classFqn);
76+
@NotNull final Collection<PhpClass> interfaces = phpIndex.getInterfacesByFQN(classFqn);
77+
if (classes.isEmpty() && interfaces.isEmpty()) {
7578
problemsHolder.registerProblem(
7679
classAttribute,
7780
inspectionBundle.message(
@@ -80,10 +83,13 @@ public void visitXmlTag(final XmlTag xmlTag) {
8083
),
8184
ProblemHighlightType.WARNING
8285
);
86+
return;
8387
}
8488

8589
//Check whether the method attribute is not empty
86-
final XmlAttribute methodAttribute = xmlTag.getAttribute(ModuleWebApiXmlFile.METHOD_ATTR);
90+
final XmlAttribute methodAttribute = xmlTag.getAttribute(
91+
ModuleWebApiXmlFile.METHOD_ATTR
92+
);
8793
if (methodAttribute == null) {
8894
return;
8995
}
@@ -102,40 +108,48 @@ public void visitXmlTag(final XmlTag xmlTag) {
102108
}
103109

104110
//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-
}
111+
Method targetMethod = findTargetMethod(classes, methodName);
114112
if (targetMethod == null) {
113+
targetMethod = findTargetMethod(interfaces, methodName);
114+
}
115+
if (targetMethod == null && methodAttribute.getValueElement() != null) {
115116
problemsHolder.registerProblem(
116-
methodAttribute,
117+
methodAttribute.getValueElement(),
117118
inspectionBundle.message(
118119
"inspection.warning.method.does.not.exist",
119120
methodName
120121
),
121122
ProblemHighlightType.WARNING
122123
);
124+
123125
return;
124126
}
125127

126128
//API method should have public access
127-
if (targetMethod.getAccess() != null && !targetMethod.getAccess().toString()
128-
.equals(AbstractPhpFile.PUBLIC_ACCESS)) {
129+
if (targetMethod.getAccess() != null && !targetMethod.getAccess().isPublic()) {
129130
problemsHolder.registerProblem(
130131
methodAttribute,
131132
inspectionBundle.message(
132133
"inspection.warning.method.should.have.public.access",
133134
methodName
134135
),
135-
ProblemHighlightType.WARNING
136+
ProblemHighlightType.WARNING,
137+
new MethodNotPublicAccessQuickFix(targetMethod)
136138
);
137139
}
138140
}
141+
142+
@Nullable
143+
private Method findTargetMethod(final Collection<PhpClass> classes, final String methodName) {
144+
for (final PhpClass phpClass : classes) {
145+
for (final Method method : phpClass.getMethods()) {
146+
if (method.getName().equals(methodName)) {
147+
return method;
148+
}
149+
}
150+
}
151+
return null;
152+
}
139153
};
140154
}
141155
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.fix;
7+
8+
import com.intellij.codeInspection.LocalQuickFix;
9+
import com.intellij.codeInspection.ProblemDescriptor;
10+
import com.intellij.openapi.command.WriteCommandAction;
11+
import com.intellij.openapi.project.Project;
12+
import com.jetbrains.php.lang.inspections.quickfix.PhpChangeMethodModifiersQuickFix;
13+
import com.jetbrains.php.lang.psi.elements.Method;
14+
import com.jetbrains.php.lang.psi.elements.PhpModifier;
15+
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
16+
import org.jetbrains.annotations.NotNull;
17+
18+
public class MethodNotPublicAccessQuickFix implements LocalQuickFix {
19+
private InspectionBundle inspectionBundle = new InspectionBundle();
20+
private Method method;
21+
22+
public MethodNotPublicAccessQuickFix(Method method) {
23+
this.method = method;
24+
}
25+
26+
@NotNull
27+
@Override
28+
public String getFamilyName() {
29+
return inspectionBundle.message("inspection.warning.method.should.have.public.access.fix");
30+
}
31+
32+
@Override
33+
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
34+
PhpChangeMethodModifiersQuickFix.changeMethodModifier(
35+
this.method,
36+
PhpModifier.PUBLIC_IMPLEMENTED_DYNAMIC
37+
);
38+
}
39+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
3+
<route method="GET" url="/V1/my-awesome-route">
4+
<service class="Magento\Catalog\Api\ProductRepositoryInterface" method="some" />
5+
</route>
6+
</routes>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
3+
<route method="GET" url="/V1/my-awesome-route">
4+
<service class="Magento\Catalog\Api\ProductRepositoryInterface" method="save" />
5+
</route>
6+
</routes>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
3+
<route method="GET" url="/V1/my-awesome-route">
4+
<service class="Not\Existent\Class" method="some" />
5+
</route>
6+
</routes>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
3+
<route method="GET" url="/V1/my-awesome-route">
4+
<service class="Magento\Catalog\Api\ProductRepositoryInterface" method="notExistent" />
5+
</route>
6+
</routes>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
3+
<route method="GET" url="/V1/my-awesome-route">
4+
<service class="Foo\Bar\Service\SimpleService" method="myProtected" />
5+
</route>
6+
</routes>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
3+
<route method="GET" url="/V1/my-awesome-route">
4+
<service class="Foo\Bar\Service\SimpleService" method="execute" />
5+
</route>
6+
</routes>

testData/project/magento2/app/code/Foo/Bar/Service/SimpleService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ public function execute(int $param1, string $param2)
77
{
88

99
}
10+
11+
protected function myProtected(int $param1, string $param2)
12+
{
13+
14+
}
1015
}

0 commit comments

Comments
 (0)