Search in sources :

Example 26 with FunctionReference

use of com.jetbrains.php.lang.psi.elements.FunctionReference 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 27 with FunctionReference

use of com.jetbrains.php.lang.psi.elements.FunctionReference 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 28 with FunctionReference

use of com.jetbrains.php.lang.psi.elements.FunctionReference 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 29 with FunctionReference

use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.

the class AssertRegexStrategy method apply.

public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
    boolean result = false;
    if (numberCompareTargets.containsKey(methodName)) {
        final PsiElement[] assertionArguments = reference.getParameters();
        if (assertionArguments.length >= 2 && OpenapiTypesUtil.isNumber(assertionArguments[0])) {
            final boolean isTarget = OpenapiTypesUtil.isFunctionReference(assertionArguments[1]);
            if (isTarget) {
                final FunctionReference candidate = (FunctionReference) assertionArguments[1];
                final String candidateName = candidate.getName();
                if (candidateName != null && candidateName.equals("preg_match")) {
                    final PsiElement[] functionArguments = candidate.getParameters();
                    if (functionArguments.length == 2) {
                        final String suggestedAssertion = assertionArguments[0].getText().equals(numberCompareTargets.get(methodName)) ? "assertRegExp" : "assertNotRegExp";
                        final String[] suggestedArguments = new String[assertionArguments.length];
                        suggestedArguments[0] = functionArguments[0].getText();
                        suggestedArguments[1] = functionArguments[1].getText();
                        if (assertionArguments.length > 2) {
                            suggestedArguments[2] = assertionArguments[2].getText();
                        }
                        holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
                        result = true;
                    }
                }
            }
        }
    } else if (binaryTargets.contains(methodName)) {
        final PsiElement[] assertionArguments = reference.getParameters();
        if (assertionArguments.length > 0 && assertionArguments[0] instanceof BinaryExpression) {
            final BinaryExpression binary = (BinaryExpression) assertionArguments[0];
            if (binary.getOperationType() == PhpTokenTypes.opGREATER) {
                final PsiElement left = binary.getLeftOperand();
                final PsiElement right = binary.getRightOperand();
                if (OpenapiTypesUtil.isNumber(right) && OpenapiTypesUtil.isFunctionReference(left)) {
                    final boolean isTargetNumber = right.getText().equals("0");
                    if (isTargetNumber) {
                        final FunctionReference candidate = (FunctionReference) left;
                        final String candidateName = candidate.getName();
                        if (candidateName != null && candidateName.equals("preg_match")) {
                            final PsiElement[] functionArguments = candidate.getParameters();
                            if (functionArguments.length == 2) {
                                final String suggestedAssertion = methodName.equals("assertTrue") ? "assertRegExp" : "assertNotRegExp";
                                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 : BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) PhpUnitAssertFixer(com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) PsiElement(com.intellij.psi.PsiElement)

Example 30 with FunctionReference

use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.

the class AssertCountStrategy 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 > 1 && OpenapiTypesUtil.isFunctionReference(assertionArguments[1])) {
            final FunctionReference candidate = (FunctionReference) assertionArguments[1];
            final String functionName = candidate.getName();
            if (functionName != null && functionName.equals("count")) {
                final PsiElement[] functionArguments = candidate.getParameters();
                if (functionArguments.length == 1) {
                    final String suggestedAssertion = targetMapping.get(methodName);
                    final String[] suggestedArguments = new String[assertionArguments.length];
                    suggestedArguments[0] = assertionArguments[0].getText();
                    suggestedArguments[1] = functionArguments[0].getText();
                    if (assertionArguments.length > 2) {
                        suggestedArguments[2] = assertionArguments[2].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)

Aggregations

FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)58 PsiElement (com.intellij.psi.PsiElement)55 NotNull (org.jetbrains.annotations.NotNull)46 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)43 BinaryExpression (com.jetbrains.php.lang.psi.elements.BinaryExpression)16 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)15 IElementType (com.intellij.psi.tree.IElementType)11 PhpUnitAssertFixer (com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer)9 ArrayCreationExpression (com.jetbrains.php.lang.psi.elements.ArrayCreationExpression)6 UnaryExpression (com.jetbrains.php.lang.psi.elements.UnaryExpression)6 Function (com.jetbrains.php.lang.psi.elements.Function)5 MethodReference (com.jetbrains.php.lang.psi.elements.MethodReference)5 ParameterList (com.jetbrains.php.lang.psi.elements.ParameterList)5 PhpLanguageLevel (com.jetbrains.php.config.PhpLanguageLevel)4 ArrayList (java.util.ArrayList)4 Nullable (org.jetbrains.annotations.Nullable)4 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)3 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)3 ArrayAccessExpression (com.jetbrains.php.lang.psi.elements.ArrayAccessExpression)3 HashSet (java.util.HashSet)3