Search in sources :

Example 1 with PhpUnitAssertFixer

use of com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer in project phpinspectionsea by kalessil.

the class AssertResourceExistsStrategy method apply.

public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
    boolean result = false;
    if (targetMapping.containsKey(methodName)) {
        final PsiElement[] assertionArguments = reference.getParameters();
        if (assertionArguments.length > 0 && OpenapiTypesUtil.isFunctionReference(assertionArguments[0])) {
            final FunctionReference candidate = (FunctionReference) assertionArguments[0];
            final String functionName = candidate.getName();
            if (functionName != null && targetFunctions.containsKey(functionName)) {
                final PsiElement[] functionArguments = candidate.getParameters();
                if (functionArguments.length == 1) {
                    final String suggestedAssertion = String.format(targetMapping.get(methodName), targetFunctions.get(functionName));
                    final String[] suggestedArguments = new String[assertionArguments.length];
                    suggestedArguments[0] = functionArguments[0].getText();
                    if (assertionArguments.length > 1) {
                        suggestedArguments[1] = assertionArguments[1].getText();
                    }
                    holder.registerProblem(reference, String.format(messagePattern, suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
                    result = true;
                }
            }
        }
    }
    return result;
}
Also used : PhpUnitAssertFixer(com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PhpUnitAssertFixer

use of com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer in project phpinspectionsea by kalessil.

the class AssertInternalTypeStrategy method apply.

public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder, @NotNull PhpUnitVersion version) {
    boolean result = false;
    if (targetMapping.containsKey(methodName)) {
        final PsiElement[] assertionArguments = reference.getParameters();
        if (assertionArguments.length > 0 && OpenapiTypesUtil.isFunctionReference(assertionArguments[0])) {
            final FunctionReference functionReference = (FunctionReference) assertionArguments[0];
            final String functionName = functionReference.getName();
            if (functionName != null && targetFunctionMapping.containsKey(functionName)) {
                final PsiElement[] functionArguments = functionReference.getParameters();
                if (functionArguments.length > 0) {
                    /* generate QF arguments */
                    final String suggestedAssertion;
                    final String suggestedType;
                    final String[] suggestedArguments;
                    final String message;
                    if (version.below(PhpUnitVersion.PHPUNIT80)) {
                        suggestedAssertion = targetMapping.get(methodName);
                        suggestedType = targetFunctionMapping.get(functionName);
                        suggestedArguments = new String[assertionArguments.length + 1];
                        suggestedArguments[0] = String.format("'%s'", suggestedType);
                        suggestedArguments[1] = functionArguments[0].getText();
                        if (assertionArguments.length > 1) {
                            suggestedArguments[2] = assertionArguments[1].getText();
                        }
                        message = String.format(messagePatternInternalType, suggestedAssertion, suggestedType);
                    } else {
                        /* internal type assertions were deprecated */
                        suggestedAssertion = String.format(targetMapping.get(methodName).replace("InternalType", "Is%s").replace("NotIs", "IsNot"), StringUtils.capitalize(targetFunctionMapping.get(functionName)));
                        suggestedType = null;
                        suggestedArguments = new String[assertionArguments.length];
                        suggestedArguments[0] = functionArguments[0].getText();
                        if (assertionArguments.length > 1) {
                            suggestedArguments[1] = assertionArguments[1].getText();
                        }
                        message = String.format(messagePattern, suggestedAssertion);
                    }
                    /* register an issue */
                    holder.registerProblem(reference, MessagesPresentationUtil.prefixWithEa(message), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
                    result = true;
                }
            }
        }
    }
    return result;
}
Also used : PhpUnitAssertFixer(com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PhpUnitAssertFixer

use of com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer in project phpinspectionsea by kalessil.

the class AssertResourceExistsStrategy method apply.

public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder, @NotNull PhpUnitVersion version) {
    boolean result = false;
    if (targetMapping.containsKey(methodName)) {
        final PsiElement[] assertionArguments = reference.getParameters();
        if (assertionArguments.length > 0 && OpenapiTypesUtil.isFunctionReference(assertionArguments[0])) {
            final FunctionReference candidate = (FunctionReference) assertionArguments[0];
            final String functionName = candidate.getName();
            if (functionName != null && targetFunctions.containsKey(functionName)) {
                final PsiElement[] functionArguments = candidate.getParameters();
                if (functionArguments.length == 1) {
                    final String suggestedAssertion = String.format(targetMapping.get(methodName), targetFunctions.get(functionName));
                    final String[] suggestedArguments = new String[assertionArguments.length];
                    suggestedArguments[0] = functionArguments[0].getText();
                    if (assertionArguments.length > 1) {
                        suggestedArguments[1] = assertionArguments[1].getText();
                    }
                    holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), suggestNotDeprecated(suggestedAssertion, version)), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
                    result = true;
                }
            }
        }
    }
    return result;
}
Also used : PhpUnitAssertFixer(com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PhpUnitAssertFixer

use of com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer in project phpinspectionsea by kalessil.

the class AssertContainsStrategy method apply.

public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder, @NotNull PhpUnitVersion version) {
    boolean result = false;
    if (version.below(PhpUnitVersion.PHPUNIT90) && targetMapping.containsKey(methodName)) {
        final PsiElement[] assertionArguments = reference.getParameters();
        if (assertionArguments.length > 0 && OpenapiTypesUtil.isFunctionReference(assertionArguments[0])) {
            final FunctionReference candidate = (FunctionReference) assertionArguments[0];
            final String functionName = candidate.getName();
            if (functionName != null && functionName.equals("in_array")) {
                final PsiElement[] functionArguments = candidate.getParameters();
                if (functionArguments.length >= 2) {
                    final String suggestedAssertion = targetMapping.get(methodName);
                    final String[] suggestedArguments = new String[assertionArguments.length + 1];
                    suggestedArguments[0] = functionArguments[0].getText();
                    suggestedArguments[1] = functionArguments[1].getText();
                    if (assertionArguments.length > 1) {
                        suggestedArguments[2] = assertionArguments[1].getText();
                    }
                    holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
                    result = true;
                }
            }
        }
    }
    return result;
}
Also used : PhpUnitAssertFixer(com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) PsiElement(com.intellij.psi.PsiElement)

Example 5 with PhpUnitAssertFixer

use of com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer in project phpinspectionsea by kalessil.

the class AssertEmptyStrategy method apply.

public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
    boolean result = false;
    if (targetMapping.containsKey(methodName)) {
        final PsiElement[] assertionArguments = reference.getParameters();
        if (assertionArguments.length > 0 && assertionArguments[0] instanceof PhpEmpty) {
            final PsiElement[] emptyArguments = ((PhpEmpty) assertionArguments[0]).getVariables();
            if (emptyArguments.length == 1) {
                /* generate QF arguments */
                final String suggestedAssertion = targetMapping.get(methodName);
                final String[] suggestedArguments = new String[assertionArguments.length];
                suggestedArguments[0] = emptyArguments[0].getText();
                if (assertionArguments.length > 1) {
                    suggestedArguments[1] = assertionArguments[1].getText();
                }
                /* register an issue */
                holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
                result = true;
            }
        }
    }
    return result;
}
Also used : PhpEmpty(com.jetbrains.php.lang.psi.elements.PhpEmpty) PhpUnitAssertFixer(com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)15 PhpUnitAssertFixer (com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer)15 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)9 MethodReference (com.jetbrains.php.lang.psi.elements.MethodReference)3 Function (com.jetbrains.php.lang.psi.elements.Function)2 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)1 BinaryExpression (com.jetbrains.php.lang.psi.elements.BinaryExpression)1 ConstantReference (com.jetbrains.php.lang.psi.elements.ConstantReference)1 PhpEmpty (com.jetbrains.php.lang.psi.elements.PhpEmpty)1 ExpressionSemanticUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.ExpressionSemanticUtil)1 MessagesPresentationUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.MessagesPresentationUtil)1 OpenapiTypesUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiTypesUtil)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 NotNull (org.jetbrains.annotations.NotNull)1