Skip to content

Commit

Permalink
#12: add warning annotation for methods not described in mock
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfilatov committed Jan 7, 2016
1 parent ea7162b commit 42e2e41
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
10 changes: 7 additions & 3 deletions src/com/phpuaca/annotator/StringAnnotator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.refactoring.PhpNameUtil;
import com.phpuaca.filter.Filter;
Expand All @@ -22,10 +23,13 @@ public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder a
PhpClass phpClass = filter.getPhpClass();
if (phpClass != null) {
String name = PhpNameUtil.unquote(psiElement.getText());
if (phpClass.findMethodByName(name) == null && phpClass.findFieldByName(name, false) == null) {
TextRange textRange = psiElement.getTextRange();
TextRange annotationTextRange = new TextRange(textRange.getStartOffset() + 1, textRange.getEndOffset() - 1);
Method method = phpClass.findMethodByName(name);
TextRange textRange = psiElement.getTextRange();
TextRange annotationTextRange = new TextRange(textRange.getStartOffset() + 1, textRange.getEndOffset() - 1);
if (method == null && phpClass.findFieldByName(name, false) == null) {
annotationHolder.createWarningAnnotation(annotationTextRange, "Method '" + name + "' not found in class " + phpClass.getName());
} else if (!filter.isMethodAllowed(method)) {
annotationHolder.createWarningAnnotation(annotationTextRange, "Method '" + name + "' is not allowed to use here");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/phpuaca/completion/StringCompletionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected List<LookupElement> getLookupElements(@NotNull Filter filter) {

if (phpClass != null) {
for (Method method : phpClass.getMethods()) {
if (filter.isMethodAllowed(method)) {
if (filter.isMethodAllowed(method) && !filter.isMethodDescribed(method)) {
list.add(new PhpLookupElement(method));
}
}
Expand Down
32 changes: 24 additions & 8 deletions src/com/phpuaca/filter/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpModifier;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -15,18 +16,15 @@ abstract public class Filter {
private boolean isMethodsAllowed = false;
private boolean isFieldsAllowed = false;

private List<String> allowedMethods;
private List<String> allowedFields;
private List<String> allowedModifiers;
private List<String> disallowedMethods;
private List<String> allowedMethods = new ArrayList<String>();
private List<String> allowedFields = new ArrayList<String>();
private List<String> allowedModifiers = new ArrayList<String>();
private List<String> disallowedMethods = new ArrayList<String>();
private List<String> describedMethods = new ArrayList<String>();

private PhpClass phpClass;

public Filter(FilterContext context) {
allowedMethods = new ArrayList<String>();
allowedFields = new ArrayList<String>();
allowedModifiers = new ArrayList<String>();
disallowedMethods = new ArrayList<String>();
}

public void allowMethod(String methodName) {
Expand All @@ -41,6 +39,10 @@ public void disallowMethod(String methodName) {
disallowedMethods.add(methodName);
}

public void describeMethod(String methodName) {
describedMethods.add(methodName);
}

public void allowField(String fieldName) {
allowFields();
allowedFields.add(fieldName);
Expand Down Expand Up @@ -70,6 +72,12 @@ public void disallowMethods(List<String> methodNames) {
}
}

public void describeMethods(List<String> methodNames) {
for (String methodName : methodNames) {
describeMethod(methodName);
}
}

public void allowFields() {
isFieldsAllowed = true;
}
Expand All @@ -82,6 +90,14 @@ public boolean isMethodAllowed(Method method) {
return !(method instanceof PhpDocMethod) && isMethodAllowed(method.getName()) && isModifierAllowed(method.getModifier());
}

public boolean isMethodDescribed(String methodName) {
return describedMethods.contains(methodName);
}

public boolean isMethodDescribed(@NotNull Method method) {
return isMethodDescribed(method.getName());
}

protected boolean isFieldAllowed(String fieldName) {
return isFieldsAllowed && (allowedFields.isEmpty() || allowedFields.contains(fieldName));
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/phpuaca/filter/MockBuilderFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public MockBuilderFilter(FilterContext context) {
if (parameterList != null) {
PhpArrayParameter phpArrayParameter = PhpArrayParameter.create(parameterList, context.getFilterConfigItem().getParameterNumber());
if (phpArrayParameter != null) {
disallowMethods(phpArrayParameter.getValues());
describeMethods(phpArrayParameter.getValues());
}
}
}
Expand Down

0 comments on commit 42e2e41

Please sign in to comment.