Skip to content

Commit

Permalink
some refactoring + reformat code + optimize imports
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfilatov committed Jan 6, 2016
1 parent 8934092 commit eef6044
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 116 deletions.
50 changes: 26 additions & 24 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<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>
<vendor url="https://github.com/maxfilatov/phpuaca/">Max Filatov</vendor>
<id>com.phpuaca</id>
<name>PHPUnit Autocomplete Assistant</name>
<version>1.3.0-unstable</version>
<vendor url="https://github.com/maxfilatov/phpuaca/">Max Filatov</vendor>

<description><![CDATA[
<description><![CDATA[
<p>PhpStorm plugin to provide smart autocomplete for mocked class methods.</p>
<p>Features are available for method definition arguments of these PHPUnit methods:</p>
<ul style="margin-bottom: 8px;">
Expand All @@ -21,7 +21,7 @@
<p><img src="https://raw.githubusercontent.com/maxfilatov/phpuaca/master/img/pim.png" alt="" width="346" height="340" /></p>
]]></description>

<change-notes><![CDATA[
<change-notes><![CDATA[
<h3>1.2</h3>
<ul>
<li>Fixed false activation of plugin in classes with magic methods defined in PHPDoc blocks.</li>
Expand All @@ -37,29 +37,31 @@
<li>Added support for methods <tt>PHPUnit_Framework_MockObject_MockBuilder::setMethods</tt> and <tt>PHPUnit_Framework_MockObject_Builder_InvocationMocker::method</tt>.</li>
</ul>
]]>
</change-notes>
</change-notes>

<idea-version since-build="133.0"/>
<idea-version since-build="133.0"/>

<depends>com.jetbrains.php</depends>
<depends>com.intellij.modules.platform</depends>
<depends>com.jetbrains.php</depends>
<depends>com.intellij.modules.platform</depends>

<extensions defaultExtensionNs="com.intellij">
<annotator language="PHP" implementationClass="com.phpuaca.annotator.StringAnnotator"/>
<completion.confidence language="PHP" implementationClass="com.phpuaca.completion.StringCompletionConfidence" id="asTrue" order="first"/>
<completion.contributor language="PHP" implementationClass="com.phpuaca.completion.StringCompletionContributor"/>
<psi.referenceContributor language="PHP" implementation="com.phpuaca.reference.StringReferenceContributor"/>
<php.typeProvider2 implementation="com.phpuaca.completion.PHPUnitTypeProvider"/>
<php.typeProvider2 implementation="com.phpuaca.completion.ProphecyTypeProvider"/>
</extensions>
<extensions defaultExtensionNs="com.intellij">
<annotator language="PHP" implementationClass="com.phpuaca.annotator.StringAnnotator"/>
<completion.confidence language="PHP" implementationClass="com.phpuaca.completion.StringCompletionConfidence"
id="asTrue" order="first"/>
<completion.contributor language="PHP"
implementationClass="com.phpuaca.completion.StringCompletionContributor"/>
<psi.referenceContributor language="PHP" implementation="com.phpuaca.reference.StringReferenceContributor"/>
<php.typeProvider2 implementation="com.phpuaca.completion.PHPUnitTypeProvider"/>
<php.typeProvider2 implementation="com.phpuaca.completion.ProphecyTypeProvider"/>
</extensions>

<application-components>
</application-components>
<application-components>
</application-components>

<project-components>
</project-components>
<project-components>
</project-components>

<actions>
</actions>
<actions>
</actions>

</idea-plugin>
16 changes: 10 additions & 6 deletions src/com/phpuaca/completion/BaseTypeProvider.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.phpuaca.completion;

import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.PhpIndex;
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.resolve.types.PhpTypeProvider2;
import com.phpuaca.util.PhpClassAdapter;
Expand All @@ -13,20 +16,21 @@

abstract public class BaseTypeProvider implements PhpTypeProvider2 {

protected Collection<PhpClass> getPhpInterfaceCollection(@NotNull Project project, String FQN)
{
protected boolean isAvailable(@NotNull PsiElement psiElement) {
return (psiElement instanceof MethodReference) && !DumbService.getInstance(psiElement.getProject()).isDumb();
}

protected Collection<PhpClass> getPhpInterfaceCollection(@NotNull Project project, String FQN) {
return PhpIndex.getInstance(project).getInterfacesByFQN(FQN);
}

@NotNull
protected Collection<PhpClass> getPhpClassCollection(@NotNull Project project, String FQN)
{
protected Collection<PhpClass> getPhpClassCollection(@NotNull Project project, String FQN) {
return PhpIndex.getInstance(project).getClassesByFQN(FQN);
}

@Nullable
protected PhpClassAdapter getPhpClassAdapterForMethod(@NotNull Method method)
{
protected PhpClassAdapter getPhpClassAdapterForMethod(@NotNull Method method) {
PhpClass phpClass = method.getContainingClass();
if (phpClass == null) {
return null;
Expand Down
18 changes: 5 additions & 13 deletions src/com/phpuaca/completion/PHPUnitTypeProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public char getKey() {
@Nullable
@Override
public String getType(PsiElement psiElement) {
if (!(psiElement instanceof MethodReference)) {
if (!isAvailable(psiElement)) {
return null;
}

Expand Down Expand Up @@ -68,24 +68,17 @@ public String getType(PsiElement psiElement) {
@Override
public Collection<? extends PhpNamedElement> getBySignature(String s, Project project) {
Collection<PhpClass> collection = new ArrayList<PhpClass>();
collection.addAll(getMockObjectCollection(project));
collection.addAll(getPhpInterfaceCollection(project, CLASS_PHP_UNIT_MOCK_OBJECT));
collection.addAll(getPhpClassCollection(project, s));
return collection;
}

@NotNull
protected Collection<PhpClass> getMockObjectCollection(@NotNull Project project) {
return getPhpInterfaceCollection(project, CLASS_PHP_UNIT_MOCK_OBJECT);
}

protected boolean isMethodIsMockCreator(@NotNull Method method)
{
protected boolean isMethodIsMockCreator(@NotNull Method method) {
return mockCreatorMethodMap.containsKey(method.getName());
}

@Nullable
protected String getTypeForMockBuilder(@NotNull MethodReference methodReference)
{
protected String getTypeForMockBuilder(@NotNull MethodReference methodReference) {
MethodReference mockBuilderMethodReference = (new PhpMethodChain(methodReference)).findMethodReference("getMockBuilder");
if (mockBuilderMethodReference == null) {
return null;
Expand All @@ -100,8 +93,7 @@ protected String getTypeForMockBuilder(@NotNull MethodReference methodReference)
}

@Nullable
protected String getTypeForTestCase(@NotNull MethodReference methodReference)
{
protected String getTypeForTestCase(@NotNull MethodReference methodReference) {
PhpClass phpClass = (new PhpClassResolver()).resolveByParameterListContainingClassReference(methodReference.getParameterList());
if (phpClass == null) {
return null;
Expand Down
109 changes: 41 additions & 68 deletions src/com/phpuaca/completion/ProphecyTypeProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

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.resolve.types.PhpTypeProvider2;
import com.phpuaca.util.*;
import com.phpuaca.util.PhpClassAdapter;
import com.phpuaca.util.PhpClassResolver;
import com.phpuaca.util.PhpMethodResolver;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

public class ProphecyTypeProvider implements PhpTypeProvider2 {
public class ProphecyTypeProvider extends BaseTypeProvider {

final protected static String TYPE_SEPARATOR = "θ";

final protected static String METHOD_PROPHESIZE = "prophesize";
final protected static String METHOD_REVEAL = "reveal";
Expand All @@ -21,10 +25,6 @@ public class ProphecyTypeProvider implements PhpTypeProvider2 {
final protected static String CLASS_METHOD_PROPHECY = "\\Prophecy\\Prophecy\\MethodProphecy";
final protected static String CLASS_PHP_UNIT_TEST_CASE = "\\PHPUnit_Framework_TestCase";

final protected static String KEY_NONE = "PROPHECY_TYPE_PROVIDER_NONE:";
final protected static String KEY_OBJECT_PROPHECY = "PROPHECY_TYPE_PROVIDER_OBJECT:";
final protected static String KEY_METHOD_PROPHECY = "PROPHECY_TYPE_PROVIDER_METHOD:";

protected HashMap<String, Boolean> mockedMethods = new HashMap<String, Boolean>();

@Override
Expand All @@ -35,7 +35,7 @@ public char getKey() {
@Nullable
@Override
public String getType(PsiElement psiElement) {
if (!(psiElement instanceof MethodReference)) {
if (!isAvailable(psiElement)) {
return null;
}

Expand All @@ -50,94 +50,67 @@ public String getType(PsiElement psiElement) {
if (methodName.equals(METHOD_PROPHESIZE)) {
PhpClassAdapter phpClassAdapter = getPhpClassAdapterForMethod(method);
if (phpClassAdapter != null && (phpClassAdapter.isSubclassOf(CLASS_PHP_UNIT_TEST_CASE) || phpClassAdapter.isSubclassOf(CLASS_PROPHET))) {
ParameterList parameterList = methodReference.getParameterList();
PhpClass phpClass = (new PhpClassResolver()).resolveByParameterListContainingClassReference(parameterList);
if (phpClass != null) {
mockMethods(phpClass.getMethods());
return KEY_OBJECT_PROPHECY + phpClass.getFQN();
}
return getTypeForProphesize(methodReference);
}
} else if (methodName.equals(METHOD_REVEAL) && signature.contains(KEY_OBJECT_PROPHECY)) {
} else if (methodName.equals(METHOD_REVEAL) && signature.contains(TYPE_SEPARATOR)) {
PhpClassAdapter phpClassAdapter = getPhpClassAdapterForMethod(method);
if (phpClassAdapter != null && phpClassAdapter.isSubclassOf(CLASS_OBJECT_PROPHECY)) {
int offsetStart = signature.indexOf(KEY_OBJECT_PROPHECY);
int offsetEnd = signature.indexOf("." + METHOD_REVEAL);
String className = signature.substring(offsetStart + KEY_OBJECT_PROPHECY.length(), offsetEnd);
return KEY_NONE + className;
return getTypeForReveal(methodReference);
}
} else if (isMethodMocked(method) && signature.contains(KEY_OBJECT_PROPHECY)) {
return KEY_METHOD_PROPHECY;
} else if (isMethodMocked(method) && signature.contains(TYPE_SEPARATOR)) {
return getTypeForMockedMethod(methodReference);
}

return null;
}

@Override
public Collection<? extends PhpNamedElement> getBySignature(String s, Project project) {
if (s.equals(KEY_METHOD_PROPHECY)) {
return getMethodProphecyCollection(project);
Collection<PhpClass> collection = new ArrayList<PhpClass>();
for (String FQN : s.split(TYPE_SEPARATOR)) {
collection.addAll(getPhpClassCollection(project, FQN));
}
return collection;
}

if (s.startsWith(KEY_OBJECT_PROPHECY)) {
String className = s.substring(KEY_OBJECT_PROPHECY.length());
Collection<PhpClass> phpClassCollection = new ArrayList<PhpClass>();
phpClassCollection.addAll(getObjectProphecyCollection(project));
phpClassCollection.addAll(getPhpClassCollection(project, className));
return phpClassCollection;
@Nullable
protected String getTypeForProphesize(@NotNull MethodReference methodReference) {
ParameterList parameterList = methodReference.getParameterList();
PhpClass phpClass = (new PhpClassResolver()).resolveByParameterListContainingClassReference(parameterList);
if (phpClass == null) {
return null;
}

if (s.startsWith(KEY_NONE)) {
String className = s.substring(KEY_NONE.length());
return getPhpClassCollection(project, className);
}
mockMethods(phpClass.getMethods());
return CLASS_OBJECT_PROPHECY + TYPE_SEPARATOR + phpClass.getFQN();
}

return null;
protected String getTypeForReveal(@NotNull MethodReference methodReference) {
String signature = methodReference.getSignature();
int offsetStart = signature.indexOf(TYPE_SEPARATOR);
int offsetEnd = signature.indexOf("." + METHOD_REVEAL);
return signature.substring(offsetStart + TYPE_SEPARATOR.length(), offsetEnd);
}

protected void mockMethod(@NotNull Method method)
{
protected String getTypeForMockedMethod(@NotNull MethodReference methodReference) {
return CLASS_METHOD_PROPHECY;
}

protected void mockMethod(@NotNull Method method) {
String hash = method.getFQN();
if (!mockedMethods.containsKey(hash)) {
mockedMethods.put(hash, true);
}
}

protected void mockMethods(Collection<Method> methods)
{
protected void mockMethods(Collection<Method> methods) {
for (Method method : methods) {
mockMethod(method);
}
}

protected boolean isMethodMocked(Method method)
{
protected boolean isMethodMocked(Method method) {
String hash = method.getFQN();
return mockedMethods.containsKey(hash);
}

protected Collection<PhpClass> getObjectProphecyCollection(@NotNull Project project)
{
return getPhpClassCollection(project, CLASS_OBJECT_PROPHECY);
}

protected Collection<PhpClass> getMethodProphecyCollection(@NotNull Project project)
{
return getPhpClassCollection(project, CLASS_METHOD_PROPHECY);
}

protected Collection<PhpClass> getPhpClassCollection(@NotNull Project project, String className)
{
return PhpIndex.getInstance(project).getClassesByFQN(className);
}

@Nullable
protected PhpClassAdapter getPhpClassAdapterForMethod(@NotNull Method method)
{
PhpClass phpClass = method.getContainingClass();
if (phpClass == null) {
return null;
}

return new PhpClassAdapter(phpClass);
}
}
5 changes: 4 additions & 1 deletion src/com/phpuaca/filter/util/ClassFinder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.phpuaca.filter.util;

import com.jetbrains.php.lang.psi.elements.*;
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;
import com.phpuaca.filter.FilterFactory;
import com.phpuaca.util.PhpClassResolver;
Expand Down
3 changes: 1 addition & 2 deletions src/com/phpuaca/util/PhpClassResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public PhpClass resolveByClassStringLiteralExpression(@NotNull StringLiteralExpr
}

@Nullable
public PhpClass resolveByParameterListContainingClassReference(@Nullable ParameterList parameterList)
{
public PhpClass resolveByParameterListContainingClassReference(@Nullable ParameterList parameterList) {
ClassConstantReference classConstantReference = PsiTreeUtil.getChildOfType(parameterList, ClassConstantReference.class);
if (classConstantReference != null) {
return resolveByClassConstantReference(classConstantReference);
Expand Down
3 changes: 1 addition & 2 deletions src/com/phpuaca/util/PhpMethodResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
final public class PhpMethodResolver {

@Nullable
public Method resolveByMethodReference(@NotNull MethodReference methodReference)
{
public Method resolveByMethodReference(@NotNull MethodReference methodReference) {
Collection<? extends PhpNamedElement> resolvedCollection = methodReference.resolveGlobal(true);
if (!resolvedCollection.isEmpty()) {
PhpNamedElement resolvedElement = resolvedCollection.iterator().next();
Expand Down

0 comments on commit eef6044

Please sign in to comment.