Java Code Examples for com.jetbrains.php.lang.psi.elements.PhpClass#getMethods()

The following examples show how to use com.jetbrains.php.lang.psi.elements.PhpClass#getMethods() . 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: 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 2
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 3
Source File: LattePsiImplUtil.java    From intellij-latte with MIT License 6 votes vote down vote up
public static @NotNull LattePhpType getReturnType(@NotNull LattePhpMethod element) {
	LattePhpType type = element.getPhpType();
	Collection<PhpClass> phpClasses = type.getPhpClasses(element.getProject());
	String name = element.getMethodName();
	if (phpClasses.size() == 0) {
		LatteFunctionSettings customFunction = LatteConfiguration.getInstance(element.getProject()).getFunction(name);
		return customFunction == null ? LattePhpType.MIXED : LattePhpType.create(customFunction.getFunctionReturnType());
	}

	List<PhpType> types = new ArrayList<>();
	for (PhpClass phpClass : phpClasses) {
		for (Method phpMethod : phpClass.getMethods()) {
			if (phpMethod.getName().equals(name)) {
				types.add(phpMethod.getType());
			}
		}
	}
	return types.size() > 0 ? LattePhpType.create(types).withDepth(element.getPhpArrayLevel()) : LattePhpType.MIXED;
}
 
Example 4
Source File: YamlCompletionContributor.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {

    PsiElement position = parameters.getPosition();
    if(!Symfony2ProjectComponent.isEnabled(position)) {
        return;
    }

    String service = YamlHelper.getPreviousSequenceItemAsText(position);
    if (service == null) {
        return;
    }

    PhpClass phpClass = ServiceUtil.getServiceClass(position.getProject(), service);
    if(phpClass == null) {
        return;
    }

    for (Method method : phpClass.getMethods()) {
        if(method.getAccess().isPublic() && !(method.getName().startsWith("__"))) {
            completionResultSet.addElement(new PhpLookupElement(method));
        }
    }

}
 
Example 5
Source File: YamlCompletionContributor.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
    PsiElement position = parameters.getPosition();
    if(!Symfony2ProjectComponent.isEnabled(position)) {
        return;
    }

    PsiElement parent = position.getParent();
    if(!(parent instanceof YAMLScalar)) {
        return;
    }

    String textValue = ((YAMLScalar) parent).getTextValue();
    String[] split = textValue.split(":");
    if(split.length < 2) {
        new ServiceCompletionProvider().addCompletions(parameters, processingContext, completionResultSet);
        return;
    }

    PhpClass phpClass = ServiceUtil.getServiceClass(position.getProject(), split[0]);
    if(phpClass == null) {
        return;
    }

    for (Method method : phpClass.getMethods()) {
        if(method.getAccess().isPublic() && !(method.getName().startsWith("__"))) {
            completionResultSet.addElement(
                LookupElementBuilder.create(split[0] + ":" + method.getName())
                .withIcon(method.getIcon()).withTypeText(phpClass.getName(), true)
            );
        }
    }
}
 
Example 6
Source File: YamlGoToDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
private Collection<PsiElement> getArrayMethodGoto(@NotNull PsiElement psiElement) {
    String text = PsiElementUtils.trimQuote(psiElement.getText());
    if(StringUtils.isBlank(text)) {
        return Collections.emptyList();
    }

    String service = YamlHelper.getPreviousSequenceItemAsText(psiElement);
    if (service == null) {
        return Collections.emptyList();
    }

    PhpClass phpClass = ServiceUtil.getServiceClass(psiElement.getProject(), service);
    if(phpClass == null) {
        return Collections.emptyList();
    }

    Collection<PsiElement> results = new ArrayList<>();

    for (Method method : phpClass.getMethods()) {
        if(text.equals(method.getName())) {
            results.add(method);
        }
    }

    return results;
}
 
Example 7
Source File: YamlGoToDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * Factory goto: "factory: 'foo:bar'"
 */
@NotNull
private Collection<PsiElement> getFactoryStringGoto(@NotNull PsiElement psiElement) {
    PsiElement parent = psiElement.getParent();
    if(!(parent instanceof YAMLScalar)) {
        return Collections.emptyList();
    }

    String textValue = ((YAMLScalar) parent).getTextValue();
    String[] split = textValue.split(":");
    if(split.length != 2) {
        return Collections.emptyList();
    }

    PhpClass phpClass = ServiceUtil.getServiceClass(psiElement.getProject(), split[0]);
    if(phpClass == null) {
        return Collections.emptyList();
    }

    Collection<PsiElement> results = new ArrayList<>();

    for (Method method : phpClass.getMethods()) {
        if(split[1].equals(method.getName())) {
            results.add(method);
        }
    }

    return results;
}
 
Example 8
Source File: ControllerIndex.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
private Collection<ControllerAction> getServiceActionMethods(@NotNull Project project) {
    Map<String,Route> routes = RouteHelper.getAllRoutes(project);
    if(routes.size() == 0) {
        return Collections.emptyList();
    }

    // there is now way to find service controllers directly,
    // so we search for predefined service controller and use the public methods
    ContainerCollectionResolver.LazyServiceCollector collector = new ContainerCollectionResolver.LazyServiceCollector(project);

    Collection<ControllerAction> actions = new ArrayList<>();
    for (String serviceName : ServiceRouteContainer.build(routes).getServiceNames()) {
        PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(project, serviceName, collector);
        if(phpClass == null) {
            continue;
        }

        // find public method of the service class which are possible Actions
        for(Method method : phpClass.getMethods()) {
            if(method.getAccess().isPublic() && !method.getName().startsWith("__") && !method.getName().startsWith("set")) {
                actions.add(new ControllerAction(serviceName + ":" + method.getName(), method));
            }
        }
    }

    return actions;
}
 
Example 9
Source File: MethodUsagesInspection.java    From intellij-latte with MIT License 4 votes vote down vote up
private void processMethod(
		LattePhpMethod element,
		@NotNull List<ProblemDescriptor> problems,
		@NotNull final InspectionManager manager,
		final boolean isOnTheFly
) {
	LattePhpType phpType = element.getPhpType();

	boolean isFound = false;
	Collection<PhpClass> phpClasses = phpType.getPhpClasses(element.getProject());
	String methodName = element.getMethodName();
	if (phpClasses != null) {
		for (PhpClass phpClass : phpClasses) {
			for (Method method : phpClass.getMethods()) {
				if (method.getName().equals(methodName)) {
					if (method.getModifier().isPrivate()) {
						addProblem(manager, problems, getElementToLook(element), "Used private method '" + methodName + "'", isOnTheFly);
					} else if (method.getModifier().isProtected()) {
						addProblem(manager, problems, getElementToLook(element), "Used protected method '" + methodName + "'", isOnTheFly);
					} else if (method.isDeprecated()) {
						addDeprecated(manager, problems, getElementToLook(element), "Used method '" + methodName + "' is marked as deprecated", isOnTheFly);
					} else if (method.isInternal()) {
						addDeprecated(manager, problems, getElementToLook(element), "Used method '" + methodName + "' is marked as internal", isOnTheFly);
					}

					String description;
					boolean isStatic = element.isStatic();
					if (isStatic && !method.getModifier().isStatic()) {
						description = "Method '" + methodName + "' is not static but called statically";
						addProblem(manager, problems, getElementToLook(element), description, isOnTheFly);

					} else if (!isStatic && method.getModifier().isStatic()) {
						description = "Method '" + methodName + "' is static but called non statically";
						addProblem(manager, problems, getElementToLook(element), description, isOnTheFly);
					}
					isFound = true;
				}
			}
		}
	}

	if (!isFound) {
		addProblem(manager, problems, getElementToLook(element), "Method '" + methodName + "' not found for type '" + phpType.toString() + "'", isOnTheFly);
	}
}