Search in sources :

Example 36 with FunctionReference

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

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

the class EncryptionInitializationVectorRandomnessInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new BasePhpElementVisitor() {

        @Override
        public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
            final String functionName = reference.getName();
            /* variable functions are not supported, as we are checking 2 different extensions functions */
            if (functionName != null && (functionName.equals("openssl_encrypt") || functionName.equals("mcrypt_encrypt"))) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length != 5 || arguments[4] == null || arguments[4].getText().isEmpty()) {
                    return;
                }
                /* discover and inspect possible values */
                final Set<PsiElement> values = PossibleValuesDiscoveryUtil.discover(arguments[4]);
                if (!values.isEmpty()) {
                    /* check all possible values */
                    final List<String> reporting = new ArrayList<>();
                    for (final PsiElement source : values) {
                        if (OpenapiTypesUtil.isFunctionReference(source)) {
                            final String sourceName = ((FunctionReference) source).getName();
                            if (sourceName != null && secureFunctions.contains(sourceName)) {
                                continue;
                            }
                        }
                        reporting.add(source.getText());
                    }
                    if (!reporting.isEmpty() && !this.isAggregatedGeneration(values)) {
                        /* sort reporting list to produce testable results */
                        Collections.sort(reporting);
                        final String ivFunction = functionName.startsWith("openssl_") ? "openssl_random_pseudo_bytes" : "mcrypt_create_iv";
                        holder.registerProblem(arguments[4], MessagesPresentationUtil.prefixWithEa(String.format(messagePattern, ivFunction, String.join(", ", reporting))), ProblemHighlightType.GENERIC_ERROR);
                    }
                    reporting.clear();
                }
                values.clear();
            }
        }

        private boolean isAggregatedGeneration(@NotNull Set<PsiElement> candidates) {
            if (candidates.size() == 1) {
                final PsiElement candidate = candidates.iterator().next();
                if (candidate instanceof FunctionReference) {
                    final PsiElement resolved = OpenapiResolveUtil.resolveReference((PsiReference) candidate);
                    if (resolved instanceof Function) {
                        final GroupStatement body = ExpressionSemanticUtil.getGroupStatement(resolved);
                        if (body != null) {
                            return PsiTreeUtil.findChildrenOfType(body, FunctionReference.class).stream().anyMatch(c -> secureFunctions.contains(c.getName()));
                        }
                    }
                }
            }
            return false;
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) Function(com.jetbrains.php.lang.psi.elements.Function) GroupStatement(com.jetbrains.php.lang.psi.elements.GroupStatement) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 38 with FunctionReference

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

the class StringsFirstCharactersCompareInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new BasePhpElementVisitor() {

        @Override
        public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
            final String functionName = reference.getName();
            if (functionName != null && (functionName.equals("strncmp") || functionName.equals("strncasecmp"))) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length == 3 && OpenapiTypesUtil.isNumber(arguments[2])) {
                    /* find out if we have a string literal in arguments */
                    final StringLiteralExpression literal;
                    if (arguments[1] instanceof StringLiteralExpression) {
                        literal = (StringLiteralExpression) arguments[1];
                    } else if (arguments[0] instanceof StringLiteralExpression) {
                        literal = (StringLiteralExpression) arguments[0];
                    } else {
                        literal = null;
                    }
                    /* if so, do deeper inspection */
                    if (literal != null) {
                        boolean isTarget;
                        int stringLength;
                        try {
                            final String string = PhpStringUtil.unescapeText(literal.getContents(), literal.isSingleQuote());
                            stringLength = string.length();
                            isTarget = stringLength != Integer.parseInt(arguments[2].getText());
                        } catch (NumberFormatException lengthParsingHasFailed) {
                            isTarget = false;
                            stringLength = 0;
                        }
                        if (isTarget && stringLength > 0) {
                            holder.registerProblem(arguments[2], MessagesPresentationUtil.prefixWithEa(message), new LengthFix(String.valueOf(stringLength)));
                        }
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 39 with FunctionReference

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

the class GenerateAlternativeFromArrayKeyExistsStrategy method generate.

@Nullable
public static String generate(@NotNull TernaryExpression expression) {
    /* handle inverted cases */
    PsiElement callCandidate = ExpressionSemanticUtil.getExpressionTroughParenthesis(expression.getCondition());
    boolean isInverted = false;
    if (callCandidate instanceof UnaryExpression) {
        final PsiElement operator = ((UnaryExpression) callCandidate).getOperation();
        if (null != operator && PhpTokenTypes.opNOT == operator.getNode().getElementType()) {
            isInverted = true;
            callCandidate = ((UnaryExpression) callCandidate).getValue();
        }
    }
    /* verify condition structure */
    final FunctionReference call = OpenapiTypesUtil.isFunctionReference(callCandidate) ? (FunctionReference) callCandidate : null;
    final String functionName = null == call ? null : call.getName();
    if (null == functionName || 2 != call.getParameters().length || !functionName.equals("array_key_exists")) {
        return null;
    }
    /* array_key_exists is valid only with null-alternatives */
    final PsiElement alternative = ExpressionSemanticUtil.getExpressionTroughParenthesis(isInverted ? expression.getTrueVariant() : expression.getFalseVariant());
    final PsiElement value = ExpressionSemanticUtil.getExpressionTroughParenthesis(isInverted ? expression.getFalseVariant() : expression.getTrueVariant());
    if (!(value instanceof ArrayAccessExpression) || !PhpLanguageUtil.isNull(alternative)) {
        return null;
    }
    /* verify condition and variant structure */
    final PsiElement[] params = call.getParameters();
    final ArrayAccessExpression array = (ArrayAccessExpression) value;
    final PsiElement container = array.getValue();
    final PsiElement index = null == array.getIndex() ? null : array.getIndex().getValue();
    if (null == params[0] || null == params[1] || null == container || null == index) {
        return null;
    }
    if (!OpenapiEquivalenceUtil.areEqual(params[1], container) || !OpenapiEquivalenceUtil.areEqual(params[0], index)) {
        return null;
    }
    return value.getText() + " ?? " + alternative.getText();
}
Also used : FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) UnaryExpression(com.jetbrains.php.lang.psi.elements.UnaryExpression) ArrayAccessExpression(com.jetbrains.php.lang.psi.elements.ArrayAccessExpression) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with FunctionReference

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

the class DeprecatedIniOptionsInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new BasePhpElementVisitor() {

        @Override
        public void visitPhpFunctionCall(@NotNull final FunctionReference reference) {
            final String functionName = reference.getName();
            if (functionName != null && targetFunctions.contains(functionName)) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length > 0 && arguments[0] instanceof StringLiteralExpression) {
                    final String directive = ((StringLiteralExpression) arguments[0]).getContents().toLowerCase();
                    if (options.containsKey(directive)) {
                        final PhpLanguageLevel php = PhpLanguageLevel.get(holder.getProject());
                        final Triple<PhpLanguageLevel, PhpLanguageLevel, String> details = options.get(directive);
                        final PhpLanguageLevel removalVersion = details.getMiddle();
                        final PhpLanguageLevel deprecationVersion = details.getLeft();
                        if (removalVersion != null && php.atLeast(removalVersion)) {
                            final String alternative = details.getRight();
                            holder.registerProblem(arguments[0], String.format(MessagesPresentationUtil.prefixWithEa(alternative == null ? patternRemoved : patternRemovedWithAlternative), directive, removalVersion.getVersion(), alternative));
                        } else if (deprecationVersion != null && php.atLeast(deprecationVersion)) {
                            final String alternative = details.getRight();
                            holder.registerProblem(arguments[0], String.format(MessagesPresentationUtil.prefixWithEa(alternative == null ? patternDeprecated : patternDeprecatedWithAlternative), directive, deprecationVersion.getVersion(), alternative), ProblemHighlightType.LIKE_DEPRECATED);
                        }
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) PhpLanguageLevel(com.kalessil.phpStorm.phpInspectionsEA.openApi.PhpLanguageLevel) NotNull(org.jetbrains.annotations.NotNull)

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