Search in sources :

Example 16 with MethodReference

use of com.jetbrains.php.lang.psi.elements.MethodReference 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 17 with MethodReference

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

the class MethodArgumentDroppedMatcherInspection 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.METHOD_REFERENCE).accepts(element)) {
                return;
            }
            MethodReference methodReference = (MethodReference) element;
            ParameterList parameterList = methodReference.getParameterList();
            PhpExpression classReference = methodReference.getClassReference();
            if (classReference != null) {
                PhpType inferredType = classReference.getInferredType();
                String compiledClassMethodKey = inferredType.toString() + "->" + methodReference.getName();
                if (ExtensionScannerUtil.classMethodHasDroppedArguments(element.getProject(), compiledClassMethodKey)) {
                    Integer maximumNumberOfArguments = ExtensionScannerUtil.getMaximumNumberOfArguments(element.getProject(), compiledClassMethodKey);
                    if (parameterList != null && maximumNumberOfArguments != -1 && parameterList.getParameters().length != maximumNumberOfArguments) {
                        problemsHolder.registerProblem(element, "Number of arguments changed with TYPO3 9, consider refactoring");
                    }
                }
            }
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) PhpExpression(com.jetbrains.php.lang.psi.elements.PhpExpression) ParameterList(com.jetbrains.php.lang.psi.elements.ParameterList) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with MethodReference

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

the class GeneralUtilityServiceTypeProvider method getType.

@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
    if (DumbService.getInstance(psiElement.getProject()).isDumb()) {
        return null;
    }
    if (!(psiElement instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(psiElement, "makeInstanceService")) {
        return null;
    }
    MethodReference methodReference = (MethodReference) psiElement;
    if (methodReference.getParameters().length == 0) {
        return null;
    }
    PsiElement firstParam = methodReference.getParameters()[0];
    if (firstParam instanceof StringLiteralExpression) {
        StringLiteralExpression ref = (StringLiteralExpression) firstParam;
        String serviceId = ref.getContents();
        return new PhpType().add("#" + this.getKey() + serviceId);
    }
    return null;
}
Also used : StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PsiElement(com.intellij.psi.PsiElement) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with MethodReference

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

the class GeneralUtilityTypeProvider method getType.

@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
    if (DumbService.getInstance(psiElement.getProject()).isDumb()) {
        return null;
    }
    if (!(psiElement instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(psiElement, "makeInstance")) {
        return null;
    }
    MethodReference methodReference = (MethodReference) psiElement;
    if (methodReference.getParameters().length == 0) {
        return null;
    }
    PsiElement firstParam = methodReference.getParameters()[0];
    if (firstParam instanceof PhpReference) {
        PhpReference ref = (PhpReference) firstParam;
        if (ref.getText().toLowerCase().contains("::class")) {
            return new PhpType().add("#" + this.getKey() + ref.getSignature());
        }
    }
    return null;
}
Also used : PhpReference(com.jetbrains.php.lang.psi.elements.PhpReference) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PsiElement(com.intellij.psi.PsiElement) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

MethodReference (com.jetbrains.php.lang.psi.elements.MethodReference)19 PsiElement (com.intellij.psi.PsiElement)15 NotNull (org.jetbrains.annotations.NotNull)9 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)7 PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)7 Project (com.intellij.openapi.project.Project)5 ParameterList (com.jetbrains.php.lang.psi.elements.ParameterList)4 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)4 PhpElementVisitor (com.jetbrains.php.lang.psi.visitors.PhpElementVisitor)4 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)4 ViewResolve (com.nvlad.yii2support.views.entities.ViewResolve)4 Function (com.jetbrains.php.lang.psi.elements.Function)3 ViewInfo (com.nvlad.yii2support.views.entities.ViewInfo)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Nullable (org.jetbrains.annotations.Nullable)3 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)2 ArrayCreationExpression (com.jetbrains.php.lang.psi.elements.ArrayCreationExpression)2 PhpReference (com.jetbrains.php.lang.psi.elements.PhpReference)2 HashSet (java.util.HashSet)2