com.jetbrains.php.lang.psi.elements.PhpNamedElement Java Examples

The following examples show how to use com.jetbrains.php.lang.psi.elements.PhpNamedElement. 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: DocTagNameAnnotationReferenceContributor.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
/**
 * Attach element identify name to class of "use" usage
 *
 * @param psiElement PhpClass used in "use" statement
 */
@Override
public boolean isReferenceTo(@NotNull PsiElement psiElement) {
    if(!(psiElement instanceof PhpNamedElement)) {
        return false;
    }

    String text = getElement().getText();
    if(StringUtils.isBlank(text)) {
        return false;
    }

    String classByContext = AnnotationUtil.getUseImportMap(myElement).get(text);
    if(classByContext != null) {
        return StringUtils.stripStart(((PhpNamedElement) psiElement).getFQN(), "\\")
            .equalsIgnoreCase(StringUtils.stripStart(fqn, "\\"));
    }

    return false;
}
 
Example #2
Source File: CodeInsightFixtureTestCase.java    From silex-idea-plugin with MIT License 6 votes vote down vote up
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 #3
Source File: PhpDiTypeProvider.java    From phpstorm-phpdi with MIT License 6 votes vote down vote up
@Override
public Collection<? extends PhpNamedElement> getBySignature(String s, Project project) {
    int endIndex = s.lastIndexOf("%");
    if (endIndex == -1) {
        return Collections.emptySet();
    }

    // Get FQN from parameter string.
    // Example (PhpStorm 8): #K#C\Foo\Bar::get()%#K#C\Bar\Baz. -> \Bar\Baz.
    // Example (PhpStorm 9): #K#C\Foo\Bar::get()%#K#C\Bar\Baz.class -> \Bar\Baz.class
    String parameter = s.substring(endIndex + 5, s.length());

    if (parameter.contains(".class")) { // for PhpStorm 9
        parameter = parameter.replace(".class", "");
    }

    if (parameter.contains(".")) {
        parameter = parameter.replace(".", "");
    }

    return PhpIndex.getInstance(project).getAnyByFQN(parameter);
}
 
Example #4
Source File: TwigVariableDeprecatedInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private void visit(@NotNull PsiElement element) {
    Collection<String> beforeLeaf = TwigTypeResolveUtil.formatPsiTypeName(element);
    if(beforeLeaf.size() == 0) {
        return;
    }

    Collection<TwigTypeContainer> types = TwigTypeResolveUtil.resolveTwigMethodName(element, beforeLeaf);
    if(types.size() == 0) {
        return;
    }

    for(TwigTypeContainer twigTypeContainer: types) {
        PhpNamedElement phpClass = twigTypeContainer.getPhpNamedElement();
        if(!(phpClass instanceof PhpClass)) {
            continue;
        }

        String text = element.getText();

        for (PhpNamedElement namedElement : TwigTypeResolveUtil.getTwigPhpNameTargets(phpClass, text)) {
            if(namedElement instanceof Method && namedElement.isDeprecated()) {
                this.holder.registerProblem(element, String.format("Method '%s::%s' is deprecated", phpClass.getName(), namedElement.getName()), ProblemHighlightType.LIKE_DEPRECATED);
            }
        }
    }
}
 
Example #5
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 #6
Source File: FluidTypeResolver.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
private static Collection<FluidTypeContainer> resolveFluidMethodName(Collection<FluidTypeContainer> previousElement, String typeName, Collection<List<FluidTypeContainer>> twigTypeContainer) {
    ArrayList<FluidTypeContainer> phpNamedElements = new ArrayList<>();
    for (FluidTypeContainer phpNamedElement : previousElement) {
        if (phpNamedElement.getPhpNamedElement() != null) {
            for (PhpNamedElement target : getFluidPhpNameTargets(phpNamedElement.getPhpNamedElement(), typeName)) {
                PhpType phpType = target.getType();
                for (String typeString : phpType.getTypes()) {
                    PhpNamedElement phpNamedElement1 = getClassInterface(phpNamedElement.getPhpNamedElement().getProject(), typeString);
                    if (phpNamedElement1 != null) {
                        phpNamedElements.add(new FluidTypeContainer(phpNamedElement1));
                    }
                }
            }
        }
    }

    return phpNamedElements;
}
 
Example #7
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 #8
Source File: AbstractServiceLocatorTypeProvider.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) {
    int endIndex = expression.lastIndexOf(TRIM_KEY);
    if (endIndex == -1) {
        return Collections.emptySet();
    }

    // Get FQN from parameter string.
    // Example (PhpStorm 8): #K#C\Foo\Bar::get()%#K#C\Bar\Baz. -> \Bar\Baz.
    // Example (PhpStorm 9): #K#C\Foo\Bar::get()%#K#C\Bar\Baz.class -> \Bar\Baz.class
    String parameter = expression.substring(endIndex + 5);

    if (parameter.contains(".class")) { // for PhpStorm 9
        parameter = parameter.replace(".class", "");
    }

    if (parameter.contains(".")) {
        parameter = parameter.replace(".", "");
    }

    return PhpIndex.getInstance(project).getAnyByFQN(parameter);
}
 
Example #9
Source File: GeneralUtilityServiceTypeProvider.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@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 #10
Source File: PhpTypeProviderUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpTypeProviderUtil#getResolvedParameter
 */
public void testGetTypeSignature() {
    Function<PhpNamedElement, String> func = phpNamedElement ->
        phpNamedElement instanceof Method ? ((Method) phpNamedElement).getContainingClass().getFQN() : null;

    ArrayList<? extends PhpNamedElement> typeSignature = new ArrayList<PhpNamedElement>(PhpTypeProviderUtil.getTypeSignature(
        PhpIndex.getInstance(getProject()),
        "#M#C\\Doctrine\\Common\\Persistence\\ObjectManager.getRepository|#M#C\\Doctrine\\Common\\Persistence\\ObjectFoo.getRepository"
    ));

    assertContainsElements(ContainerUtil.map(typeSignature, func), "\\Doctrine\\Common\\Persistence\\ObjectManager", "\\Doctrine\\Common\\Persistence\\ObjectFoo");

    typeSignature = new ArrayList<PhpNamedElement>(PhpTypeProviderUtil.getTypeSignature(
        PhpIndex.getInstance(getProject()),
        "#M#C\\Doctrine\\Common\\Persistence\\ObjectManager.getRepository"
    ));
    assertContainsElements(ContainerUtil.map(typeSignature, func), "\\Doctrine\\Common\\Persistence\\ObjectManager");
}
 
Example #11
Source File: DocTagNameAnnotationReferenceContributor.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@Override
public boolean isReferenceTo(@NotNull PsiElement element) {

    // use Doctrine\ORM\Mapping as "ORM";
    if (element instanceof PhpUse) {
        String useName = ((PhpUse) element).getName();
        String docUseName = getDocBlockName();

        if(useName.equals(docUseName)) {
            return true;
        }

    }

    // eg for "Optimize Imports"
    // attach reference to @Template()
    // reference can also point to a namespace e.g. @Annotation\Exclude()
    if (element instanceof PhpNamedElement && !(element instanceof Variable)) {
        if(((PhpNamedElement) element).getName().equals(getDocBlockName())) {
            return true;
        }
    }

    return false;
}
 
Example #12
Source File: JsonRawContainerProvider.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@Nullable
@Override
public Collection<PhpNamedElement> resolveParameter(@NotNull PhpToolboxTypeProviderArguments args) {

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

    for (JsonRawLookupElement jsonRawLookupElement: args.getLookupElements()) {
        String type = jsonRawLookupElement.getType();
        if(type == null || StringUtils.isBlank(type)) {
            continue;
        }

        type = type.replaceAll("\\\\+", "\\\\");

        // internal fully fqn needed by converter since phpstorm9;
        // we normalize it on our side for a unique collection
        if(!type.startsWith("\\")) {
            type = "\\" + type;
        }

        if(args.getParameter().equals(jsonRawLookupElement.getLookupString())) {
            elements.addAll(PhpIndex.getInstance(args.getProject()).getAnyByFQN(type));
        }
    }

    return elements;
}
 
Example #13
Source File: ServiceToolboxProviderTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * @see ServiceToolboxProvider#resolveParameter
 */
public void testTypeResolve() {
    ServiceToolboxProvider provider = new ServiceToolboxProvider();
    Collection<PhpNamedElement> classes = provider.resolveParameter(
        new PhpToolboxTypeProviderArguments(getProject(), "foo_bar_foo_Bar", new ArrayList<>())
    );

    assertNotNull(classes);
    assertNotNull(ContainerUtil.find(classes, phpNamedElement ->
        phpNamedElement instanceof PhpClass && "FooBar".equals(phpNamedElement.getName()))
    );
}
 
Example #14
Source File: FluidTypeResolver.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public static Set<String> resolveFluidMethodName(Project project, Collection<String> previousElement, String typeName) {

        Set<String> types = new HashSet<>();
        for (String prevClass : previousElement) {
            for (PhpClass phpClass : getClassesInterface(project, prevClass)) {
                for (PhpNamedElement target : getFluidPhpNameTargets(phpClass, typeName)) {
                    types.addAll(target.getType().getTypes());
                }
            }
        }

        return types;
    }
 
Example #15
Source File: CodeInsightFixtureTestCase.java    From silex-idea-plugin with MIT License 5 votes vote down vote up
protected void assertTypeEquals(LanguageFileType languageFileType, @NotNull Class aClass, String configureByText, String phpClassType) {
    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);

    String types = "";

    for (String type : collection.iterator().next().getType().getTypes()) {
        Collection<? extends PhpNamedElement> col = phpIndex.getBySignature(type, null, 0);
        if (col.size() == 0) {
            continue;
        }

        for (String classType : col.iterator().next().getType().getTypes()) {
            types = types + classType + '|';
            if (classType.equals(phpClassType)) {
                return;
            }
        }
    }

    fail("Can't find type: "+phpClassType+", found:"+types);
}
 
Example #16
Source File: BxSuperglobalsProvider.java    From bxfs with MIT License 5 votes vote down vote up
@Override
public Collection<? extends PhpNamedElement> getBySignature(String expression, Project project) {
	for (BxSuperglobal superglobal : bxSuperglobals.values())
		if (superglobal.className.equals(expression)) {
			if (superglobal.className.equals("GLOBALS") || superglobal.className.equals("php_errormsg")) {
				return Arrays.asList(PhpPsiElementFactory.createVariable(project, superglobal.className, true));
			} else {
				return PhpIndex.getInstance(project).getClassesByFQN(expression);
			}
		}

	return Collections.emptySet();
}
 
Example #17
Source File: BaseLatteCompletionProvider.java    From intellij-latte with MIT License 5 votes vote down vote up
PhpLookupElement getPhpLookupElement(@NotNull PhpNamedElement phpNamedElement, @Nullable String searchedWord) {
	PhpLookupElement lookupItem = new PhpLookupElement(phpNamedElement) {
		@Override
		public Set<String> getAllLookupStrings() {
			Set<String> original = super.getAllLookupStrings();
			Set<String> strings = new HashSet<String>(original.size() + 1);
			strings.addAll(original);
			strings.add(searchedWord == null ? this.getNamedElement().getFQN() : searchedWord);
			return strings;
		}
	};
	return lookupItem;
}
 
Example #18
Source File: JsonRawContainerProvider.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@Nullable
@Override
public Collection<PhpNamedElement> resolveParameter(@NotNull PhpToolboxTypeProviderArguments args) {

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

    for (JsonRawLookupElement jsonRawLookupElement: args.getLookupElements()) {
        String type = jsonRawLookupElement.getType();
        if(type == null || StringUtils.isBlank(type)) {
            continue;
        }

        type = type.replaceAll("\\\\+", "\\\\");

        // internal fully fqn needed by converter since phpstorm9;
        // we normalize it on our side for a unique collection
        if(!type.startsWith("\\")) {
            type = "\\" + type;
        }

        if(args.getParameter().equals(jsonRawLookupElement.getLookupString())) {
            elements.addAll(PhpIndex.getInstance(args.getProject()).getAnyByFQN(type));
        }
    }

    return elements;
}
 
Example #19
Source File: PhpIndexAbstractProviderAbstract.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@Nullable
@Override
public Collection<PhpNamedElement> resolveParameter(@NotNull PhpToolboxTypeProviderArguments args) {
    String type = args.getParameter().replaceAll("\\\\+", "\\\\");

    return new HashSet<>(
        PhpIndex.getInstance(args.getProject()).getAnyByFQN(type)
    );
}
 
Example #20
Source File: TemplateAnnotationTypeProvider.java    From idea-php-generics-plugin with MIT License 5 votes vote down vote up
/**
 * Supports "@extends" and "@implements"
 *
 * - "@extends \Extended\Implementations\MyContainer<\Extended\Implementations\Foobar>"
 */
private void visitTemplateAnnotatedMethod(@NotNull Project project, @NotNull Collection<String> types, @NotNull String signature, @NotNull PhpNamedElement phpNamedElement, @NotNull Collection<TemplateAnnotationUsage> usages) {
    if (!(phpNamedElement instanceof Method)) {
        return;
    }

    for (TemplateAnnotationUsage usage : usages) {
        if (usage.getType() == TemplateAnnotationUsage.Type.METHOD_TEMPLATE) {
            // it class does not implement the method we got into "parent" class; here we get the orgin class name
            Matcher matcher = METHOD_CALL_SIGNATURE_MATCHER.matcher(signature);
            if (!matcher.find()) {
                continue;
            }

            // Find class "@extends" tag and the origin class
            String group = matcher.group(1);
            for (TemplateAnnotationUsage origin : getTemplateAnnotationUsagesMap(project, group)) {
                if (origin.getType() == TemplateAnnotationUsage.Type.EXTENDS) {
                    String context = origin.getContext();
                    if (context != null) {
                        String[] split = context.split("::");
                        if (split.length > 1) {
                            types.add("#" + this.getKey() + "#K#C" + split[1] + ".class");
                        }
                    }
                }
            }
        }
    }
}
 
Example #21
Source File: ObjectRepositoryResultTypeProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
private Collection<Method> getObjectRepositoryCall(Collection<? extends PhpNamedElement> phpNamedElements) {
    Collection<Method> methods = new HashSet<>();
    for (PhpNamedElement phpNamedElement: phpNamedElements) {
        if(phpNamedElement instanceof Method && PhpElementsUtil.isMethodInstanceOf((Method) phpNamedElement, FIND_SIGNATURES)) {
            methods.add((Method) phpNamedElement);
        }
    }

    return methods;
}
 
Example #22
Source File: PhpTypeSignatureTypes.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public Collection<? extends PhpNamedElement> getByParameter(Project project, String parameter) {

    ContainerService containerService = ContainerCollectionResolver.getService(project, parameter);
    if(containerService != null) {
        String serviceClass = containerService.getClassName();
        if(serviceClass != null) {
            return PhpIndex.getInstance(project).getAnyByFQN(serviceClass);
        }
    }

    return null;
}
 
Example #23
Source File: PhpTwigMethodLookupElement.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void renderElement(LookupElementPresentation presentation) {
    super.renderElement(presentation);

    PhpNamedElement phpNamedElement = this.getNamedElement();

    // reset method to show full name again, which was stripped inside getLookupString
    if(phpNamedElement instanceof Method && TwigTypeResolveUtil.isPropertyShortcutMethod((Method) phpNamedElement)) {
        presentation.setItemText(phpNamedElement.getName());
    }

}
 
Example #24
Source File: TwigExtensionLookupElement.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void buildTailText(@NotNull LookupElementPresentation presentation) {
    if(this.twigExtension.getTwigExtensionType() == TwigExtensionParser.TwigExtensionType.SIMPLE_TEST) {
        return;
    }

    String signature = this.twigExtension.getSignature();
    if(signature == null) {
        return;
    }

    Collection<? extends PhpNamedElement> phpNamedElements = PhpIndex.getInstance(this.project).getBySignature(signature);
    if(phpNamedElements.size() == 0) {
        return;
    }

    PhpNamedElement function = phpNamedElements.iterator().next();
    if(function instanceof Function) {
        List<Parameter> parameters = new LinkedList<>(Arrays.asList(((Function) function).getParameters()));

        if(this.twigExtension.getOption("needs_context") != null && parameters.size() > 0) {
            parameters.remove(0);
        }

        if(this.twigExtension.getOption("needs_environment") != null && parameters.size() > 0) {
            parameters.remove(0);
        }

        presentation.setTailText(PhpPresentationUtil.formatParameters(null, parameters.toArray(new Parameter[parameters.size()])).toString(), true);
    }
}
 
Example #25
Source File: TwigVariablePathInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void visit(@NotNull PsiElement element) {
    Collection<String> beforeLeaf = TwigTypeResolveUtil.formatPsiTypeName(element);
    if(beforeLeaf.size() == 0) {
        return;
    }

    Collection<TwigTypeContainer> types = TwigTypeResolveUtil.resolveTwigMethodName(element, beforeLeaf);
    if(types.size() == 0) {
        return;
    }

    for(TwigTypeContainer twigTypeContainer: types) {
        PhpNamedElement phpNamedElement = twigTypeContainer.getPhpNamedElement();
        if(phpNamedElement == null) {
            continue;
        }

        if(isWeakPhpClass(phpNamedElement)) {
            return;
        }

        String text = element.getText();
        if(TwigTypeResolveUtil.getTwigPhpNameTargets(phpNamedElement, text).size() > 0) {
            return;
        }
    }

    this.holder.registerProblem(element, "Field or method not found", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
 
Example #26
Source File: PhpTypeProviderUtilTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * @see PhpTypeProviderUtil#mergeSignatureResults
 */
public void testMergeSignatureResults() {

    Collection<PhpNamedElement> phpNamedElements = new ArrayList<>();
    phpNamedElements.add(PhpElementsUtil.getClassMethod(getProject(), "PhpType\\Bar", "foo"));
    phpNamedElements.add(PhpElementsUtil.getClassMethod(getProject(), "PhpType\\Bar", "bar"));
    phpNamedElements.add(PhpElementsUtil.getClassMethod(getProject(), "PhpType\\Bar", "car"));

    Collection<? extends PhpNamedElement> elements = PhpTypeProviderUtil.mergeSignatureResults(phpNamedElements, PhpElementsUtil.getClass(getProject(), "\\PhpType\\Foo"));
    assertEquals(2, elements.size());
}
 
Example #27
Source File: TwigTypeResolveUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private static Collection<TwigTypeContainer> resolveTwigMethodName(Collection<TwigTypeContainer> previousElement, String typeName, Collection<List<TwigTypeContainer>> twigTypeContainer) {

        ArrayList<TwigTypeContainer> phpNamedElements = new ArrayList<>();

        for(TwigTypeContainer phpNamedElement: previousElement) {

            if(phpNamedElement.getPhpNamedElement() != null) {
                for(PhpNamedElement target : getTwigPhpNameTargets(phpNamedElement.getPhpNamedElement(), typeName)) {
                    PhpType phpType = target.getType();

                    // @TODO: provide extension
                    // custom resolving for Twig here: "app.user" => can also be a general solution just support the "getToken()->getUser()"
                    if (target instanceof Method && StaticVariableCollector.isUserMethod((Method) target)) {
                        phpNamedElements.addAll(getApplicationUserImplementations(target.getProject()));
                    }

                    // @TODO: use full resolving for object, that would allow using TypeProviders and core PhpStorm feature
                    for (String typeString: phpType.filterPrimitives().getTypes()) {
                        PhpClass phpClass = PhpElementsUtil.getClassInterface(phpNamedElement.getPhpNamedElement().getProject(), typeString);
                        if(phpClass != null) {
                            phpNamedElements.add(new TwigTypeContainer(phpClass));
                        }
                    }
                }
            }

            for(TwigTypeResolver twigTypeResolver: TWIG_TYPE_RESOLVERS) {
                twigTypeResolver.resolve(phpNamedElements, previousElement, typeName, twigTypeContainer, null);
            }

        }

        return phpNamedElements;
    }
 
Example #28
Source File: TwigTypeResolveUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private static Set<String> resolveTwigMethodName(Project project, Collection<String> previousElement, String typeName) {

        Set<String> types = new HashSet<>();

        for(String prevClass: previousElement) {
            for (PhpClass phpClass : PhpElementsUtil.getClassesInterface(project, prevClass)) {
                for(PhpNamedElement target : getTwigPhpNameTargets(phpClass, typeName)) {
                    types.addAll(target.getType().getTypes());
                }
            }
        }

        return types;
    }
 
Example #29
Source File: ServiceToolboxProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public Collection<PhpNamedElement> resolveParameter(@NotNull PhpToolboxTypeProviderArguments arguments) {
    PhpClass serviceClass = ServiceUtil.getServiceClass(arguments.getProject(), arguments.getParameter());
    if(serviceClass == null) {
        return null;
    }

    return new ArrayList<PhpNamedElement>(){{
        add(serviceClass);
    }};
}
 
Example #30
Source File: PhpConstGotoCompletionProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void addAllClassConstants(Collection<LookupElement> elements, Collection<PhpClass> classes) {
    for (PhpClass phpClass : classes) {
        // All class constants
        List<Field> fields = Arrays.stream(phpClass.getOwnFields()).filter(Field::isConstant).collect(Collectors.toList());
        for (PhpNamedElement field : fields) {
            // Foo::BAR
            String lookupString = phpClass.getName() + SCOPE_OPERATOR + field.getName();
            elements.add(wrapClassConstInsertHandler(new MyPhpLookupElement(field, lookupString)));
        }
    }
}