Java Code Examples for com.jetbrains.php.PhpIndex#getInstance()
The following examples show how to use
com.jetbrains.php.PhpIndex#getInstance() .
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: RouteHelper.java From idea-php-typo3-plugin with MIT License | 6 votes |
@NotNull private static PsiElement[] getTargetMethods(@NotNull Project project, @NotNull String routeName) { List<PsiElement> result = new ArrayList<>(); List<RouteStub> values = FileBasedIndex.getInstance().getValues(RouteIndex.KEY, routeName, GlobalSearchScope.allScope(project)); PhpIndex phpIndex = PhpIndex.getInstance(project); for (RouteStub routeStub : values) { String fqn = routeStub.getController(); Collection<PhpClass> classesByFQN = phpIndex.getClassesByFQN(fqn); classesByFQN.forEach(c -> { if (c.findMethodByName(routeStub.getMethod()) != null) { result.add(c.findMethodByName(routeStub.getMethod())); } }); } return result.toArray(new PsiElement[0]); }
Example 2
Source File: LattePhpNamespaceCompletionProvider.java From intellij-latte with MIT License | 6 votes |
@Override protected void addCompletions(@NotNull CompletionParameters params, ProcessingContext context, @NotNull CompletionResultSet result) { PsiElement curr = params.getPosition().getOriginalElement(); if (PsiTreeUtil.getParentOfType(curr, LattePhpContent.class) == null) { return; } PhpIndex phpIndex = PhpIndex.getInstance(curr.getProject()); String prefix = result.getPrefixMatcher().getPrefix(); String namespace = ""; if (prefix.contains("\\")) { int index = prefix.lastIndexOf("\\"); namespace = prefix.substring(0, index) + "\\"; prefix = prefix.substring(index + 1); } PhpCompletionUtil.addSubNamespaces(namespace, result.withPrefixMatcher(prefix), phpIndex, PhpNamespaceInsertHandler.getInstance()); }
Example 3
Source File: EventDispatcherSubscriberUtil.java From idea-php-symfony2-plugin with MIT License | 6 votes |
@NotNull private static Collection<EventDispatcherSubscribedEvent> getSubscribedEventsProxy(@NotNull Project project) { Collection<EventDispatcherSubscribedEvent> events = new ArrayList<>(); // http://symfony.com/doc/current/components/event_dispatcher/introduction.html PhpIndex phpIndex = PhpIndex.getInstance(project); Collection<PhpClass> phpClasses = phpIndex.getAllSubclasses("\\Symfony\\Component\\EventDispatcher\\EventSubscriberInterface"); for(PhpClass phpClass: phpClasses) { if(PhpElementsUtil.isTestClass(phpClass)) { continue; } Method method = phpClass.findMethodByName("getSubscribedEvents"); if(method != null) { PhpReturn phpReturn = PsiTreeUtil.findChildOfType(method, PhpReturn.class); if(phpReturn != null) { attachSubscriberEventNames(events, phpClass, phpReturn); } } } return events; }
Example 4
Source File: PhpIndexAbstractProviderAbstract.java From idea-php-toolbox with MIT License | 6 votes |
@NotNull public Collection<LookupElement> getLookupElements(@NotNull PhpToolboxCompletionContributorParameter parameter) { PhpIndex instance = PhpIndex.getInstance(parameter.getProject()); Collection<LookupElement> lookupElements = new ArrayList<>(); for (String className : getClasses(parameter)) { // strip double backslash className = className.replaceAll("\\\\+", "\\\\"); for (PhpClass phpClass : getPhpClassesForLookup(instance, className)) { lookupElements.add( LookupElementBuilder.create(phpClass.getPresentableFQN()).withIcon(phpClass.getIcon()) ); } } return lookupElements; }
Example 5
Source File: CodeInsightFixtureTestCase.java From silex-idea-plugin with MIT License | 6 votes |
protected void assertPhpReferenceSignatureEquals(LanguageFileType languageFileType, @NotNull Class aClass, String configureByText, String typeSignature) { myFixture.configureByText(languageFileType, configureByText); PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset()); psiElement = PsiTreeUtil.getParentOfType(psiElement, aClass); if (!(psiElement instanceof PhpReference)) { fail("Element is not PhpReference."); } PhpIndex phpIndex = PhpIndex.getInstance(myFixture.getProject()); Collection<? extends PhpNamedElement> collection = phpIndex.getBySignature(((PhpReference)psiElement).getSignature(), null, 0); assertNotEmpty(collection); for (String type : collection.iterator().next().getType().getTypes()) { if (type.equals(typeSignature)) { return; } } fail("Can't find type: "+typeSignature+", found:"+collection.iterator().next().getType().toString()); }
Example 6
Source File: PhpClassReference.java From idea-php-symfony2-plugin with MIT License | 6 votes |
@NotNull @Override public ResolveResult[] multiResolve(boolean incompleteCode) { List<ResolveResult> results = new ArrayList<>(); PhpIndex phpIndex = PhpIndex.getInstance(getElement().getProject()); if(this.useClasses) { this.attachPhpClassResolveResults(phpIndex.getClassesByFQN(classFQN), results); } if(this.useInterfaces) { this.attachPhpClassResolveResults(phpIndex.getInterfacesByFQN(classFQN.startsWith("\\") ? classFQN : "\\" + classFQN), results); } return results.toArray(new ResolveResult[results.size()]); }
Example 7
Source File: PhpIndexAbstractProviderAbstract.java From idea-php-toolbox with MIT License | 6 votes |
@NotNull public Collection<LookupElement> getLookupElements(@NotNull PhpToolboxCompletionContributorParameter parameter) { PhpIndex instance = PhpIndex.getInstance(parameter.getProject()); Collection<LookupElement> lookupElements = new ArrayList<>(); for (String className : getClasses(parameter)) { // strip double backslash className = className.replaceAll("\\\\+", "\\\\"); for (PhpClass phpClass : getPhpClassesForLookup(instance, className)) { lookupElements.add( LookupElementBuilder.create(phpClass.getPresentableFQN()).withIcon(phpClass.getIcon()) ); } } return lookupElements; }
Example 8
Source File: GeneralUtilityServiceTypeProvider.java From idea-php-typo3-plugin with MIT License | 6 votes |
@Override public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) { Collection<PhpNamedElement> phpNamedElementCollections = new ArrayList<>(); PhpIndex phpIndex = PhpIndex.getInstance(project); CoreServiceParser serviceParser = new CoreServiceParser(); serviceParser.collect(project); List<TYPO3ServiceDefinition> resolvedServices = serviceParser.resolve(project, expression); if (resolvedServices == null || resolvedServices.isEmpty()) { return phpNamedElementCollections; } resolvedServices.forEach(serviceDefinition -> { Collection<PhpClass> classesByFQN = phpIndex.getClassesByFQN(serviceDefinition.getClassName()); phpNamedElementCollections.addAll(classesByFQN); }); return phpNamedElementCollections; }
Example 9
Source File: GlobalStringClassGoto.java From idea-php-toolbox with MIT License | 5 votes |
@NotNull public static PsiElement[] getPsiElements(@NotNull Project project, @NotNull String contents) { Collection<PsiElement> psiElements = new HashSet<>(); contents = contents.replaceAll("\\\\+", "\\\\"); // DateTime // date Matcher matcher = Pattern.compile("^([\\w\\\\-]+)$").matcher(contents); if (matcher.find()) { PhpIndex phpIndex = PhpIndex.getInstance(project); ContainerUtil.addAllNotNull(psiElements, phpIndex.getAnyByFQN(contents)); ContainerUtil.addAllNotNull(psiElements, phpIndex.getFunctionsByName(contents)); return psiElements.toArray(new PsiElement[psiElements.size()]); } // DateTime:format // DateTime::format matcher = Pattern.compile("^([\\w\\\\-]+):+([\\w_\\-]+)$").matcher(contents); if (matcher.find()) { for (PhpClass phpClass : PhpIndex.getInstance(project).getAnyByFQN(matcher.group(1))) { ContainerUtil.addIfNotNull(psiElements, phpClass.findMethodByName(matcher.group(2))); } return psiElements.toArray(new PsiElement[psiElements.size()]); } return psiElements.toArray(new PsiElement[psiElements.size()]); }
Example 10
Source File: PimplePhpTypeProvider.java From silex-idea-plugin with MIT License | 5 votes |
@Override public Collection<? extends PhpNamedElement> getBySignature(String expression, Project project) { PhpIndex phpIndex = PhpIndex.getInstance(project); Signature signature = new Signature(expression); // try to resolve service type if(ProjectComponent.isEnabled(project) && signature.hasParameter()) { ArrayList<String> parameters = new ArrayList<String>(); if (Utils.findPimpleContainer(phpIndex, expression, parameters)) { return phpIndex.getClassesByFQN(getClassNameFromParameters(phpIndex, project, parameters)); } } // if it's not a service try to get original type Collection<? extends PhpNamedElement> collection = phpIndex.getBySignature(signature.base, null, 0); if (collection.size() == 0) { return Collections.emptySet(); } // original type can be array (#C\ClassType[]) resolve to proper value type PhpNamedElement element = collection.iterator().next(); for (String type : element.getType().getTypes()) { if (type.endsWith("[]")) { Collection<? extends PhpNamedElement> result = phpIndex.getClassesByFQN(type.substring(0, type.length() - 2)); if (result.size() != 0) { return result; } } } return collection; }
Example 11
Source File: ClassCompletionProviderAbstract.java From idea-php-annotation-plugin with MIT License | 5 votes |
@Override public void getPropertyValueCompletions(AnnotationPropertyParameter parameter, AnnotationCompletionProviderParameter completionParameter) { if(!supports(parameter)) { return; } String className = ""; PsiElement element = parameter.getElement(); if(element instanceof StringLiteralExpression) { className = ((StringLiteralExpression) element).getContents(); } PhpIndex phpIndex = PhpIndex.getInstance(parameter.getProject()); PhpCompletionUtil.addClasses(className, completionParameter.getResult(), phpIndex, null); }
Example 12
Source File: PhpElementsUtil.java From idea-php-typo3-plugin with MIT License | 5 votes |
public static List<PhpClass> allExtendedClasses(PhpClass phpClass) { List<PhpClass> classReferences = new ArrayList<>(); PhpIndex index = PhpIndex.getInstance(phpClass.getProject()); List<ClassReference> referenceElements = phpClass.getExtendsList().getReferenceElements(); for (ClassReference reference : referenceElements) { Collection<PhpClass> classesByFQN = index.getClassesByFQN(reference.getFQN()); for (PhpClass phpClass1 : classesByFQN) { classReferences.add(phpClass); classReferences.addAll(allExtendedClasses(phpClass1)); } } return classReferences; }
Example 13
Source File: TwigUtil.java From idea-php-symfony2-plugin with MIT License | 5 votes |
/** * Visit Twig TokenParser * eg. {% my_token %} */ public static void visitTokenParsers(@NotNull Project project, @NotNull Consumer<Triple<String, PsiElement, Method>> consumer) { Set<PhpClass> allSubclasses = new HashSet<>(); PhpIndex phpIndex = PhpIndex.getInstance(project); allSubclasses.addAll(phpIndex.getAllSubclasses("\\Twig_TokenParserInterface")); allSubclasses.addAll(phpIndex.getAllSubclasses("\\Twig\\TokenParser\\TokenParserInterface")); for (PhpClass allSubclass : allSubclasses) { // we dont want to see test extension like "ยง" if(allSubclass.getName().endsWith("Test") || allSubclass.getContainingFile().getVirtualFile().getNameWithoutExtension().endsWith("Test")) { continue; } Method getTag = allSubclass.findMethodByName("getTag"); if(getTag == null) { continue; } // get string return value PhpReturn childrenOfType = PsiTreeUtil.findChildOfType(getTag, PhpReturn.class); if(childrenOfType != null) { PhpPsiElement returnValue = childrenOfType.getFirstPsiChild(); if(returnValue instanceof StringLiteralExpression) { String contents = ((StringLiteralExpression) returnValue).getContents(); if(StringUtils.isNotBlank(contents)) { consumer.consume(new Triple<>(contents, returnValue, getTag)); } } } } }
Example 14
Source File: TemplateAnnotationTypeProvider.java From idea-php-generics-plugin with MIT License | 5 votes |
@Override public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> set, int i, Project project) { PhpIndex phpIndex = PhpIndex.getInstance(project); String resolvedParameter = PhpTypeProviderUtil.getResolvedParameter(phpIndex, expression); if(resolvedParameter == null) { return null; } return phpIndex.getAnyByFQN(resolvedParameter); }
Example 15
Source File: PhpViewHelpersProvider.java From idea-php-typo3-plugin with MIT License | 4 votes |
@NotNull private synchronized Map<String, ViewHelper> doProvide(@NotNull Project project, @NotNull String namespace) { HashMap<String, ViewHelper> collectedViewHelpers = new HashMap<>(); String fqnPart = namespace.replace("/", "\\"); PhpIndex phpIndex = PhpIndex.getInstance(project); Collection<PhpClass> viewHelperClasses = phpIndex.getAllSubclasses("TYPO3Fluid\\Fluid\\Core\\ViewHelper\\ViewHelperInterface"); for (PhpClass viewHelperPhpClass : viewHelperClasses) { String fqn = viewHelperPhpClass.getPresentableFQN(); if (fqn.startsWith(fqnPart) && !viewHelperPhpClass.isAbstract()) { CachedValue cachedViewHelper = viewHelperPhpClass.getUserData(VIEWHELPER_DEFINITION_KEY); if (cachedViewHelper != null) { ViewHelper value = (ViewHelper) cachedViewHelper.getValue(); collectedViewHelpers.put(value.name, value); continue; } try { CachedValue<ViewHelper> cachedValue = CachedValuesManager.getManager(project).createCachedValue(() -> { ViewHelperVisitor visitor = new ViewHelperVisitor(); viewHelperPhpClass.accept(visitor); ViewHelper viewHelper = new ViewHelper(convertFqnToViewHelperName(fqn.substring(fqnPart.length() + 1))); viewHelper.setFqn(fqn); viewHelper.arguments.putAll(visitor.arguments); return CachedValueProvider.Result.createSingleDependency(viewHelper, viewHelperPhpClass); }, false); viewHelperPhpClass.putUserData(VIEWHELPER_DEFINITION_KEY, cachedValue); collectedViewHelpers.put(cachedValue.getValue().name, cachedValue.getValue()); } catch (IllegalArgumentException e) { e.getMessage(); } } } return collectedViewHelpers; }
Example 16
Source File: PhpToolboxTypeProvider.java From idea-php-toolbox with MIT License | 4 votes |
@Override public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) { // get back our original call // since phpstorm 7.1.2 we need to validate this int endIndex = expression.indexOf(String.valueOf(TRIM_KEY)); if(endIndex == -1) { return Collections.emptySet(); } String originalSignature = expression.substring(0, endIndex); String parametersSignature = expression.substring(endIndex + 1); // search for called method PhpIndex phpIndex = PhpIndex.getInstance(project); Collection<? extends PhpNamedElement> phpNamedElements = phpIndex.getBySignature(originalSignature, null, 0); if(phpNamedElements.size() == 0) { return Collections.emptySet(); } // get first matched item PhpNamedElement phpNamedElement = phpNamedElements.iterator().next(); if(!(phpNamedElement instanceof Function)) { return phpNamedElements; } Map<Integer, String> parameters = getParameters(parametersSignature, phpIndex); if (parameters.isEmpty()) { return phpNamedElements; } Map<String, Collection<JsonRawLookupElement>> providerMap = ExtensionProviderUtil.getProviders( project, ApplicationManager.getApplication().getComponent(PhpToolboxApplicationService.class) ); Set<Pair<String, Integer>> providers = getProviderNames(project, (Function) phpNamedElement); Collection<PhpNamedElement> elements = new HashSet<>(); elements.addAll(phpNamedElements); for (Pair<String, Integer> providerPair : providers) { String providerName = providerPair.first; Integer index = providerPair.second; String parameter = parameters.get(index); if (parameter == null) { continue; } PhpToolboxProviderInterface provider = ExtensionProviderUtil.getProvider(project, providerName); if(!(provider instanceof PhpToolboxTypeProviderInterface)) { continue; } PhpToolboxTypeProviderArguments args = new PhpToolboxTypeProviderArguments( project, parameter, providerMap.containsKey(providerName) ? providerMap.get(providerName) : Collections.emptyList() ); Collection<PhpNamedElement> items = ((PhpToolboxTypeProviderInterface) provider).resolveParameter(args); if(items != null && items.size() > 0) { elements.addAll(items); } } return elements; }
Example 17
Source File: ObjectRepositoryResultTypeProvider.java From idea-php-symfony2-plugin with MIT License | 4 votes |
@Nullable @Override public PhpType complete(String s, Project project) { int endIndex = s.lastIndexOf(TRIM_KEY); if(endIndex == -1) { return null; } String originalSignature = s.substring(0, endIndex); String parameter = s.substring(endIndex + 1); parameter = PhpTypeProviderUtil.getResolvedParameter(PhpIndex.getInstance(project), parameter); if(parameter == null) { return null; } PhpClass phpClass = EntityHelper.resolveShortcutName(project, parameter); if(phpClass == null) { return null; } PhpIndex phpIndex = PhpIndex.getInstance(project); Collection<? extends PhpNamedElement> typeSignature = getTypeSignatureMagic(phpIndex, originalSignature); // ->getRepository(SecondaryMarket::class)->findAll() => "findAll", but only if its a instance of this method; // so non Doctrine method are already filtered Set<String> resolveMethods = getObjectRepositoryCall(typeSignature).stream() .map(PhpNamedElement::getName) .collect(Collectors.toSet()); if (resolveMethods.isEmpty()) { return null; } PhpType phpType = new PhpType(); resolveMethods.stream() .map(name -> name.equals("findAll") || name.startsWith("findBy") ? phpClass.getFQN() + "[]" : phpClass.getFQN()) .collect(Collectors.toSet()) .forEach(phpType::add); return phpType; }
Example 18
Source File: ObjectManagerFindTypeProvider.java From idea-php-symfony2-plugin with MIT License | 4 votes |
@Override public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) { // get back our original call int endIndex = expression.lastIndexOf(TRIM_KEY); if(endIndex == -1) { return Collections.emptySet(); } String originalSignature = expression.substring(0, endIndex); String parameter = expression.substring(endIndex + 1); // search for called method PhpIndex phpIndex = PhpIndex.getInstance(project); Collection<? extends PhpNamedElement> phpNamedElementCollections = PhpTypeProviderUtil.getTypeSignature(phpIndex, originalSignature); if(phpNamedElementCollections.size() == 0) { return Collections.emptySet(); } PhpNamedElement phpNamedElement = phpNamedElementCollections.iterator().next(); if(!(phpNamedElement instanceof Method)) { return Collections.emptySet(); } if (!( PhpElementsUtil.isMethodInstanceOf((Method) phpNamedElement, "\\Doctrine\\Common\\Persistence\\ObjectManager", "find") || PhpElementsUtil.isMethodInstanceOf((Method) phpNamedElement, "\\Doctrine\\Persistence\\ObjectManager", "find") )) { return Collections.emptySet(); } parameter = PhpTypeProviderUtil.getResolvedParameter(phpIndex, parameter); if(parameter == null) { return Collections.emptySet(); } PhpClass phpClass = EntityHelper.resolveShortcutName(project, parameter); if(phpClass == null) { return Collections.emptySet(); } return PhpTypeProviderUtil.mergeSignatureResults(phpNamedElementCollections, phpClass); }
Example 19
Source File: ShopwareApiResourcesTypeProvider.java From idea-php-shopware-plugin with MIT License | 4 votes |
@Override public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) { // get back our original call // since phpstorm 7.1.2 we need to validate this int endIndex = expression.lastIndexOf(TRIM_KEY); if(endIndex == -1) { return Collections.emptySet(); } String originalSignature = expression.substring(0, endIndex); String parameter = expression.substring(endIndex + 1); // search for called method PhpIndex phpIndex = PhpIndex.getInstance(project); Collection<? extends PhpNamedElement> phpNamedElementCollections = phpIndex.getBySignature(originalSignature, null, 0); if(phpNamedElementCollections.size() == 0) { return Collections.emptySet(); } // get first matched item PhpNamedElement phpNamedElement = phpNamedElementCollections.iterator().next(); if(!(phpNamedElement instanceof Method)) { return Collections.emptySet(); } parameter = PhpTypeProviderUtil.getResolvedParameter(phpIndex, parameter); if(parameter == null) { return Collections.emptySet(); } // finally search the classes if(PhpElementsUtil.isMethodInstanceOf((Method) phpNamedElement, "\\Shopware\\Components\\Api\\Manager", "getResource")) { PhpClass phpClass = ShopwareUtil.getResourceClass(project, parameter); if(phpClass != null) { return Collections.singletonList(phpClass); } } return phpNamedElementCollections; }
Example 20
Source File: EventDispatcherTypeProvider.java From idea-php-symfony2-plugin with MIT License | 4 votes |
@Override public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) { // get back our original call // since phpstorm 7.1.2 we need to validate this int endIndex = expression.lastIndexOf(TRIM_KEY); if(endIndex == -1) { return Collections.emptySet(); } String originalSignature = expression.substring(0, endIndex); String parameter = expression.substring(endIndex + 1); if(!parameter.startsWith("\\")) { return Collections.emptySet(); } PhpClass phpClass = PhpElementsUtil.getClass(project, parameter); if(phpClass == null) { return Collections.emptySet(); } // search for called method PhpIndex phpIndex = PhpIndex.getInstance(project); Collection<? extends PhpNamedElement> phpNamedElementCollections = PhpTypeProviderUtil.getTypeSignature(phpIndex, originalSignature); if(phpNamedElementCollections.size() == 0) { return Collections.emptySet(); } // get first matched item PhpNamedElement phpNamedElement = phpNamedElementCollections.iterator().next(); if(!(phpNamedElement instanceof Method)) { return phpNamedElementCollections; } PhpClass containingClass = ((Method) phpNamedElement).getContainingClass(); if(containingClass == null) { return phpNamedElementCollections; } parameter = PhpTypeProviderUtil.getResolvedParameter(phpIndex, parameter); if(parameter == null) { return phpNamedElementCollections; } // finally search the classes if(!PhpElementsUtil.isInstanceOf(containingClass, "\\Symfony\\Component\\EventDispatcher\\EventDispatcherInterface")) { return phpNamedElementCollections; } return Collections.singletonList(phpClass); }