-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 52600c0
Showing
6 changed files
with
320 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
out/ | ||
/.idea | ||
/.idea/misc.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
Phake Mocking Hints - PhpStorm Plugin | ||
|
||
Copyright (c) 2015, Mike Lively <[email protected]> | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions | ||
are met: | ||
|
||
Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
|
||
Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in | ||
the documentation and/or other materials provided with the | ||
distribution. | ||
|
||
Neither the name of Mike Lively nor the names of his | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<idea-plugin version="2"> | ||
<id>com.digitalsandwich.phake</id> | ||
<name>Phake Mocking Hints</name> | ||
<version>0.1</version> | ||
<vendor email="[email protected]" url="http://digitalsandwich.com">Mike Lively</vendor> | ||
|
||
<description><![CDATA[ | ||
Provides auto completion and code navigation for tests written using the Phake mocking framework. | ||
]]></description> | ||
|
||
<change-notes><![CDATA[ | ||
]]> | ||
</change-notes> | ||
|
||
<!-- please see https://confluence.jetbrains.com/display/IDEADEV/Build+Number+Ranges for description --> | ||
<idea-version since-build="131"/> | ||
|
||
<!-- please see https://confluence.jetbrains.com/display/IDEADEV/Plugin+Compatibility+with+IntelliJ+Platform+Products | ||
on how to target different products --> | ||
<!-- uncomment to enable plugin in all products | ||
<depends>com.intellij.modules.lang</depends> | ||
--> | ||
|
||
<depends>com.jetbrains.php</depends> | ||
<depends>com.intellij.modules.platform</depends> | ||
|
||
<extensions defaultExtensionNs="com.intellij"> | ||
<completion.contributor language="PHP" implementationClass="com.digitalsandwich.phake.PhakeCompletionContributor" /> | ||
|
||
<php.typeProvider2 implementation="com.digitalsandwich.phake.PhakeMockTypeProvider"/> | ||
</extensions> | ||
|
||
<application-components> | ||
<!-- Add your application components here --> | ||
</application-components> | ||
|
||
<project-components> | ||
<!-- Add your project components here --> | ||
</project-components> | ||
|
||
<actions> | ||
<!-- Add your actions here --> | ||
</actions> | ||
|
||
</idea-plugin> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="PLUGIN_MODULE" version="4"> | ||
<component name="DevKit.ModuleBuildProperties" url="file://$MODULE_DIR$/META-INF/plugin.xml" /> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" scope="PROVIDED" name="php-openapi" level="project" /> | ||
</component> | ||
</module> |
64 changes: 64 additions & 0 deletions
64
src/com/digitalsandwich/phake/PhakeCompletionContributor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.digitalsandwich.phake; | ||
|
||
import com.intellij.codeInsight.completion.*; | ||
import com.intellij.codeInsight.lookup.LookupElementBuilder; | ||
import com.intellij.patterns.PlatformPatterns; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import com.intellij.util.ProcessingContext; | ||
import com.jetbrains.php.PhpIcons; | ||
import com.jetbrains.php.PhpIndex; | ||
import com.jetbrains.php.lang.psi.elements.MethodReference; | ||
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; | ||
import org.jetbrains.annotations.NotNull; | ||
import java.util.Collection; | ||
|
||
/** | ||
* Handles code completion of class names as strings for mock() partialMock() and partMock() | ||
*/ | ||
public class PhakeCompletionContributor extends CompletionContributor | ||
{ | ||
public PhakeCompletionContributor() | ||
{ | ||
extend(CompletionType.BASIC, PlatformPatterns.psiElement(), new CompletionProvider<CompletionParameters>() { | ||
@Override | ||
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) { | ||
MethodReference method = PsiTreeUtil.getContextOfType(completionParameters.getOriginalPosition(), MethodReference.class, true); | ||
|
||
if (method == null) | ||
{ | ||
return; | ||
} | ||
|
||
PsiElement[] parameters = method.getParameters(); | ||
if (parameters.length < 1 || !(parameters[0] instanceof StringLiteralExpression)) | ||
{ | ||
return; | ||
} | ||
|
||
|
||
if (method.getSignature().equals("#M#C\\Phake.mock") || method.getSignature().equals("#M#C\\Phake.partMock") || method.getSignature().equals("#M#C\\Phake.partialMock")) | ||
{ | ||
PhpIndex phpIndex = PhpIndex.getInstance(method.getProject()); | ||
Collection<String> classNames = phpIndex.getAllClassNames(null); | ||
for (String className : classNames) | ||
{ | ||
LookupElementBuilder lookupElement = LookupElementBuilder.create(className) | ||
.withTypeText(className) | ||
.withIcon(PhpIcons.CLASS_ICON); | ||
completionResultSet.addElement(lookupElement); | ||
} | ||
|
||
Collection<String> interfaceNames = phpIndex.getAllInterfaceNames(); | ||
for (String interfaceName : interfaceNames) | ||
{ | ||
LookupElementBuilder lookupElement = LookupElementBuilder.create(interfaceName) | ||
.withTypeText(interfaceName) | ||
.withIcon(PhpIcons.INTERFACE_ICON); | ||
completionResultSet.addElement(lookupElement); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} |
160 changes: 160 additions & 0 deletions
160
src/com/digitalsandwich/phake/PhakeMockTypeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package com.digitalsandwich.phake; | ||
|
||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.util.text.StringUtil; | ||
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.jetbrains.php.lang.psi.resolve.types.PhpType; | ||
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider2; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
/** | ||
* Handles the type translation | ||
*/ | ||
public class PhakeMockTypeProvider implements PhpTypeProvider2 | ||
{ | ||
|
||
public static final String CALLTYPE_MOCK = "<01>"; | ||
public static final String CALLTYPE_VERIFICATION = "<02>"; | ||
public static final String CALLTYPE_STUB = "<03>"; | ||
public static final String CALLTYPE_STUBBED_METHOD = "<04>"; | ||
|
||
@Override | ||
public char getKey() { | ||
return '\u2002'; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getType(PsiElement psiElement) { | ||
if (psiElement instanceof MethodReference) | ||
{ | ||
MethodReference methodReference = (MethodReference) psiElement; | ||
String signature = methodReference.getSignature(); | ||
if (isMockCall(signature)) | ||
{ | ||
PsiElement[] parameters = methodReference.getParameters(); | ||
|
||
if (parameters.length > 0) | ||
{ | ||
PsiElement parameter = parameters[0]; | ||
if (parameter instanceof StringLiteralExpression) | ||
{ | ||
String phpClassName = ((StringLiteralExpression)parameter).getContents(); | ||
|
||
if (StringUtil.isNotEmpty(phpClassName)) | ||
{ | ||
return CALLTYPE_MOCK + signature + "~" + phpClassName; | ||
} | ||
} | ||
} | ||
} | ||
|
||
else if (isVerifyCall(signature)) | ||
{ | ||
int parameterPosition = 0; | ||
String typeList = passThruMethodParameterType(methodReference, parameterPosition); | ||
if (StringUtil.isNotEmpty(typeList)) | ||
{ | ||
return CALLTYPE_VERIFICATION + typeList; | ||
} | ||
} | ||
|
||
else if (isWhenCall(signature)) | ||
{ | ||
int parameterPosition = 0; | ||
String typeList = passThruMethodParameterType(methodReference, parameterPosition); | ||
if (StringUtil.isNotEmpty(typeList)) | ||
{ | ||
return CALLTYPE_STUB + typeList; | ||
} | ||
} | ||
else if (signature.startsWith("#M#") && signature.contains(CALLTYPE_STUB)) | ||
{ | ||
MethodReference previousMethodInChain = PsiTreeUtil.findChildOfType(psiElement, MethodReference.class); | ||
|
||
if (previousMethodInChain != null && (isWhenCall(previousMethodInChain.getSignature()))) | ||
{ | ||
return CALLTYPE_STUBBED_METHOD; | ||
} | ||
} | ||
else if (signature.startsWith("#M#") && signature.contains(CALLTYPE_STUBBED_METHOD)) | ||
{ | ||
return CALLTYPE_STUBBED_METHOD; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Nullable | ||
private String passThruMethodParameterType(MethodReference methodReference, int parameterPosition) { | ||
String typeList = null; | ||
PsiElement[] parameters = methodReference.getParameters(); | ||
if (parameters.length > parameterPosition) | ||
{ | ||
PsiElement parameter = parameters[parameterPosition]; | ||
if (parameter instanceof Variable) | ||
{ | ||
PhpType type = ((Variable) parameter).getType(); | ||
typeList = StringUtil.join(type.getTypes(), "|"); | ||
} | ||
} | ||
return typeList; | ||
} | ||
|
||
@Override | ||
public Collection<? extends PhpNamedElement> getBySignature(String s, Project project) { | ||
|
||
PhpIndex phpIndex = PhpIndex.getInstance(project); | ||
Collection<PhpNamedElement> signedClasses = new ArrayList<PhpNamedElement>(); | ||
if (s.substring(0, 4).equals(CALLTYPE_MOCK)) | ||
{ | ||
int separator = s.indexOf("~"); | ||
String phakeSignature = s.substring(4, separator); | ||
String className = s.substring(separator + 1); | ||
|
||
PhpClass phpClass = phpIndex.getClassByName(className); | ||
signedClasses.addAll(phpIndex.getBySignature(phakeSignature)); | ||
if (phpClass != null) | ||
{ | ||
signedClasses.add(phpClass); | ||
} | ||
else | ||
{ | ||
signedClasses.addAll(phpIndex.getInterfacesByName(className)); | ||
} | ||
} | ||
else if (s.substring(0, 4).equals(CALLTYPE_VERIFICATION) || s.substring(0, 4).equals(CALLTYPE_STUB)) | ||
{ | ||
for (String signature : StringUtil.split(s.substring(4),"|")) | ||
{ | ||
Collection<? extends PhpNamedElement> phpNamedElements = phpIndex.getBySignature(signature); | ||
signedClasses.addAll(phpNamedElements); | ||
} | ||
} | ||
else if (s.substring(0, 4).equals(CALLTYPE_STUBBED_METHOD)) | ||
{ | ||
PhpClass answerBinder = phpIndex.getClassByName("Phake_Proxies_AnswerBinderProxy"); | ||
signedClasses.add(answerBinder); | ||
} | ||
|
||
return signedClasses.size() == 0 ? null : signedClasses; | ||
} | ||
|
||
private boolean isWhenCall(String signature) { | ||
return signature.equals("#M#C\\Phake.when") || signature.equals("#M#C\\Phake.whenStatic"); | ||
} | ||
|
||
private boolean isVerifyCall(String signature) { | ||
return signature.equals("#M#C\\Phake.verify") || signature.equals("#M#C\\Phake.verifyStatic"); | ||
} | ||
|
||
private boolean isMockCall(String signature) { | ||
return signature.equals("#M#C\\Phake.mock") || signature.equals("#M#C\\Phake.partialMock") || signature.equals("#M#C\\Phake.partMock"); | ||
} | ||
} |