Search in sources :

Example 31 with StringLiteralExpression

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

the class ArgumentUnpackingCanBeUsedInspector method buildVisitor.

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

        @Override
        public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
            final PhpLanguageLevel php = PhpProjectConfigurationFacade.getInstance(holder.getProject()).getLanguageLevel();
            if (php.compareTo(PhpLanguageLevel.PHP560) >= 0) {
                final String functionName = reference.getName();
                if (functionName != null && functionName.equals("call_user_func_array")) {
                    final PsiElement[] arguments = reference.getParameters();
                    if (arguments.length == 2) {
                        final boolean isContainerValid = arguments[1] instanceof Variable || arguments[1] instanceof ArrayCreationExpression || arguments[1] instanceof FunctionReference;
                        if (isContainerValid && arguments[0] instanceof StringLiteralExpression) {
                            /* do not process strings with injections */
                            final StringLiteralExpression targetFunction = (StringLiteralExpression) arguments[0];
                            if (targetFunction.getFirstPsiChild() == null) {
                                final String replacement = "%f%(...%a%)".replace("%a%", arguments[1].getText()).replace("%f%", targetFunction.getContents());
                                final String message = messagePattern.replace("%e%", replacement);
                                holder.registerProblem(reference, message, new UnpackFix(replacement));
                            }
                        }
                    }
                }
            }
        // TODO: if (isContainerValid && params[0] instanceof ArrayCreationExpression) {
        // TODO: call_user_func_array([...], ...); string method name must not contain ::
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) Variable(com.jetbrains.php.lang.psi.elements.Variable) ArrayCreationExpression(com.jetbrains.php.lang.psi.elements.ArrayCreationExpression) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PhpLanguageLevel(com.jetbrains.php.config.PhpLanguageLevel) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 32 with StringLiteralExpression

use of com.jetbrains.php.lang.psi.elements.StringLiteralExpression in project yii2support by nvlad.

the class RequireParameterInspection method buildVisitor.

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

        @Override
        public void visitPhpMethodReference(MethodReference reference) {
            if (!ViewUtil.isValidRenderMethod(reference)) {
                return;
            }
            final String name = reference.getName();
            if (name == null || !ArrayUtil.contains(name, ViewUtil.renderMethods)) {
                return;
            }
            final PsiElement[] renderParameters = reference.getParameters();
            if (renderParameters.length == 0 || !(renderParameters[0] instanceof StringLiteralExpression)) {
                return;
            }
            final ViewResolve resolve = ViewUtil.resolveView(renderParameters[0]);
            if (resolve == null) {
                return;
            }
            String key = resolve.key;
            if (Files.getFileExtension(key).isEmpty()) {
                key = key + '.' + Yii2SupportSettings.getInstance(reference.getProject()).defaultViewExtension;
            }
            final Project project = reference.getProject();
            final Collection<ViewInfo> views = FileBasedIndex.getInstance().getValues(ViewFileIndex.identity, key, GlobalSearchScope.projectScope(project));
            final String application = YiiApplicationUtils.getApplicationName(reference.getContainingFile());
            views.removeIf(viewInfo -> !application.equals(viewInfo.application));
            if (views.size() == 0) {
                return;
            }
            final Collection<String> viewParameters = new HashSet<>();
            for (ViewInfo view : views) {
                viewParameters.addAll(view.parameters);
            }
            if (viewParameters.size() == 0) {
                return;
            }
            final Collection<String> existKeys;
            if (renderParameters.length > 1) {
                if (renderParameters[1] instanceof ArrayCreationExpression) {
                    existKeys = PhpUtil.getArrayKeys((ArrayCreationExpression) renderParameters[1]);
                } else if (renderParameters[1] instanceof FunctionReference) {
                    FunctionReference function = (FunctionReference) renderParameters[1];
                    if (function.getName() != null && function.getName().equals("compact")) {
                        existKeys = new HashSet<>();
                        for (PsiElement element : function.getParameters()) {
                            if (element instanceof StringLiteralExpression) {
                                existKeys.add(((StringLiteralExpression) element).getContents());
                            }
                        }
                    } else {
                        return;
                    }
                } else {
                    return;
                }
            } else {
                existKeys = new HashSet<>();
            }
            viewParameters.removeIf(existKeys::contains);
            if (viewParameters.size() == 0) {
                return;
            }
            String description = "View " + renderParameters[0].getText() + " require ";
            final Iterator<String> parameterIterator = viewParameters.iterator();
            if (!isOnTheFly) {
                while (parameterIterator.hasNext()) {
                    final String parameter = parameterIterator.next();
                    final String problemDescription = description + "\"" + parameter + "\" parameter.";
                    problemsHolder.registerProblem(reference, problemDescription, new RequireParameterLocalQuickFix(parameter));
                }
                return;
            }
            final Collection<LocalQuickFix> fixes = new HashSet<>();
            if (viewParameters.size() > 1) {
                fixes.add(new RequireParameterLocalQuickFix(viewParameters.toArray(new String[0])));
                StringBuilder parameterString = new StringBuilder();
                String parameter = parameterIterator.next();
                while (parameterIterator.hasNext()) {
                    if (parameterString.length() > 0) {
                        parameterString.append(", ");
                    }
                    parameterString.append("\"").append(parameter).append("\"");
                    fixes.add(new RequireParameterLocalQuickFix(parameter));
                    parameter = parameterIterator.next();
                }
                parameterString.append(" and \"").append(parameter).append("\" parameters.");
                description += parameterString.toString();
                fixes.add(new RequireParameterLocalQuickFix(parameter));
            } else {
                String parameter = parameterIterator.next();
                description += "\"" + parameter + "\" parameter.";
                fixes.add(new RequireParameterLocalQuickFix(parameter));
            }
            problemsHolder.registerProblem(reference, description, fixes.toArray(new LocalQuickFix[0]));
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) ArrayCreationExpression(com.jetbrains.php.lang.psi.elements.ArrayCreationExpression) ViewResolve(com.nvlad.yii2support.views.entities.ViewResolve) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) ViewInfo(com.nvlad.yii2support.views.entities.ViewInfo) Project(com.intellij.openapi.project.Project) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with StringLiteralExpression

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

the class ClassNameMatcherInspection method getDeprecatedClasses.

private Set<String> getDeprecatedClasses(PhpPsiElement element) {
    Set<PsiElement> elements = new HashSet<>();
    PsiFile[] classNameMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), "ClassNameMatcher.php", GlobalSearchScope.allScope(element.getProject()));
    for (PsiFile file : classNameMatcherFiles) {
        Collections.addAll(elements, PsiTreeUtil.collectElements(file, el -> PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withAncestor(4, PlatformPatterns.psiElement(PhpElementTypes.RETURN))).accepts(el)));
    }
    return elements.stream().map(stringLiteral -> "\\" + ((StringLiteralExpression) stringLiteral).getContents()).collect(Collectors.toSet());
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) GroupNames(com.intellij.codeInsight.daemon.GroupNames) FilenameIndex(com.intellij.psi.search.FilenameIndex) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Set(java.util.Set) Collectors(java.util.stream.Collectors) PlatformPatterns(com.intellij.patterns.PlatformPatterns) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) HashSet(java.util.HashSet) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) ClassReference(com.jetbrains.php.lang.psi.elements.ClassReference) Nls(org.jetbrains.annotations.Nls) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) PhpInspection(com.jetbrains.php.lang.inspections.PhpInspection) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) Collections(java.util.Collections) PhpElementTypes(com.jetbrains.php.lang.parser.PhpElementTypes) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) PsiFile(com.intellij.psi.PsiFile) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet)

Example 34 with StringLiteralExpression

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

the class ConstantMatcherInspection method getRemovedConstantsFQNs.

private Set<String> getRemovedConstantsFQNs(ConstantReference element) {
    Set<PsiElement> elements = new HashSet<>();
    PsiFile[] constantMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), "ConstantMatcher.php", GlobalSearchScope.allScope(element.getProject()));
    for (PsiFile file : constantMatcherFiles) {
        Collections.addAll(elements, PsiTreeUtil.collectElements(file, el -> PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withAncestor(4, PlatformPatterns.psiElement(PhpElementTypes.RETURN))).accepts(el)));
    }
    return elements.stream().map(stringLiteral -> "\\" + ((StringLiteralExpression) stringLiteral).getContents()).collect(Collectors.toSet());
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) GroupNames(com.intellij.codeInsight.daemon.GroupNames) FilenameIndex(com.intellij.psi.search.FilenameIndex) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Set(java.util.Set) Collectors(java.util.stream.Collectors) PlatformPatterns(com.intellij.patterns.PlatformPatterns) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) HashSet(java.util.HashSet) ConstantReference(com.jetbrains.php.lang.psi.elements.ConstantReference) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Nls(org.jetbrains.annotations.Nls) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) PhpInspection(com.jetbrains.php.lang.inspections.PhpInspection) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) Collections(java.util.Collections) PhpElementTypes(com.jetbrains.php.lang.parser.PhpElementTypes) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) PsiFile(com.intellij.psi.PsiFile) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet)

Example 35 with StringLiteralExpression

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

the class FunctionCallMatcherInspection method getRemovedGlobalFuntions.

private Set<String> getRemovedGlobalFuntions(PhpPsiElement element) {
    Set<PsiElement> elements = new HashSet<>();
    PsiFile[] constantMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), "FunctionCallMatcher.php", GlobalSearchScope.allScope(element.getProject()));
    for (PsiFile file : constantMatcherFiles) {
        Collections.addAll(elements, PsiTreeUtil.collectElements(file, el -> PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withAncestor(4, PlatformPatterns.psiElement(PhpElementTypes.RETURN))).accepts(el)));
    }
    return elements.stream().map(stringLiteral -> "\\" + ((StringLiteralExpression) stringLiteral).getContents()).collect(Collectors.toSet());
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) GroupNames(com.intellij.codeInsight.daemon.GroupNames) FilenameIndex(com.intellij.psi.search.FilenameIndex) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Set(java.util.Set) Collectors(java.util.stream.Collectors) PlatformPatterns(com.intellij.patterns.PlatformPatterns) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) HashSet(java.util.HashSet) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Nls(org.jetbrains.annotations.Nls) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) PhpInspection(com.jetbrains.php.lang.inspections.PhpInspection) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) Collections(java.util.Collections) PhpElementTypes(com.jetbrains.php.lang.parser.PhpElementTypes) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) PsiFile(com.intellij.psi.PsiFile) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet)

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