Search in sources :

Example 11 with StringLiteralExpression

use of com.jetbrains.php.lang.psi.elements.StringLiteralExpression in project idea-php-typo3-plugin by cedricziel.

the class RouteFoldingBuilder method buildFoldRegions.

@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
    FoldingGroup group = FoldingGroup.newGroup("TYPO3Route");
    List<FoldingDescriptor> descriptors = new ArrayList<>();
    Collection<StringLiteralExpression> literalExpressions = PsiTreeUtil.findChildrenOfType(root, StringLiteralExpression.class);
    for (final StringLiteralExpression literalExpression : literalExpressions) {
        for (PsiReference reference : literalExpression.getReferences()) {
            if (reference instanceof RouteReference) {
                String value = literalExpression.getContents();
                FoldingDescriptor descriptor = foldRouteReferenceString(reference, value, group);
                if (descriptor != null) {
                    descriptors.add(descriptor);
                }
            }
        }
    }
    return descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldingGroup(com.intellij.openapi.editor.FoldingGroup) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with StringLiteralExpression

use of com.jetbrains.php.lang.psi.elements.StringLiteralExpression in project idea-php-typo3-plugin by cedricziel.

the class TranslationFoldingBuilder method buildFoldRegions.

@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
    FoldingGroup group = FoldingGroup.newGroup("TYPO3Translation");
    List<FoldingDescriptor> descriptors = new ArrayList<>();
    Collection<StringLiteralExpression> literalExpressions = PsiTreeUtil.findChildrenOfType(root, StringLiteralExpression.class);
    for (final StringLiteralExpression literalExpression : literalExpressions) {
        String value = literalExpression.getContents();
        if (value.startsWith("LLL:")) {
            Project project = literalExpression.getProject();
            final List<StubTranslation> properties = TranslationIndex.findById(project, value);
            StubTranslation defaultTranslation = findDefaultTranslationFromVariants(properties);
            if (defaultTranslation != null) {
                TextRange foldingRange = new TextRange(literalExpression.getTextRange().getStartOffset() + 1, literalExpression.getTextRange().getEndOffset() - 1);
                descriptors.add(new FoldingDescriptor(literalExpression.getNode(), foldingRange, group) {

                    @Nullable
                    @Override
                    public String getPlaceholderText() {
                        PsiElement[] definitionElements = TranslationUtil.findDefinitionElements(project, value);
                        for (PsiElement definitionElement : definitionElements) {
                            if (definitionElement instanceof XmlTag) {
                                if (((XmlTag) definitionElement).getName().equals("label")) {
                                    return ((XmlTag) definitionElement).getValue().getTrimmedText();
                                }
                                for (XmlTag xmlTag : ((XmlTag) definitionElement).getSubTags()) {
                                    if (xmlTag.getName().equals("source")) {
                                        return xmlTag.getValue().getTrimmedText();
                                    }
                                }
                            }
                            if (definitionElement instanceof XmlAttributeValue) {
                                if (((XmlTag) definitionElement.getParent().getParent()).getName().equals("label")) {
                                    return ((XmlTag) definitionElement).getValue().getTrimmedText();
                                }
                                for (XmlTag xmlTag : ((XmlTag) definitionElement.getParent().getParent()).getSubTags()) {
                                    if (xmlTag.getName().equals("source")) {
                                        return xmlTag.getValue().getTrimmedText();
                                    }
                                }
                            }
                        }
                        return null;
                    }
                });
            }
        }
    }
    return descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldingGroup(com.intellij.openapi.editor.FoldingGroup) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Project(com.intellij.openapi.project.Project) Nullable(org.jetbrains.annotations.Nullable) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with StringLiteralExpression

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

the class FixedTimeStartWithInspector 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)) {
                final PsiElement parent = reference.getParent();
                if (parent instanceof BinaryExpression) {
                    final BinaryExpression binary = (BinaryExpression) parent;
                    final IElementType operator = binary.getOperationType();
                    if (operator == PhpTokenTypes.opIDENTICAL || operator == PhpTokenTypes.opNOT_IDENTICAL) {
                        final PsiElement[] arguments = reference.getParameters();
                        if (arguments.length == 2 && arguments[1] instanceof StringLiteralExpression) {
                            final PsiElement zeroCandidate = OpenapiElementsUtil.getSecondOperand(binary, reference);
                            if (zeroCandidate != null && zeroCandidate.getText().equals("0")) {
                                final StringLiteralExpression literal = (StringLiteralExpression) arguments[1];
                                if (literal.getFirstPsiChild() == null) {
                                    final String replacement = String.format("%s(%s, %s, %s)", mapping.get(functionName), arguments[0].getText(), arguments[1].getText(), PhpStringUtil.unescapeText(literal.getContents(), literal.isSingleQuote()).length());
                                    holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), replacement), new UseFirstCharactersCompareFix(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)

Example 14 with StringLiteralExpression

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

the class TypeUnsafeArraySearchInspector 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 && targetFunctions.contains(functionName)) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length == 2) {
                    /* false-positives: array of string literals */
                    if (arguments[1] instanceof ArrayCreationExpression) {
                        final PsiElement[] elements = arguments[1].getChildren();
                        if (elements.length > 0) {
                            final long validElementsCount = Arrays.stream(elements).filter(element -> OpenapiTypesUtil.is(element, PhpElementTypes.ARRAY_VALUE)).map(PsiElement::getFirstChild).filter(element -> element instanceof StringLiteralExpression).map(literal -> ((StringLiteralExpression) literal).getContents().trim()).filter(content -> !content.isEmpty() && !content.matches("^\\d+$")).count();
                            if (validElementsCount == elements.length) {
                                return;
                            }
                        }
                    }
                    /* false-positives: array and item types are complimentary */
                    if (arguments[0] instanceof PhpTypedElement && arguments[1] instanceof PhpTypedElement) {
                        final Project project = holder.getProject();
                        final PhpType arrayType = OpenapiResolveUtil.resolveType((PhpTypedElement) arguments[1], project);
                        final Set<String> arrayTypes = arrayType == null ? null : arrayType.filterUnknown().getTypes();
                        if (arrayTypes != null && arrayTypes.size() == 1) {
                            final PhpType itemType = OpenapiResolveUtil.resolveType((PhpTypedElement) arguments[0], project);
                            final Set<String> itemTypes = itemType == null ? null : itemType.filterUnknown().getTypes();
                            if (itemTypes != null && itemTypes.size() == 1) {
                                final boolean matching = this.areTypesMatching(itemTypes.iterator().next(), arrayTypes.iterator().next());
                                if (matching) {
                                    return;
                                }
                            }
                        }
                    }
                    /* general case: we need the third argument */
                    final String replacement = String.format("%s%s(%s, %s, true)", reference.getImmediateNamespaceName(), functionName, arguments[0].getText(), arguments[1].getText());
                    holder.registerProblem(reference, MessagesPresentationUtil.prefixWithEa(message), new MakeSearchTypeSensitiveFix(replacement));
                }
            }
        }

        private boolean areTypesMatching(@NotNull String itemType, @NotNull String arrayType) {
            boolean result = false;
            if (!itemType.isEmpty()) {
                result = arrayType.equals((itemType.charAt(0) == '\\' ? itemType : '\\' + itemType) + "[]");
            }
            return result;
        }
    };
}
Also used : Arrays(java.util.Arrays) BasePhpInspection(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection) UseSuggestedReplacementFixer(com.kalessil.phpStorm.phpInspectionsEA.fixers.UseSuggestedReplacementFixer) Set(java.util.Set) PhpTypedElement(com.jetbrains.php.lang.psi.elements.PhpTypedElement) HashSet(java.util.HashSet) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) OpenapiTypesUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiTypesUtil) OpenapiResolveUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiResolveUtil) MessagesPresentationUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.MessagesPresentationUtil) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) NotNull(org.jetbrains.annotations.NotNull) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) PhpElementTypes(com.jetbrains.php.lang.parser.PhpElementTypes) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) ArrayCreationExpression(com.jetbrains.php.lang.psi.elements.ArrayCreationExpression) ArrayCreationExpression(com.jetbrains.php.lang.psi.elements.ArrayCreationExpression) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) NotNull(org.jetbrains.annotations.NotNull) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) Project(com.intellij.openapi.project.Project) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) PhpTypedElement(com.jetbrains.php.lang.psi.elements.PhpTypedElement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with StringLiteralExpression

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

the class DateIntervalSpecificationInspector method buildVisitor.

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

        @Override
        public void visitPhpNewExpression(@NotNull NewExpression expression) {
            /* before inspecting check parameters amount */
            final PsiElement[] params = expression.getParameters();
            if (params.length == 1) {
                final ClassReference classReference = expression.getClassReference();
                final String classFQN = classReference == null ? null : classReference.getFQN();
                if (classFQN == null || !classFQN.equals("\\DateInterval")) {
                    /* TODO: child classes support */
                    return;
                }
                /* now try getting string literal and test against valid patterns */
                final StringLiteralExpression pattern = ExpressionSemanticUtil.resolveAsStringLiteral(params[0]);
                if (pattern != null && pattern.getFirstPsiChild() == null) {
                    final String input = pattern.getContents();
                    if (!regexRegular.matcher(input).find() && !regexDateTimeAlike.matcher(input).find()) {
                        holder.registerProblem(pattern, MessagesPresentationUtil.prefixWithEa(message));
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) NewExpression(com.jetbrains.php.lang.psi.elements.NewExpression) ClassReference(com.jetbrains.php.lang.psi.elements.ClassReference) 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