Search in sources :

Example 6 with PhpElementVisitor

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

the class ClassNameMatcherInspection method buildVisitor.

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

        @Override
        public void visitPhpElement(PhpPsiElement element) {
            if (!PlatformPatterns.psiElement(PhpElementTypes.CLASS_REFERENCE).accepts(element)) {
                return;
            }
            Set<String> constants = getDeprecatedClasses(element);
            ClassReference classReference = (ClassReference) element;
            if (constants.contains(classReference.getFQN())) {
                problemsHolder.registerProblem(element, "Class removed with TYPO3 9, consider using an alternative");
            }
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) ClassReference(com.jetbrains.php.lang.psi.elements.ClassReference) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with PhpElementVisitor

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

the class ConstantMatcherInspection method buildVisitor.

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

        @Override
        public void visitPhpElement(PhpPsiElement element) {
            if (!PlatformPatterns.psiElement(PhpElementTypes.CONSTANT_REF).accepts(element)) {
                return;
            }
            ConstantReference constantReference = (ConstantReference) element;
            Set<String> constants = getRemovedConstantsFQNs(constantReference);
            if (constants.contains(constantReference.getFQN())) {
                problemsHolder.registerProblem(element, "Constant removed with TYPO3 9, consider using an alternative");
            }
        }
    };
}
Also used : ConstantReference(com.jetbrains.php.lang.psi.elements.ConstantReference) PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with PhpElementVisitor

use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project phpinspectionsea by kalessil.

the class MultipleReturnStatementsInspector method buildVisitor.

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

        @Override
        public void visitPhpMethod(@NotNull Method method) {
            final PhpClass clazz = method.getContainingClass();
            final PsiElement nameIdentifier = NamedElementUtil.getNameIdentifier(method);
            if (nameIdentifier != null && clazz != null && !clazz.isTrait()) {
                final PhpExitPointInstruction exitPoint = method.getControlFlow().getExitPoint();
                int returnsCount = 0;
                for (final PhpInstruction instruction : OpenapiElementsUtil.getPredecessors(exitPoint)) {
                    if (instruction instanceof PhpReturnInstruction) {
                        ++returnsCount;
                    }
                }
                if (returnsCount > 1) {
                    final ProblemHighlightType level = returnsCount > 3 ? ProblemHighlightType.GENERIC_ERROR : ProblemHighlightType.GENERIC_ERROR_OR_WARNING;
                    final String message = String.format(messagePattern, returnsCount);
                    holder.registerProblem(nameIdentifier, message, level);
                }
            }
        }
    };
}
Also used : PhpInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpInstruction) PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) Method(com.jetbrains.php.lang.psi.elements.Method) PhpReturnInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpReturnInstruction) PhpExitPointInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpExitPointInstruction) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with PhpElementVisitor

use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project yii2support by nvlad.

the class ViewMissedPhpDocInspection method buildVisitor.

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

        @Override
        public void visitPhpFile(PhpFile PhpFile) {
            Project project = PhpFile.getProject();
            ViewResolve resolve = ViewUtil.resolveView(PhpFile.getVirtualFile(), project);
            if (resolve == null) {
                return;
            }
            Map<String, String> params = getVariables(PhpFile);
            Map<String, String> declaredVariables = new HashMap<>();
            Collection<PhpDocVariable> variableCollection = PsiTreeUtil.findChildrenOfType(PhpFile, PhpDocVariable.class);
            for (PhpDocVariable variable : variableCollection) {
                declaredVariables.put(variable.getName(), variable.getType().toString());
            }
            Map<String, String> missedVariables = new HashMap<>();
            for (String variableName : params.keySet()) {
                if (!declaredVariables.containsKey(variableName)) {
                    missedVariables.put(variableName, params.get(variableName));
                }
            }
            if (missedVariables.isEmpty()) {
                return;
            }
            String problemDescription = "Missed View variable declaration.";
            ViewMissedPhpDocLocalQuickFix quickFix = new ViewMissedPhpDocLocalQuickFix(PhpFile, missedVariables);
            problemsHolder.registerProblem(PhpFile, problemDescription, quickFix);
        }

        private Map<String, String> getVariables(PhpFile phpFile) {
            Collection<PsiReference> references = ReferencesSearch.search(phpFile).findAll();
            Map<String, String> result = new HashMap<>();
            Map<String, PhpType> viewArgumentCollection = new LinkedHashMap<>();
            for (PsiReference reference : references) {
                MethodReference methodReference = PsiTreeUtil.getParentOfType(reference.getElement(), MethodReference.class);
                if (methodReference == null) {
                    continue;
                }
                Map<String, PhpType> params = RenderUtil.getViewArguments(methodReference);
                for (Map.Entry<String, PhpType> entry : params.entrySet()) {
                    if (viewArgumentCollection.containsKey(entry.getKey())) {
                        PhpType.PhpTypeBuilder typeBuilder = new PhpType.PhpTypeBuilder();
                        typeBuilder.add(viewArgumentCollection.get(entry.getKey()));
                        typeBuilder.add(entry.getValue());
                        viewArgumentCollection.replace(entry.getKey(), typeBuilder.build());
                    } else {
                        viewArgumentCollection.put(entry.getKey(), entry.getValue());
                    }
                }
            }
            for (String key : viewArgumentCollection.keySet()) {
                result.put(key, viewArgumentCollection.get(key).toString());
            }
            return result;
        }
    };
}
Also used : PhpFile(com.jetbrains.php.lang.psi.PhpFile) PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ViewResolve(com.nvlad.yii2support.views.entities.ViewResolve) PsiReference(com.intellij.psi.PsiReference) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) LinkedHashMap(java.util.LinkedHashMap) Project(com.intellij.openapi.project.Project) PhpDocVariable(com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocVariable) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with PhpElementVisitor

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

Aggregations

PhpElementVisitor (com.jetbrains.php.lang.psi.visitors.PhpElementVisitor)17 NotNull (org.jetbrains.annotations.NotNull)17 PsiElement (com.intellij.psi.PsiElement)9 PhpPsiElement (com.jetbrains.php.lang.psi.elements.PhpPsiElement)5 Project (com.intellij.openapi.project.Project)4 MethodReference (com.jetbrains.php.lang.psi.elements.MethodReference)4 PhpClass (com.jetbrains.php.lang.psi.elements.PhpClass)4 ViewResolve (com.nvlad.yii2support.views.entities.ViewResolve)4 PhpIndex (com.jetbrains.php.PhpIndex)3 ViewInfo (com.nvlad.yii2support.views.entities.ViewInfo)3 ClassReference (com.jetbrains.php.lang.psi.elements.ClassReference)2 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)2 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)2 PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)2 HashSet (java.util.HashSet)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemHighlightType (com.intellij.codeInspection.ProblemHighlightType)1 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDirectory (com.intellij.psi.PsiDirectory)1