Search in sources :

Example 16 with StringLiteralExpression

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

the class StrtotimeUsageInspector 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("strtotime")) {
                return;
            }
            final PsiElement[] arguments = reference.getParameters();
            if (arguments.length == 0 || arguments.length > 2) {
                return;
            }
            /* handle case: strtotime("now") -> time() */
            if (arguments.length == 1) {
                if (arguments[0] instanceof StringLiteralExpression) {
                    final StringLiteralExpression pattern = (StringLiteralExpression) arguments[0];
                    if (pattern.getContents().equalsIgnoreCase("now")) {
                        holder.registerProblem(reference, MessagesPresentationUtil.prefixWithEa(messageUseTime), new UseTimeFunctionLocalFix());
                    }
                }
            } else /* handle case: strtotime(..., time()) -> date(...) */
            if (arguments.length == 2) {
                if (OpenapiTypesUtil.isFunctionReference(arguments[1])) {
                    final FunctionReference candidate = (FunctionReference) arguments[1];
                    final String candidateName = candidate.getName();
                    if (candidateName != null && candidateName.equals("time") && this.isFromRootNamespace(candidate)) {
                        final String replacement = "strtotime(%a%)".replace("%a%", arguments[0].getText());
                        holder.registerProblem(reference, MessagesPresentationUtil.prefixWithEa(messageDropTime), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new DropTimeFunctionCallLocalFix(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 17 with StringLiteralExpression

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

the class DynamicCallsToScopeIntrospectionInspector method buildVisitor.

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

        @Override
        public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
            if (PhpLanguageLevel.get(holder.getProject()).atLeast(PhpLanguageLevel.PHP710)) {
                final String functionName = reference.getName();
                if (functionName != null) {
                    /* discover target element */
                    final PsiElement target;
                    if (functionName.isEmpty()) {
                        final PsiElement[] children = reference.getChildren();
                        target = children.length == 2 ? children[0] : null;
                    } else if (callbacksPositions.containsKey(functionName)) {
                        final int callbackPosition = callbacksPositions.get(functionName);
                        final PsiElement[] arguments = reference.getParameters();
                        target = arguments.length >= callbackPosition + 1 ? arguments[callbackPosition] : null;
                    } else {
                        target = null;
                    }
                    /* discover the target function */
                    if (target != null) {
                        final StringLiteralExpression literal = ExpressionSemanticUtil.resolveAsStringLiteral(target);
                        if (literal != null) {
                            final String raw = PhpStringUtil.unescapeText(literal.getContents(), literal.isSingleQuote());
                            final String callback = raw.startsWith("\\") ? raw.substring(1) : raw;
                            if (targetCalls.containsKey(callback)) {
                                holder.registerProblem(target, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), callback));
                            }
                        }
                    }
                }
            }
        }
    };
}
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 18 with StringLiteralExpression

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

the class FopenBinaryUnsafeUsageInspector 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("fopen")) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length >= 2) {
                    /* verify if mode provided and has no 'b' already */
                    final StringLiteralExpression mode = ExpressionSemanticUtil.resolveAsStringLiteral(arguments[1]);
                    final String modeText = mode == null ? null : mode.getContents();
                    if (modeText != null && !modeText.isEmpty()) {
                        if (modeText.indexOf('b') != -1) {
                            final boolean isCorrectlyPlaced = modeText.endsWith("b") || modeText.endsWith("b+");
                            if (!isCorrectlyPlaced) {
                                holder.registerProblem(arguments[1], MessagesPresentationUtil.prefixWithEa(messageMisplacedBinaryMode), ProblemHighlightType.GENERIC_ERROR, new TheLocalFix(holder.getProject(), mode));
                            }
                        } else if (modeText.indexOf('t') != -1) {
                            if (ENFORCE_BINARY_MODIFIER_USAGE) {
                                holder.registerProblem(arguments[1], MessagesPresentationUtil.prefixWithEa(messageReplaceWithBinaryMode), new TheLocalFix(holder.getProject(), mode));
                            }
                        } else {
                            if (ENFORCE_BINARY_MODIFIER_USAGE) {
                                holder.registerProblem(arguments[1], MessagesPresentationUtil.prefixWithEa(messageUseBinaryMode), new TheLocalFix(holder.getProject(), mode));
                            }
                        }
                    }
                }
            }
        }
    };
}
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 19 with StringLiteralExpression

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

the class CaseInsensitiveStringFunctionsMissUseInspector 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 || !mapping.containsKey(functionName)) {
                return;
            }
            final PsiElement[] arguments = reference.getParameters();
            if (arguments.length != 2 && arguments.length != 3) {
                return;
            }
            // resolve second parameter
            final StringLiteralExpression pattern = ExpressionSemanticUtil.resolveAsStringLiteral(arguments[1]);
            // might be not available - in another file (PhpStorm limitations)
            if (pattern == null || pattern.getContainingFile() != arguments[1].getContainingFile()) {
                return;
            }
            final String patternString = pattern.getContents();
            if (!StringUtils.isEmpty(patternString) && !patternString.matches(".*\\p{L}.*")) {
                final String replacementFunctionName = mapping.get(functionName);
                holder.registerProblem(reference, MessagesPresentationUtil.prefixWithEa(messagePattern.replace("%f%", replacementFunctionName)), ProblemHighlightType.WEAK_WARNING, new TheLocalFix(replacementFunctionName));
            }
        }
    };
}
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 20 with StringLiteralExpression

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

the class ComparisonOperandsOrderInspector method buildVisitor.

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

        @Override
        public void visitPhpBinaryExpression(@NotNull BinaryExpression expression) {
            final IElementType operator = expression.getOperationType();
            if (operator != null && OpenapiTypesUtil.tsCOMPARE_EQUALITY_OPS.contains(operator)) {
                final PsiElement left = expression.getLeftOperand();
                final PsiElement right = expression.getRightOperand();
                if (left != null && right != null) {
                    final boolean isLeftConstant = left instanceof StringLiteralExpression || left instanceof ConstantReference || OpenapiTypesUtil.isNumber(left);
                    final boolean isRightConstant = right instanceof StringLiteralExpression || right instanceof ConstantReference || OpenapiTypesUtil.isNumber(right);
                    if (isLeftConstant != isRightConstant) {
                        final boolean isRegular = ComparisonStyle.isRegular();
                        if (isRightConstant && !isRegular) {
                            problemsHolder.registerProblem(expression, MessagesPresentationUtil.prefixWithEa(messageUseYoda), new TheLocalFix());
                        }
                        if (isLeftConstant && isRegular) {
                            problemsHolder.registerProblem(expression, MessagesPresentationUtil.prefixWithEa(messageUseRegular), new TheLocalFix());
                        }
                    }
                }
            }
        }
    };
}
Also used : IElementType(com.intellij.psi.tree.IElementType) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) ConstantReference(com.jetbrains.php.lang.psi.elements.ConstantReference) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) 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