Skip to content

Commit

Permalink
php class resolver - fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfilatov committed Jan 11, 2016
1 parent 1725da4 commit 3c1a6db
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 38 deletions.
2 changes: 1 addition & 1 deletion META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin version="2" url="https://github.com/maxfilatov/phpuaca/">
<id>com.phpuaca</id>
<name>PHPUnit Autocomplete Assistant</name>
<version>1.3.0-unstable</version>
<version>1.3.0-unstable-5</version>
<vendor url="https://github.com/maxfilatov/phpuaca/">Max Filatov</vendor>

<description><![CDATA[
Expand Down
4 changes: 2 additions & 2 deletions src/com/phpuaca/completion/PHPUnitTypeProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected String getTypeForMockBuilder(@NotNull MethodReference methodReference)
return null;
}

PhpClass phpClass = (new PhpClassResolver()).resolveByParameterListContainingClassReference(mockBuilderMethodReference.getParameterList());
PhpClass phpClass = (new PhpClassResolver()).resolveByMethodReferenceContainingParameterListWithClassReference(mockBuilderMethodReference);
if (phpClass == null) {
return null;
}
Expand All @@ -96,7 +96,7 @@ protected String getTypeForMockBuilder(@NotNull MethodReference methodReference)

@Nullable
protected String getTypeForTestCase(@NotNull MethodReference methodReference) {
PhpClass phpClass = (new PhpClassResolver()).resolveByParameterListContainingClassReference(methodReference.getParameterList());
PhpClass phpClass = (new PhpClassResolver()).resolveByMethodReferenceContainingParameterListWithClassReference(methodReference);
if (phpClass == null) {
return null;
}
Expand Down
8 changes: 5 additions & 3 deletions src/com/phpuaca/completion/ProphecyTypeProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.*;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
import com.phpuaca.util.PhpClassAdapter;
import com.phpuaca.util.PhpClassResolver;
import com.phpuaca.util.PhpMethodResolver;
Expand Down Expand Up @@ -77,8 +80,7 @@ public Collection<? extends PhpNamedElement> getBySignature(String s, Project pr

@Nullable
protected String getTypeForProphesize(@NotNull MethodReference methodReference) {
ParameterList parameterList = methodReference.getParameterList();
PhpClass phpClass = (new PhpClassResolver()).resolveByParameterListContainingClassReference(parameterList);
PhpClass phpClass = (new PhpClassResolver()).resolveByMethodReferenceContainingParameterListWithClassReference(methodReference);
if (phpClass == null) {
return null;
}
Expand Down
1 change: 0 additions & 1 deletion src/com/phpuaca/filter/Filter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.phpuaca.filter;

import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocMethod;
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocProperty;
import com.jetbrains.php.lang.psi.elements.Field;
import com.jetbrains.php.lang.psi.elements.Method;
Expand Down
24 changes: 5 additions & 19 deletions src/com/phpuaca/filter/MethodMockFilter.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
package com.phpuaca.filter;

import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.psi.elements.*;
import com.phpuaca.filter.util.ClassFinder;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpModifier;
import com.phpuaca.util.PhpClassResolver;

public class MethodMockFilter extends Filter {

public MethodMockFilter(FilterContext context) {
super(context);

PhpClass phpClass = null;
ParameterList parameterList = context.getMethodReference().getParameterList();
ClassConstantReference classConstantReference = PsiTreeUtil.getChildOfType(parameterList, ClassConstantReference.class);
if (classConstantReference == null) {
// MethodMock::callProtectedMethod($Variable, 'doSomething');
Variable variable = PsiTreeUtil.getChildOfType(parameterList, Variable.class);
if (variable != null) {
ClassFinder.Result classFinderResult = (new ClassFinder()).find(variable);
if (classFinderResult != null) {
phpClass = classFinderResult.getPhpClass();
}
}
} else {
phpClass = (new PhpClassResolver()).resolveByClassConstantReference(classConstantReference);
}

MethodReference methodReference = context.getMethodReference();
PhpClass phpClass = (new PhpClassResolver()).resolveByMethodReferenceContainingParameterListWithClassReference(methodReference);
if (phpClass == null) {
return;
}
Expand Down
4 changes: 1 addition & 3 deletions src/com/phpuaca/filter/util/ClassFinder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.phpuaca.filter.util;

import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.ParameterList;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.Variable;
import com.phpuaca.filter.FilterConfigItem;
Expand Down Expand Up @@ -35,8 +34,7 @@ public Result find(@NotNull MethodReference methodReference) {
return null;
}

ParameterList parameterList = mockBuilderMethodReference.getParameterList();
PhpClass phpClass = (new PhpClassResolver()).resolveByParameterListContainingClassReference(parameterList);
PhpClass phpClass = (new PhpClassResolver()).resolveByMethodReferenceContainingParameterListWithClassReference(mockBuilderMethodReference);
if (phpClass == null) {
return null;
}
Expand Down
47 changes: 38 additions & 9 deletions src/com/phpuaca/util/PhpClassResolver.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.phpuaca.util;

import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.*;
import com.phpuaca.filter.util.ClassFinder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -43,17 +45,44 @@ public PhpClass resolveByClassStringLiteralExpression(@NotNull StringLiteralExpr
}

@Nullable
public PhpClass resolveByParameterListContainingClassReference(@Nullable ParameterList parameterList) {
ClassConstantReference classConstantReference = PsiTreeUtil.getChildOfType(parameterList, ClassConstantReference.class);
if (classConstantReference != null) {
return resolveByClassConstantReference(classConstantReference);
} else {
StringLiteralExpression stringLiteralExpression = PsiTreeUtil.getChildOfType(parameterList, StringLiteralExpression.class);
if (stringLiteralExpression != null) {
return resolveByClassStringLiteralExpression(stringLiteralExpression);
}
public PhpClass resolveByParameterListContainingClassReference(@NotNull ParameterList parameterList) {
PsiElement[] parameters = parameterList.getParameters();
if (parameters.length == 0) {
return null;
}

if (parameters[0] instanceof ClassConstantReference) {
return resolveByClassConstantReference((ClassConstantReference) parameters[0]);
}

if (parameters[0] instanceof StringLiteralExpression) {
return resolveByClassStringLiteralExpression((StringLiteralExpression) parameters[0]);
}

if (parameters[0] instanceof Variable) {
return resolveByVariable((Variable) parameters[0]);
}

return null;
}

@Nullable
public PhpClass resolveByVariable(@NotNull Variable variable) {
ClassFinder.Result classFinderResult = (new ClassFinder()).find(variable);
if (classFinderResult == null) {
return null;
}

return classFinderResult.getPhpClass();
}

@Nullable
public PhpClass resolveByMethodReferenceContainingParameterListWithClassReference(@NotNull MethodReference methodReference) {
ParameterList parameterList = methodReference.getParameterList();
if (parameterList == null) {
return null;
}

return resolveByParameterListContainingClassReference(parameterList);
}
}

0 comments on commit 3c1a6db

Please sign in to comment.