Search in sources :

Example 26 with StringLiteralExpression

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

use of com.jetbrains.php.lang.psi.elements.StringLiteralExpression 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)

Example 28 with StringLiteralExpression

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

the class StringNormalizationInspector 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) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length > 0 && OpenapiTypesUtil.isFunctionReference(arguments[0])) {
                    final FunctionReference innerCall = (FunctionReference) arguments[0];
                    final String innerCallName = innerCall.getName();
                    if (innerCallName != null) {
                        final PsiElement[] innerArguments = innerCall.getParameters();
                        if (innerArguments.length > 0) {
                            if (lengthManipulation.contains(functionName) && caseManipulation.contains(innerCallName)) {
                                final boolean isTarget = !functionName.endsWith("trim") || arguments.length == 1 || (arguments[1] instanceof StringLiteralExpression && !regexTrimmedCharacters.matcher(arguments[1].getText()).matches());
                                if (isTarget) {
                                    final String theString = innerArguments[0].getText();
                                    final String newInnerCall = reference.getText().replace(arguments[0].getText(), theString);
                                    final String replacement = innerCall.getText().replace(theString, newInnerCall);
                                    holder.registerProblem(reference, MessagesPresentationUtil.prefixWithEa(String.format(patternInvertedNesting, replacement)), new NormalizationFix(replacement));
                                }
                            } else if (caseManipulation.contains(functionName) && caseManipulation.contains(innerCallName)) {
                                if (functionName.equals(innerCallName)) {
                                    holder.registerProblem(innerCall, MessagesPresentationUtil.prefixWithEa(String.format(patternSenselessNesting, innerCallName)), new NormalizationFix(innerArguments[0].getText()));
                                } else if (!innerCaseManipulation.contains(innerCallName)) {
                                    /* false-positives: ucwords with 2 arguments */
                                    final boolean isTarget = !innerCallName.equals("ucwords") || innerArguments.length == 1;
                                    if (isTarget) {
                                        holder.registerProblem(innerCall, MessagesPresentationUtil.prefixWithEa(String.format(patternSenselessNesting, innerCallName)), new NormalizationFix(innerArguments[0].getText()));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    };
}
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 29 with StringLiteralExpression

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

the class StrTrUsageAsStrReplaceInspector 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("strtr")) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length == 3) {
                    final StringLiteralExpression search = ExpressionSemanticUtil.resolveAsStringLiteral(arguments[1]);
                    if (search != null) {
                        final String content = search.getContents();
                        if (!content.isEmpty() && content.length() <= 2) {
                            final boolean isTarget;
                            if (search.isSingleQuote()) {
                                isTarget = signleQuoted.matcher(content).matches();
                            } else {
                                isTarget = doubleQuoted.matcher(content).matches();
                            }
                            if (isTarget) {
                                final String replacement = String.format("%sstr_replace(%s, %s, %s)", reference.getImmediateNamespaceName(), arguments[1].getText(), arguments[2].getText(), arguments[0].getText());
                                holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), replacement), new UseStringReplaceFix(replacement));
                            }
                        }
                    }
                }
            }
        }
    };
}
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 30 with StringLiteralExpression

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

the class GetTypeMissUseInspector 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("gettype")) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length == 1) {
                    final PsiElement parent = reference.getParent();
                    if (parent instanceof BinaryExpression) {
                        final BinaryExpression expression = (BinaryExpression) parent;
                        final IElementType operator = expression.getOperationType();
                        if (OpenapiTypesUtil.tsCOMPARE_EQUALITY_OPS.contains(operator)) {
                            final PsiElement candidate = OpenapiElementsUtil.getSecondOperand(expression, reference);
                            final StringLiteralExpression value = ExpressionSemanticUtil.resolveAsStringLiteral(candidate);
                            if (value != null) {
                                final String type = value.getContents();
                                if (!mapping.containsKey(type)) {
                                    /* edge case: compared string is wrong xD - bug */
                                    if (!type.equals("unknown type") && !type.equals("resource (closed)")) {
                                        holder.registerProblem(value, String.format(MessagesPresentationUtil.prefixWithEa(messageInvalidPattern), type), ProblemHighlightType.GENERIC_ERROR);
                                    }
                                } else {
                                    final boolean isInverted = operator == PhpTokenTypes.opNOT_EQUAL || operator == PhpTokenTypes.opNOT_IDENTICAL;
                                    final String replacement = String.format("%s%s(%s)", isInverted ? "!" : "", mapping.get(type), arguments[0].getText());
                                    holder.registerProblem(parent, String.format(MessagesPresentationUtil.prefixWithEa(messageUseFunctionPattern), replacement), new UseSuggestedFunctionFix(replacement));
                                }
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : IElementType(com.intellij.psi.tree.IElementType) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) 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)

Aggregations

StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)39 PsiElement (com.intellij.psi.PsiElement)35 NotNull (org.jetbrains.annotations.NotNull)28 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)18 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)16 HashSet (java.util.HashSet)8 PhpElementVisitor (com.jetbrains.php.lang.psi.visitors.PhpElementVisitor)7 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)6 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)6 ArrayCreationExpression (com.jetbrains.php.lang.psi.elements.ArrayCreationExpression)6 PhpPsiElement (com.jetbrains.php.lang.psi.elements.PhpPsiElement)6 Set (java.util.Set)6 Project (com.intellij.openapi.project.Project)5 PhpElementTypes (com.jetbrains.php.lang.parser.PhpElementTypes)5 GroupNames (com.intellij.codeInsight.daemon.GroupNames)4 PlatformPatterns (com.intellij.patterns.PlatformPatterns)4 PsiFile (com.intellij.psi.PsiFile)4 FilenameIndex (com.intellij.psi.search.FilenameIndex)4 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)4 IElementType (com.intellij.psi.tree.IElementType)4