Search in sources :

Example 11 with PhpElementVisitor

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

the class UnusedParameterInspection 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 < 2 || !(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) {
                if (renderParameters[1] instanceof ArrayCreationExpression || renderParameters[1] instanceof FunctionReference) {
                    String errorUnusedParameters = "This View does not use parameters";
                    UnusedParametersLocalQuickFix fix = new UnusedParametersLocalQuickFix();
                    problemsHolder.registerProblem(renderParameters[1], errorUnusedParameters, ProblemHighlightType.LIKE_UNUSED_SYMBOL, fix);
                    if (isOnTheFly) {
                        problemsHolder.registerProblem(reference, errorUnusedParameters, ProblemHighlightType.INFORMATION, fix);
                    }
                }
                return;
            }
            final String errorUnusedParameter = "View " + renderParameters[0].getText() + " not use \"%parameter%\" parameter";
            final Set<String> unusedParameters = new HashSet<>();
            BiConsumer<String, PsiElement> processParameter = (String arrayKey, PsiElement element) -> {
                if (!viewParameters.contains(arrayKey)) {
                    UnusedParameterLocalQuickFix fix = new UnusedParameterLocalQuickFix(arrayKey);
                    String description = errorUnusedParameter.replace("%parameter%", arrayKey);
                    problemsHolder.registerProblem(element, description, ProblemHighlightType.LIKE_UNUSED_SYMBOL, fix);
                    unusedParameters.add(arrayKey);
                }
            };
            if (renderParameters[1] instanceof ArrayCreationExpression) {
                for (ArrayHashElement element : ((ArrayCreationExpression) renderParameters[1]).getHashElements()) {
                    if (element.getKey() != null && element.getKey() instanceof StringLiteralExpression) {
                        final String arrayKey = ((StringLiteralExpression) element.getKey()).getContents();
                        processParameter.accept(arrayKey, element);
                    }
                }
            }
            if (renderParameters[1] instanceof FunctionReference) {
                FunctionReference function = ((FunctionReference) renderParameters[1]);
                if (function.getName() != null && function.getName().contains("compact")) {
                    for (PsiElement element : function.getParameters()) {
                        if (element instanceof StringLiteralExpression) {
                            String arrayKey = ((StringLiteralExpression) element).getContents();
                            processParameter.accept(arrayKey, element);
                        }
                    }
                }
            }
            if (unusedParameters.size() > 0 && isOnTheFly) {
                if (viewParameters.containsAll(unusedParameters)) {
                    String errorUnusedParameters = "This View does not use parameters";
                    UnusedParametersLocalQuickFix fix = new UnusedParametersLocalQuickFix();
                    problemsHolder.registerProblem(renderParameters[1], errorUnusedParameters, ProblemHighlightType.LIKE_UNUSED_SYMBOL, fix);
                    problemsHolder.registerProblem(reference, errorUnusedParameters, ProblemHighlightType.INFORMATION, fix);
                } else {
                    String errorUnusedParameters = "This View have unused parameters";
                    UnusedParametersLocalQuickFix fix = new UnusedParametersLocalQuickFix(unusedParameters);
                    problemsHolder.registerProblem(reference, errorUnusedParameters, ProblemHighlightType.INFORMATION, fix);
                }
            }
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) ViewResolve(com.nvlad.yii2support.views.entities.ViewResolve) ViewInfo(com.nvlad.yii2support.views.entities.ViewInfo) Project(com.intellij.openapi.project.Project) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with PhpElementVisitor

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

the class UndetectableTableInspection method buildVisitor.

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

        @Override
        public void visitPhpClass(PhpClass clazz) {
            PhpIndex index = PhpIndex.getInstance(problemsHolder.getProject());
            if (DatabaseUtils.HasConnections(problemsHolder.getProject()) && ClassUtils.isClassInheritsOrEqual(clazz, ClassUtils.getClass(index, "\\yii\\db\\ActiveRecord"))) {
                String table = DatabaseUtils.getTableByActiveRecordClass(clazz);
                if (table == null) {
                    problemsHolder.registerProblem(clazz.getFirstChild(), "Can not detect database table for class " + clazz.getFQN(), ProblemHighlightType.WEAK_WARNING);
                } else if (!DatabaseUtils.isTableExists(table, problemsHolder.getProject())) {
                    problemsHolder.registerProblem(clazz.getFirstChild(), "Table '" + table + "' not found in database connections", ProblemHighlightType.WEAK_WARNING);
                }
            }
            super.visitPhpClass(clazz);
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) PhpIndex(com.jetbrains.php.PhpIndex) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with PhpElementVisitor

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

the class PropertiesInspection method buildVisitor.

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

        @Override
        public void visitElement(PsiElement element) {
            if (element instanceof PhpDocComment && DatabaseUtils.HasConnections(element.getProject())) {
                PhpDocComment docComment = (PhpDocComment) element;
                PhpIndex index = PhpIndex.getInstance(element.getProject());
                PhpClass phpClass = DatabaseUtils.getClassByClassPhpDoc(docComment);
                if (phpClass != null && ClassUtils.isClassInheritsOrEqual(phpClass, ClassUtils.getClass(index, "\\yii\\db\\BaseActiveRecord"))) {
                    Collection<Field> fields = phpClass.getFields();
                    String table = DatabaseUtils.getTableByActiveRecordClass(phpClass);
                    ArrayList<VirtualProperty> notDeclaredColumns = DatabaseUtils.getNotDeclaredColumns(table, fields, element.getProject());
                    if (notDeclaredColumns.size() > 0) {
                        MissingPropertiesQuickFix qFix = new MissingPropertiesQuickFix(notDeclaredColumns, docComment);
                        String str1 = notDeclaredColumns.size() > 1 ? "properties" : "property";
                        problemsHolder.registerProblem(docComment, "Class " + phpClass.getFQN() + " is missing " + notDeclaredColumns.size() + " " + str1 + " that corresponds to database columns", ProblemHighlightType.WEAK_WARNING, qFix);
                    }
                    ArrayList<PhpDocPropertyTag> unusedProperties = DatabaseUtils.getUnusedProperties(table, docComment.getPropertyTags(), phpClass);
                    if (unusedProperties.size() > 0) {
                        for (PhpDocPropertyTag tag : unusedProperties) {
                            problemsHolder.registerProblem(tag, "Property is unused in class " + phpClass.getFQN(), ProblemHighlightType.LIKE_UNUSED_SYMBOL);
                        }
                    }
                }
            }
            super.visitElement(element);
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) VirtualProperty(com.nvlad.yii2support.common.VirtualProperty) PhpIndex(com.jetbrains.php.PhpIndex) PhpDocPropertyTag(com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocPropertyTag) PhpDocComment(com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with PhpElementVisitor

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

the class ObjectFactoryMissedFieldInspection method buildVisitor.

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

        @Override
        public void visitPhpArrayCreationExpression(ArrayCreationExpression expression) {
            PsiDirectory dir = expression.getContainingFile().getContainingDirectory();
            PhpClass phpClass = ObjectFactoryUtils.findClassByArrayCreation(expression, dir);
            if (phpClass != null && !phpClass.getFQN().equals("\\" + phpClass.getName())) {
                // Avoid System Classes: \Closure, \ArrayAccess
                for (ArrayHashElement elem : expression.getHashElements()) {
                    PsiElement key = elem.getKey();
                    if (key != null) {
                        String keyName = (key instanceof ClassConstantReference || key instanceof ConstantReference) ? ClassUtils.getConstantValue(key) : key.getText();
                        if (keyName != null) {
                            keyName = ClassUtils.removeQuotes(keyName);
                            if (keyName != null && !keyName.equals("class") && !keyName.startsWith("as ") && !keyName.startsWith("on ") && ClassUtils.findWritableField(phpClass, keyName) == null) {
                                final String descriptionTemplate = "Field '" + keyName + "' not exists in referenced class " + phpClass.getFQN();
                                problemsHolder.registerProblem(elem, descriptionTemplate);
                            }
                        }
                    }
                }
            }
            super.visitPhpArrayCreationExpression(expression);
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) PsiDirectory(com.intellij.psi.PsiDirectory) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with PhpElementVisitor

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

the class ClassConstantMatcherInspection 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_CONSTANT_REFERENCE).accepts(element)) {
                return;
            }
            Set<String> constants = getDeprecatedClassConstants(element);
            ClassConstantReference classConstantReference = (ClassConstantReference) element;
            if (constants.contains(classConstantReference.getText())) {
                problemsHolder.registerProblem(element, "Deprecated class constant");
            }
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) ClassConstantReference(com.jetbrains.php.lang.psi.elements.ClassConstantReference) 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