forked from maxfilatov/phpuaca
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from Haehnchen/feature/depreaction-mock
add inspection for mocked deprecation methods
- Loading branch information
Showing
10 changed files
with
336 additions
and
21 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
src/main/java/de/espend/idea/php/phpunit/inspection/DeprecatedMockedMethodInspection.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,95 @@ | ||
package de.espend.idea.php.phpunit.inspection; | ||
|
||
import com.intellij.codeInspection.LocalInspectionTool; | ||
import com.intellij.codeInspection.ProblemHighlightType; | ||
import com.intellij.codeInspection.ProblemsHolder; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiElementVisitor; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import com.jetbrains.php.PhpIndex; | ||
import com.jetbrains.php.lang.psi.elements.*; | ||
import de.espend.idea.php.phpunit.utils.PatternUtil; | ||
import de.espend.idea.php.phpunit.utils.PhpElementsUtil; | ||
import de.espend.idea.php.phpunit.utils.PhpUnitPluginUtil; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
*/ | ||
public class DeprecatedMockedMethodInspection extends LocalInspectionTool { | ||
@Override | ||
public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { | ||
return new PsiElementVisitor() { | ||
@Override | ||
public void visitElement(@NotNull PsiElement psiElement) { | ||
if (!(psiElement instanceof StringLiteralExpression)) { | ||
super.visitElement(psiElement); | ||
return; | ||
} | ||
|
||
if (PatternUtil.getMethodReferenceWithParameterPattern().accepts(psiElement)) { | ||
visitCreateMock((StringLiteralExpression) psiElement, holder); | ||
} | ||
|
||
if (PatternUtil.getArrayParameterPattern().accepts(psiElement)) { | ||
visitCreatePartialMock((StringLiteralExpression) psiElement, holder); | ||
} | ||
|
||
super.visitElement(psiElement); | ||
} | ||
}; | ||
} | ||
private static void visitCreateMock(@NotNull StringLiteralExpression psiElement, @NotNull ProblemsHolder holder) { | ||
String clazz = PhpUnitPluginUtil.findCreateMockParameterOnParameterScope(psiElement); | ||
if (clazz == null) { | ||
return; | ||
} | ||
|
||
String method = psiElement.getContents(); | ||
if (!method.isBlank()) { | ||
registerProblemIfDeprecated(psiElement, holder, clazz, method); | ||
} | ||
} | ||
|
||
private static void registerProblemIfDeprecated(@NotNull StringLiteralExpression psiElement, @NotNull ProblemsHolder holder, @NotNull String clazz, @NotNull String method) { | ||
boolean isDeprecated = PhpIndex.getInstance(psiElement.getProject()) | ||
.getAnyByFQN(clazz).stream() | ||
.map(phpClass -> phpClass.findMethodByName(method)) | ||
.filter(Objects::nonNull) | ||
.anyMatch(PhpNamedElement::isDeprecated); | ||
|
||
if (isDeprecated) { | ||
holder.registerProblem(psiElement, "Method '" + method + "' is deprecated ", ProblemHighlightType.LIKE_DEPRECATED); | ||
} | ||
} | ||
|
||
private static void visitCreatePartialMock(@NotNull StringLiteralExpression psiElement, @NotNull ProblemsHolder holder) { | ||
MethodReference parentOfType = PsiTreeUtil.getParentOfType(psiElement, MethodReference.class); | ||
if (parentOfType == null) { | ||
return; | ||
} | ||
|
||
if (PhpUnitPluginUtil.isCreatePartialMockMethod(parentOfType)) { | ||
PsiElement originalClassName = parentOfType.getParameter("originalClassName", 0); | ||
|
||
if (originalClassName == null) { | ||
return; | ||
} | ||
|
||
String method = psiElement.getContents(); | ||
if (method.isBlank()) { | ||
return; | ||
} | ||
|
||
String clazz = PhpElementsUtil.getStringValue(originalClassName); | ||
if (StringUtils.isBlank(clazz)) { | ||
return; | ||
} | ||
|
||
registerProblemIfDeprecated(psiElement, holder, clazz, method); | ||
} | ||
} | ||
} |
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
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
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
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
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
7 changes: 7 additions & 0 deletions
7
src/main/resources/inspectionDescriptions/DeprecatedMockedMethodInspection.html
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,7 @@ | ||
<html> | ||
<body> | ||
Reports the usages of deprecated entities. | ||
<!-- tooltip end --> | ||
In most cases, such usages should be removed or replaced with other constructs. | ||
</body> | ||
</html> |
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
60 changes: 60 additions & 0 deletions
60
...test/java/de/espend/idea/php/phpunit/inspection/DeprecatedMockedMethodInspectionTest.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,60 @@ | ||
package de.espend.idea.php.phpunit.inspection; | ||
|
||
import de.espend.idea.php.phpunit.PhpUnitLightCodeInsightFixtureTestCase; | ||
|
||
/** | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
* @see DeprecatedMockedMethodInspection | ||
*/ | ||
public class DeprecatedMockedMethodInspectionTest extends PhpUnitLightCodeInsightFixtureTestCase { | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
myFixture.copyFileToProject("DeprecatedMockedMethodInspection.php"); | ||
} | ||
|
||
@Override | ||
public String getTestDataPath() { | ||
return "src/test/java/de/espend/idea/php/phpunit/inspection/fixtures"; | ||
} | ||
|
||
public void testDeprecationForMockedMethodViaCreatePartialMock() { | ||
assertLocalInspectionContains("test.php", "<?php\n" + | ||
"class Foo extends \\PHPUnit\\Framework\\TestCase\n" + | ||
"{\n" + | ||
" public function foobar()\n" + | ||
" {\n" + | ||
" $this->createPartialMock(\\Foo\\Bar::class, ['getFoobarD<caret>eprecated']);\n" + | ||
" }\n" + | ||
"}", | ||
"Method 'getFoobarDeprecated' is deprecated" | ||
); | ||
} | ||
|
||
public void testDeprecationForMockedMethodViaCreatePartialMock2() { | ||
assertLocalInspectionContains("test.php", "<?php\n" + | ||
"class Foo extends \\PHPUnit\\Framework\\TestCase\n" + | ||
"{\n" + | ||
" public function foobar()\n" + | ||
" {\n" + | ||
" $foo = $this->createPartialMock(\\Foo\\Bar::class, ['getFoobar']);\n" + | ||
" $foo->method('getFoobarD<caret>eprecated');\n" + | ||
" }\n" + | ||
"}", | ||
"Method 'getFoobarDeprecated' is deprecated" | ||
); | ||
} | ||
|
||
public void testDeprecationForMockedMethodViaCreateMock() { | ||
assertLocalInspectionContains("test2.php", "<?php\n" + | ||
"class Foo extends \\PHPUnit\\Framework\\TestCase\n" + | ||
"{\n" + | ||
" public function foobar()\n" + | ||
" {\n" + | ||
" $foo = $this->createMock(\\Foo\\Bar::class);\n" + | ||
" $foo->method('getFoobarD<caret>eprecated');\n" + | ||
" }\n" + | ||
"}", | ||
"Method 'getFoobarDeprecated' is deprecated" | ||
); | ||
} | ||
} |
Oops, something went wrong.