Search in sources :

Example 1 with PhpElementVisitor

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

the class MissedViewInspection 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;
            }
            if (ArrayUtil.contains(reference.getName(), ViewUtil.renderMethods)) {
                if (reference.getParameters().length > 0) {
                    final PsiElement pathParameter = reference.getParameters()[0];
                    final ViewResolve resolve = ViewUtil.resolveView(pathParameter);
                    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());
                    final boolean localViewSearch;
                    final String value = PhpUtil.getValue(pathParameter);
                    if (resolve.from == ViewResolveFrom.View) {
                        localViewSearch = !value.startsWith("@") && !value.startsWith("//");
                    } else {
                        localViewSearch = false;
                    }
                    views.removeIf(view -> {
                        if (!application.equals(view.application)) {
                            return true;
                        }
                        return localViewSearch && !resolve.theme.equals(view.theme);
                    });
                    if (views.size() != 0) {
                        return;
                    }
                    if (pathParameter instanceof StringLiteralExpression) {
                        Collection<String> paths = ViewUtil.viewResolveToPaths(resolve, project);
                        if (!paths.iterator().hasNext()) {
                            return;
                        }
                        VirtualFile yiiRoot = YiiApplicationUtils.getYiiRootVirtualFile(project);
                        if (yiiRoot == null) {
                            return;
                        }
                        int projectUrlLength = project.getBaseDir().getUrl().length();
                        String yiiRootUrl = yiiRoot.getUrl();
                        String path;
                        if (projectUrlLength > yiiRootUrl.length()) {
                            path = paths.iterator().next();
                        } else {
                            path = yiiRootUrl.substring(projectUrlLength) + paths.iterator().next();
                        }
                        final String viewNotFoundMessage = "View file for \"" + value + "\" not found in \"" + path + "\".";
                        final MissedViewLocalQuickFix quickFix = new MissedViewLocalQuickFix(value, path, RenderUtil.getViewArguments(reference));
                        final PsiElement stringPart = pathParameter.findElementAt(1);
                        if (stringPart != null) {
                            problemsHolder.registerProblem(stringPart, viewNotFoundMessage, quickFix);
                        }
                    }
                }
            }
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) ViewResolve(com.nvlad.yii2support.views.entities.ViewResolve) ViewInfo(com.nvlad.yii2support.views.entities.ViewInfo) Project(com.intellij.openapi.project.Project) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PhpElementVisitor

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

the class MissedParamInspection method buildVisitor.

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

        @Override
        public void visitPhpMethodReference(MethodReference reference) {
            if (reference != null && reference.getParameters().length > 0) {
                Method method = (Method) reference.resolve();
                if (method == null)
                    return;
                int paramParameterIndex = ClassUtils.getParamIndex(method, "params");
                int conditionParameterIndex = ClassUtils.getParamIndex(method, new String[] { "condition", "expression", "sql" });
                if (paramParameterIndex > -1 && conditionParameterIndex > -1 && reference.getParameters().length > conditionParameterIndex && conditionParameterIndex == paramParameterIndex - 1) {
                    PsiElement element = reference.getParameters()[conditionParameterIndex];
                    String condition = ClassUtils.getStringByElement(element);
                    String[] conditionParams = DatabaseUtils.extractParamsFromCondition(condition);
                    String[] conditionParamsWithoutColon = DatabaseUtils.extractParamsFromCondition(condition, false);
                    if (conditionParams.length > 0) {
                        if (reference.getParameters().length > paramParameterIndex) {
                            PsiElement paramParam = reference.getParameters()[paramParameterIndex];
                            if (paramParam instanceof ArrayCreationExpression) {
                                ArrayCreationExpression array = (ArrayCreationExpression) paramParam;
                                ArrayList<String> paramString = new ArrayList<>();
                                for (ArrayHashElement elem : array.getHashElements()) {
                                    if (elem.getKey() != null && elem.getKey().getText() != null)
                                        paramString.add(ClassUtils.removeQuotes(elem.getKey().getText()).trim());
                                }
                                if (!Arrays.equals(paramString.toArray(), conditionParams) && !Arrays.equals(paramString.toArray(), conditionParamsWithoutColon)) {
                                    MissedParamQuickFix qFix = new MissedParamQuickFix(reference);
                                    problemsHolder.registerProblem(reference.getParameters()[paramParameterIndex], "Condition parameters do not conform to the condition", qFix);
                                }
                            }
                        } else {
                            MissedParamQuickFix qFix = new MissedParamQuickFix(reference);
                            problemsHolder.registerProblem(reference.getParameters()[conditionParameterIndex], "Condition parameters must be defined", qFix);
                        }
                    }
                }
            }
            super.visitPhpMethodReference(reference);
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PhpElementVisitor

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

the class MissingActiveRecordInActiveQueryInspection method buildVisitor.

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

        @Override
        public void visitPhpClass(PhpClass clazz) {
            if (clazz.getSuperClass() != null && clazz.getSuperClass().getFQN().equals("\\yii\\db\\ActiveQuery")) {
                PhpIndex index = PhpIndex.getInstance(clazz.getProject());
                PhpClass activeRecordClass = ClassUtils.findClassInSeeTags(index, clazz, "\\yii\\db\\BaseActiveRecord");
                if (activeRecordClass == null) {
                    problemsHolder.registerProblem(clazz.getFirstChild(), "Can not find connected ActiveRecord class.\nYou should add @see tag with linked ActiveRecord", 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 4 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 5 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)

Aggregations

PhpElementVisitor (com.jetbrains.php.lang.psi.visitors.PhpElementVisitor)15 NotNull (org.jetbrains.annotations.NotNull)15 PsiElement (com.intellij.psi.PsiElement)7 PhpPsiElement (com.jetbrains.php.lang.psi.elements.PhpPsiElement)6 Project (com.intellij.openapi.project.Project)4 MethodReference (com.jetbrains.php.lang.psi.elements.MethodReference)4 ViewResolve (com.nvlad.yii2support.views.entities.ViewResolve)4 PhpIndex (com.jetbrains.php.PhpIndex)3 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)3 ViewInfo (com.nvlad.yii2support.views.entities.ViewInfo)3 ArrayCreationExpression (com.jetbrains.php.lang.psi.elements.ArrayCreationExpression)2 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)2 PhpClass (com.jetbrains.php.lang.psi.elements.PhpClass)2 PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiReference (com.intellij.psi.PsiReference)1