Java Code Examples for com.jetbrains.php.lang.psi.elements.Method#getName()

The following examples show how to use com.jetbrains.php.lang.psi.elements.Method#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FluidTypeResolver.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
/**
 * "phpNamedElement.variableName", "phpNamedElement.getVariableName" will resolve php type eg method
 *
 * @param phpNamedElement php class method or field
 * @param variableName    variable name shortcut property possible
 * @return matched php types
 */
public static Collection<? extends PhpNamedElement> getFluidPhpNameTargets(PhpNamedElement phpNamedElement, String variableName) {

    Collection<PhpNamedElement> targets = new ArrayList<>();
    if (phpNamedElement instanceof PhpClass) {

        for (Method method : ((PhpClass) phpNamedElement).getMethods()) {
            String methodName = method.getName();
            if (method.getModifier().isPublic() && (methodName.equalsIgnoreCase(variableName) || isPropertyShortcutMethodEqual(methodName, variableName))) {
                targets.add(method);
            }
        }

        for (Field field : ((PhpClass) phpNamedElement).getFields()) {
            String fieldName = field.getName();
            if (field.getModifier().isPublic() && fieldName.equalsIgnoreCase(variableName)) {
                targets.add(field);
            }
        }

    }

    return targets;
}
 
Example 2
Source File: EelProvider.java    From intellij-neos with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    Project project = parameters.getPosition().getProject();
    Collection<String> contexts = FileBasedIndex.getInstance().getAllKeys(DefaultContextFileIndex.KEY, project);

    for (String eelHelper : contexts) {
        List<String> helpers = FileBasedIndex.getInstance().getValues(DefaultContextFileIndex.KEY, eelHelper, GlobalSearchScope.allScope(project));
        if (!helpers.isEmpty()) {
            for (String helper : helpers) {
                Collection<PhpClass> classes = PhpIndex.getInstance(project).getClassesByFQN(helper);
                for (PhpClass phpClass : classes) {
                    for (Method method : phpClass.getMethods()) {
                        if (!method.getAccess().isPublic()) {
                            continue;
                        }
                        if (method.getName().equals("allowsCallOfMethod")) {
                            continue;
                        }
                        String completionText = eelHelper + "." + method.getName() + "()";
                        result.addElement(LookupElementBuilder.create(completionText).withIcon(PhpIcons.METHOD_ICON));
                    }
                }
            }
        }
    }
}
 
Example 3
Source File: ControllerCollector.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
public static void visitControllerActions(@NotNull final Project project, @NotNull ControllerActionVisitor visitor) {

        Collection<PhpClass> allSubclasses = new HashSet<PhpClass>() {{
            addAll(PhpIndex.getInstance(project).getAllSubclasses("\\Illuminate\\Routing\\Controller"));
            addAll(PhpIndex.getInstance(project).getAllSubclasses("\\App\\Http\\Controllers\\Controller"));
        }};

        for(PhpClass phpClass: allSubclasses) {
            if(!phpClass.isAbstract()) {
                for(Method method: phpClass.getMethods()) {
                    String className = phpClass.getPresentableFQN();
                    String methodName = method.getName();
                    if(!method.isStatic() && method.getAccess().isPublic() && !methodName.startsWith("__")) {
                        PhpClass phpTrait = method.getContainingClass();
                        if(phpTrait == null || !commonControllerTraits.contains(phpTrait.getName())) {
                            if(StringUtils.isNotBlank(className)) {
                                visitor.visit(phpClass, method, className + "@" + methodName);
                            }
                        }
                    }
                }
            }
        }
    }
 
Example 4
Source File: FormUnderscoreMethodReference.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public Object[] getVariants() {
    Collection<LookupElement> lookupElements = new ArrayList<>();

    // provide setter fallback for non model class or unknown methods
    for(Method method: this.phpClass.getMethods()) {
        String name = method.getName();
        if(name.length() > 3 && name.startsWith("set")) {
            lookupElements.add(new PhpFormPropertyMethodLookupElement(method, StringUtils.lcfirst(name.substring(3))));
        }
    }

    // Symfony\Component\PropertyAccess\PropertyAccessor::getWriteAccessInfo
    // property: public $foobar
    lookupElements.addAll(this.phpClass.getFields().stream()
        .filter(field -> !field.isConstant() && field.getModifier().isPublic())
        .map(field -> new PhpFormPropertyMethodLookupElement(field, field.getName()))
        .collect(Collectors.toList())
    );

    return lookupElements.toArray();
}
 
Example 5
Source File: TwigControllerUsageControllerRelatedGotoCollector.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
public void collectGotoRelatedItems(ControllerActionGotoRelatedCollectorParameter parameter) {
    Method method = parameter.getMethod();
    PhpClass containingClass = method.getContainingClass();

    if (containingClass == null) {
        return;
    }

    String controllerAction = StringUtils.stripStart(containingClass.getPresentableFQN(), "\\") + "::" + method.getName();

    Collection<VirtualFile> targets = new HashSet<>();
    FileBasedIndex.getInstance().getFilesWithKey(TwigControllerStubIndex.KEY, new HashSet<>(Collections.singletonList(controllerAction)), virtualFile -> {
        targets.add(virtualFile);
        return true;
    }, GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.allScope(parameter.getProject()), TwigFileType.INSTANCE));

    for (PsiFile psiFile: PsiElementUtils.convertVirtualFilesToPsiFiles(parameter.getProject(), targets)) {
        TwigUtil.visitControllerFunctions(psiFile, pair -> {
            if (pair.getFirst().equalsIgnoreCase(controllerAction)) {
                parameter.add(new RelatedPopupGotoLineMarker.PopupGotoRelatedItem(pair.getSecond()).withIcon(TwigIcons.TwigFileIcon, Symfony2Icons.TWIG_LINE_MARKER));
            }
        });
    }
}
 
Example 6
Source File: TwigTypeResolveUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 *
 * "phpNamedElement.variableName", "phpNamedElement.getVariableName" will resolve php type eg method
 *
 * @param phpNamedElement php class method or field
 * @param variableName variable name shortcut property possible
 * @return matched php types
 */
public static Collection<? extends PhpNamedElement> getTwigPhpNameTargets(PhpNamedElement phpNamedElement, String variableName) {

    Collection<PhpNamedElement> targets = new ArrayList<>();
    if(phpNamedElement instanceof PhpClass) {

        for(Method method: ((PhpClass) phpNamedElement).getMethods()) {
            String methodName = method.getName();
            if(method.getModifier().isPublic() && (methodName.equalsIgnoreCase(variableName) || isPropertyShortcutMethodEqual(methodName, variableName))) {
                targets.add(method);
            }
        }

        for(Field field: ((PhpClass) phpNamedElement).getFields()) {
            String fieldName = field.getName();
            if(field.getModifier().isPublic() && fieldName.equalsIgnoreCase(variableName)) {
                targets.add(field);
            }
        }

    }

    return targets;
}
 
Example 7
Source File: XmlReferenceContributor.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext context) {
    if(!Symfony2ProjectComponent.isEnabled(psiElement) || !(psiElement instanceof XmlAttributeValue)) {
        return new PsiReference[0];
    }

    String method = ((XmlAttributeValue) psiElement).getValue();
    if(StringUtils.isBlank(method)) {
        return new PsiReference[0];
    }

    PhpClass phpClass = XmlHelper.getPhpClassForServiceFactory((XmlAttributeValue) psiElement);
    if(phpClass == null) {
        return new PsiReference[0];
    }

    Method targetMethod = phpClass.findMethodByName(method);
    if(targetMethod == null) {
        return new PsiReference[0];
    }

    return new PsiReference[] {
        new ClassMethodStringPsiReference(psiElement, phpClass.getFQN(), targetMethod.getName()),
    };
}
 
Example 8
Source File: XmlReferenceContributor.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {
    if(!Symfony2ProjectComponent.isEnabled(psiElement) || !(psiElement instanceof XmlAttributeValue)) {
        return new PsiReference[0];
    }

    String method = ((XmlAttributeValue) psiElement).getValue();
    if(StringUtils.isBlank(method)) {
        return new PsiReference[0];
    }

    PhpClass phpClass = XmlHelper.getPhpClassForClassFactory((XmlAttributeValue) psiElement);
    if(phpClass == null) {
        return new PsiReference[0];
    }

    Method classMethod = phpClass.findMethodByName(method);
    if(classMethod == null) {
        return new PsiReference[0];
    }

    return new PsiReference[]{
        new ClassMethodStringPsiReference(psiElement, phpClass.getFQN(), classMethod.getName()),
    };
}
 
Example 9
Source File: PhpEventDispatcherGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public Collection<LookupElement> getLookupElements() {

    Collection<LookupElement> elements = new ArrayList<>();

    for(Method method: phpClass.getMethods()) {
        if(method.getAccess().isPublic()) {
            String name = method.getName();
            if(!"getSubscribedEvents".equals(name) && !name.startsWith("__") && !name.startsWith("set")) {
                elements.add(LookupElementBuilder.create(name).withIcon(method.getIcon()));
            }
        }
    }

    return elements;
}
 
Example 10
Source File: PhpControllerVisitor.java    From Thinkphp5-Plugin with MIT License 5 votes vote down vote up
@Override
public void visitElement(PsiElement element) {
    if (element instanceof PhpClass) {
        PsiElement[] childrens = element.getChildren();
        for (PsiElement item : childrens) {
            if (item instanceof Method) {
                Method method = (Method) item;
                String route = this.prefix + "/" + method.getName();
                this.visitor.visit(route, method, false);
            }
        }
    } else {
        super.visitElement(element);
    }
}
 
Example 11
Source File: FluidTypeResolver.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
@NotNull
public static String getPropertyShortcutMethodName(@NotNull Method method) {
    String methodName = method.getName();

    for (String shortcut : PROPERTY_SHORTCUTS) {
        // strip possible property shortcut and make it lcfirst
        if (method.getName().startsWith(shortcut) && method.getName().length() > shortcut.length()) {
            methodName = methodName.substring(shortcut.length());
            return Character.toLowerCase(methodName.charAt(0)) + methodName.substring(1);
        }
    }

    return methodName;
}
 
Example 12
Source File: TwigTypeResolveUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * Twig attribute shortcuts
 *
 * getFoo => foo
 * hasFoo => foo
 * isFoo => foo
 */
@NotNull
public static String getPropertyShortcutMethodName(@NotNull Method method) {
    String methodName = method.getName();

    for(String shortcut: PROPERTY_SHORTCUTS) {
        // strip possible property shortcut and make it lcfirst
        if(method.getName().startsWith(shortcut) && method.getName().length() > shortcut.length()) {
            methodName = methodName.substring(shortcut.length());
            return Character.toLowerCase(methodName.charAt(0)) + methodName.substring(1);
        }
    }

    return methodName;
}
 
Example 13
Source File: EventMethodCallInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void visitElement(PsiElement element) {
    super.visitElement(element);

    if(!(element instanceof StringLiteralExpression)) {
        return;
    }

    PsiElement arrayValue = element.getParent();
    if(arrayValue != null && arrayValue.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE) {
        PhpReturn phpReturn = PsiTreeUtil.getParentOfType(arrayValue, PhpReturn.class);
        if(phpReturn != null) {
            Method method = PsiTreeUtil.getParentOfType(arrayValue, Method.class);
            if(method != null) {
                String name = method.getName();
                if("getSubscribedEvents".equals(name)) {
                    PhpClass containingClass = method.getContainingClass();
                    if(containingClass != null && PhpElementsUtil.isInstanceOf(containingClass, "\\Symfony\\Component\\EventDispatcher\\EventSubscriberInterface")) {
                        String contents = ((StringLiteralExpression) element).getContents();
                        if(StringUtils.isNotBlank(contents) && containingClass.findMethodByName(contents) == null) {
                            registerMethodProblem(element, holder, containingClass);
                        }
                    }
                }
            }
        }
    }
}
 
Example 14
Source File: ResolveEngine.java    From intellij-neos with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Find template for controller action
 *
 * @param method Controller action method
 * @return VirtualFile
 */
public static VirtualFile findTemplate(Method method) {
    PsiFile file = method.getContainingFile();
    if (method.getContainingClass() != null) {
        String actionName = method.getName();
        if (!actionName.endsWith("Action")) {
            return null;
        }

        actionName = actionName.replace("Action", "");
        if (actionName.length() < 2) {
            return null;
        }

        actionName = actionName.substring(0, 1).toUpperCase() + actionName.substring(1);

        String controllerName = method.getContainingClass().getName();
        controllerName = controllerName.replace("Controller", "");

        JsonFile composerFile = ComposerUtil.getComposerManifest(file.getContainingDirectory());
        if (composerFile != null) {
            String namespace = method.getNamespaceName();
            namespace = namespace.substring(1);

            Map<String, String> namespaceMappings = ComposerUtil.getNamespaceMappings(composerFile);
            for(String key : namespaceMappings.keySet()) {
                if (namespace.startsWith(key)) {
                    namespace = namespace.replace(key, "")
                        .replace("\\", "/");

                    if (namespace.startsWith("/") && namespace.length() > 1) {
                        namespace = namespace.substring(1);
                    }

                    namespace = namespace.replace("Controller/", "");
                    break;
                }
            }

            String resourceFile = "Resources/Private/Templates/" + namespace + controllerName + "/" + actionName + ".html";
            return composerFile.getContainingDirectory().getVirtualFile().findFileByRelativePath(resourceFile);
        }
    }

    return null;
}