Search in sources :

Example 11 with PhpUnitAssertFixer

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

the class AssertStringEqualsFileStrategy method apply.

public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
    boolean result = false;
    if (targetAssertions.contains(methodName)) {
        final PsiElement[] assertionArguments = reference.getParameters();
        if (assertionArguments.length > 1 && OpenapiTypesUtil.isFunctionReference(assertionArguments[0])) {
            final FunctionReference candidate = (FunctionReference) assertionArguments[0];
            final String functionName = candidate.getName();
            if (functionName != null && functionName.equals("file_get_contents")) {
                final PsiElement[] functionArguments = candidate.getParameters();
                if (functionArguments.length == 1) {
                    final Function scope = ExpressionSemanticUtil.getScope(reference);
                    final boolean shouldReport = scope == null || !validContexts.contains(scope.getName());
                    if (shouldReport) {
                        final String[] suggestedArguments = new String[assertionArguments.length];
                        suggestedArguments[0] = functionArguments[0].getText();
                        suggestedArguments[1] = assertionArguments[1].getText();
                        if (assertionArguments.length > 2) {
                            suggestedArguments[2] = assertionArguments[2].getText();
                        }
                        final String suggestedAssertion = "assertStringEqualsFile";
                        holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
                        result = true;
                    }
                }
            }
        }
    }
    return result;
}
Also used : Function(com.jetbrains.php.lang.psi.elements.Function) PhpUnitAssertFixer(com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) PsiElement(com.intellij.psi.PsiElement)

Example 12 with PhpUnitAssertFixer

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

the class AssertSameStrategy method apply.

public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
    boolean result = false;
    if (targetMapping.containsKey(methodName)) {
        final PsiElement[] arguments = reference.getParameters();
        if (arguments.length > 1 && isPrimitiveScalar(holder.getProject(), arguments[1]) && isPrimitiveScalar(holder.getProject(), arguments[0])) {
            final String suggestedAssertion = targetMapping.get(methodName);
            final String[] suggestedArguments = new String[arguments.length];
            Arrays.stream(arguments).map(PsiElement::getText).collect(Collectors.toList()).toArray(suggestedArguments);
            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) PsiElement(com.intellij.psi.PsiElement)

Example 13 with PhpUnitAssertFixer

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

the class ExpectsOnceStrategy method apply.

public static void apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
    if (methodName.equals("expects")) {
        final PsiElement[] arguments = reference.getParameters();
        if (arguments.length == 1 && arguments[0] instanceof MethodReference) {
            final MethodReference innerReference = (MethodReference) arguments[0];
            final String innerMethodName = innerReference.getName();
            if (innerMethodName != null && innerMethodName.equals("exactly")) {
                final PsiElement[] innerArguments = innerReference.getParameters();
                if (innerArguments.length == 1 && OpenapiTypesUtil.isNumber(innerArguments[0])) {
                    final boolean isResult = innerArguments[0].getText().equals("1");
                    if (isResult) {
                        holder.registerProblem(innerReference, MessagesPresentationUtil.prefixWithEa(message), new PhpUnitAssertFixer("once", new String[] {}));
                    }
                }
            }
        }
    }
}
Also used : PhpUnitAssertFixer(com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PsiElement(com.intellij.psi.PsiElement)

Example 14 with PhpUnitAssertFixer

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

the class AssertConstantStrategy method apply.

public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
    boolean result = false;
    if (targetMapping.containsKey(methodName)) {
        final PsiElement[] arguments = reference.getParameters();
        if (arguments.length > 1) {
            for (final PsiElement argument : arguments) {
                if (argument instanceof ConstantReference) {
                    final String constantName = ((ConstantReference) argument).getName();
                    if (constantName != null) {
                        final String constantNameNormalized = constantName.toLowerCase();
                        if (targetConstants.contains(constantNameNormalized)) {
                            final String suggestedAssertion = String.format(targetMapping.get(methodName), StringUtils.capitalize(constantNameNormalized));
                            final String[] suggestedArguments = new String[arguments.length - 1];
                            suggestedArguments[0] = Arrays.stream(arguments).filter(a -> a != argument).findFirst().get().getText();
                            if (arguments.length > 2) {
                                suggestedArguments[1] = arguments[2].getText();
                            }
                            holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
                            result = true;
                            break;
                        }
                    }
                }
            }
        }
    }
    return result;
}
Also used : ConstantReference(com.jetbrains.php.lang.psi.elements.ConstantReference) PhpUnitAssertFixer(com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer) PsiElement(com.intellij.psi.PsiElement)

Example 15 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) {
    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 = targetMapping.get(methodName);
                    final String suggestedType = targetFunctionMapping.get(functionName);
                    final String[] 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();
                    }
                    /* register an issue */
                    holder.registerProblem(reference, String.format(messagePattern, suggestedAssertion, suggestedType), 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)

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