Skip to content

Commit 42e2e41

Browse files
committed
#12: add warning annotation for methods not described in mock
1 parent ea7162b commit 42e2e41

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

src/com/phpuaca/annotator/StringAnnotator.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.intellij.lang.annotation.Annotator;
55
import com.intellij.openapi.util.TextRange;
66
import com.intellij.psi.PsiElement;
7+
import com.jetbrains.php.lang.psi.elements.Method;
78
import com.jetbrains.php.lang.psi.elements.PhpClass;
89
import com.jetbrains.php.refactoring.PhpNameUtil;
910
import com.phpuaca.filter.Filter;
@@ -22,10 +23,13 @@ public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder a
2223
PhpClass phpClass = filter.getPhpClass();
2324
if (phpClass != null) {
2425
String name = PhpNameUtil.unquote(psiElement.getText());
25-
if (phpClass.findMethodByName(name) == null && phpClass.findFieldByName(name, false) == null) {
26-
TextRange textRange = psiElement.getTextRange();
27-
TextRange annotationTextRange = new TextRange(textRange.getStartOffset() + 1, textRange.getEndOffset() - 1);
26+
Method method = phpClass.findMethodByName(name);
27+
TextRange textRange = psiElement.getTextRange();
28+
TextRange annotationTextRange = new TextRange(textRange.getStartOffset() + 1, textRange.getEndOffset() - 1);
29+
if (method == null && phpClass.findFieldByName(name, false) == null) {
2830
annotationHolder.createWarningAnnotation(annotationTextRange, "Method '" + name + "' not found in class " + phpClass.getName());
31+
} else if (!filter.isMethodAllowed(method)) {
32+
annotationHolder.createWarningAnnotation(annotationTextRange, "Method '" + name + "' is not allowed to use here");
2933
}
3034
}
3135
}

src/com/phpuaca/completion/StringCompletionProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected List<LookupElement> getLookupElements(@NotNull Filter filter) {
3737

3838
if (phpClass != null) {
3939
for (Method method : phpClass.getMethods()) {
40-
if (filter.isMethodAllowed(method)) {
40+
if (filter.isMethodAllowed(method) && !filter.isMethodDescribed(method)) {
4141
list.add(new PhpLookupElement(method));
4242
}
4343
}

src/com/phpuaca/filter/Filter.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.jetbrains.php.lang.psi.elements.Method;
77
import com.jetbrains.php.lang.psi.elements.PhpClass;
88
import com.jetbrains.php.lang.psi.elements.PhpModifier;
9+
import org.jetbrains.annotations.NotNull;
910

1011
import java.util.ArrayList;
1112
import java.util.List;
@@ -15,18 +16,15 @@ abstract public class Filter {
1516
private boolean isMethodsAllowed = false;
1617
private boolean isFieldsAllowed = false;
1718

18-
private List<String> allowedMethods;
19-
private List<String> allowedFields;
20-
private List<String> allowedModifiers;
21-
private List<String> disallowedMethods;
19+
private List<String> allowedMethods = new ArrayList<String>();
20+
private List<String> allowedFields = new ArrayList<String>();
21+
private List<String> allowedModifiers = new ArrayList<String>();
22+
private List<String> disallowedMethods = new ArrayList<String>();
23+
private List<String> describedMethods = new ArrayList<String>();
2224

2325
private PhpClass phpClass;
2426

2527
public Filter(FilterContext context) {
26-
allowedMethods = new ArrayList<String>();
27-
allowedFields = new ArrayList<String>();
28-
allowedModifiers = new ArrayList<String>();
29-
disallowedMethods = new ArrayList<String>();
3028
}
3129

3230
public void allowMethod(String methodName) {
@@ -41,6 +39,10 @@ public void disallowMethod(String methodName) {
4139
disallowedMethods.add(methodName);
4240
}
4341

42+
public void describeMethod(String methodName) {
43+
describedMethods.add(methodName);
44+
}
45+
4446
public void allowField(String fieldName) {
4547
allowFields();
4648
allowedFields.add(fieldName);
@@ -70,6 +72,12 @@ public void disallowMethods(List<String> methodNames) {
7072
}
7173
}
7274

75+
public void describeMethods(List<String> methodNames) {
76+
for (String methodName : methodNames) {
77+
describeMethod(methodName);
78+
}
79+
}
80+
7381
public void allowFields() {
7482
isFieldsAllowed = true;
7583
}
@@ -82,6 +90,14 @@ public boolean isMethodAllowed(Method method) {
8290
return !(method instanceof PhpDocMethod) && isMethodAllowed(method.getName()) && isModifierAllowed(method.getModifier());
8391
}
8492

93+
public boolean isMethodDescribed(String methodName) {
94+
return describedMethods.contains(methodName);
95+
}
96+
97+
public boolean isMethodDescribed(@NotNull Method method) {
98+
return isMethodDescribed(method.getName());
99+
}
100+
85101
protected boolean isFieldAllowed(String fieldName) {
86102
return isFieldsAllowed && (allowedFields.isEmpty() || allowedFields.contains(fieldName));
87103
}

src/com/phpuaca/filter/MockBuilderFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public MockBuilderFilter(FilterContext context) {
3131
if (parameterList != null) {
3232
PhpArrayParameter phpArrayParameter = PhpArrayParameter.create(parameterList, context.getFilterConfigItem().getParameterNumber());
3333
if (phpArrayParameter != null) {
34-
disallowMethods(phpArrayParameter.getValues());
34+
describeMethods(phpArrayParameter.getValues());
3535
}
3636
}
3737
}

0 commit comments

Comments
 (0)