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

The following examples show how to use com.jetbrains.php.lang.psi.elements.Parameter. 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: YamlHelperLightTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#visitServiceCallArgumentMethodIndex
 */
public void testVisitServiceCallArgumentMethodIndexForNamedServices() {
    myFixture.configureByText(YAMLFileType.YML, "services:\n" +
        "    Foo\\Bar:\n" +
        "       calls:\n" +
        "           - [ 'setBar', ['@foo', @f<caret>oo] ]\n"
    );

    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    YAMLScalar parent = (YAMLScalar) psiElement.getParent();

    Collection<Parameter> parameters = new ArrayList<>();
    YamlHelper.visitServiceCallArgumentMethodIndex(parent, parameters::add);

    assertNotNull(ContainerUtil.find(parameters, parameter -> "arg2".equals(parameter.getName())));
}
 
Example #2
Source File: YamlHelperLightTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#visitServiceCallArgumentMethodIndex
 */
public void testVisitServiceCallArgumentMethodIndex() {
    myFixture.configureByText(YAMLFileType.YML, "services:\n" +
        "    foobar:\n" +
        "       class: Foo\\Bar\n" +
        "       calls:\n" +
        "           - [ 'setBar', [@f<caret>oo] ]\n"
    );

    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    YAMLScalar parent = (YAMLScalar) psiElement.getParent();

    Collection<Parameter> parameters = new ArrayList<>();
    YamlHelper.visitServiceCallArgumentMethodIndex(parent, parameters::add);

    assertNotNull(ContainerUtil.find(parameters, parameter -> "arg1".equals(parameter.getName())));
}
 
Example #3
Source File: ParameterResolverConsumer.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
public void consume(ParameterVisitor parameter) {
    PhpClass serviceClass = ServiceUtil.getResolvedClassDefinition(
        this.project,
        parameter.getClassName(),
        new ContainerCollectionResolver.LazyServiceCollector(this.project)
    );

    if(serviceClass == null) {
        return;
    }

    Method method = serviceClass.findMethodByName(parameter.getMethod());
    if (method == null) {
        return;
    }

    Parameter[] methodParameter = method.getParameters();
    if(parameter.getParameterIndex() >= methodParameter.length) {
        return;
    }

    consumer.consume(methodParameter[parameter.getParameterIndex()]);
}
 
Example #4
Source File: XmlServiceInstanceInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private void attachMethodInstances(@NotNull PsiElement target, @NotNull String serviceName, @NotNull Method method, int parameterIndex, @NotNull ProblemsHolder holder) {
    Parameter[] constructorParameter = method.getParameters();
    if(parameterIndex >= constructorParameter.length) {
        return;
    }

    String className = constructorParameter[parameterIndex].getDeclaredType().toString();
    PhpClass expectedClass = PhpElementsUtil.getClassInterface(method.getProject(), className);
    if(expectedClass == null) {
        return;
    }

    PhpClass serviceParameterClass = ServiceUtil.getResolvedClassDefinition(method.getProject(), serviceName);
    if(serviceParameterClass != null && !PhpElementsUtil.isInstanceOf(serviceParameterClass, expectedClass)) {
        holder.registerProblem(
            target,
            "Expect instance of: " + expectedClass.getPresentableFQN(),
            ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
            new XmlServiceSuggestIntentionAction(expectedClass.getFQN(), target)
        );
    }
}
 
Example #5
Source File: XmlHelperTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see XmlHelper#visitServiceCallArgumentMethodIndex
 */
public void testVisitServiceCallArgumentParameter() {
    myFixture.configureByText(XmlFileType.INSTANCE, "" +
        "<service class=\"Foo\\Bar\">\n" +
        "   <call method=\"setBar\">\n" +
        "      <argument/>\n" +
        "      <argument type=\"service\" id=\"ma<caret>iler\" />\n" +
        "   </call>\n" +
        "</service>"
    );

    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    PsiElement parent = psiElement.getParent();

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

    XmlHelper.visitServiceCallArgumentMethodIndex((XmlAttributeValue) parent, results::add);

    assertNotNull(
        ContainerUtil.find(results, parameter -> "arg2".equals(parameter.getName()))
    );
}
 
Example #6
Source File: YamlGoToDeclarationHandlerTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public void testNamedArgumentsNavigationForDefaultBinding() {
    assertNavigationMatch("services.yml", "" +
                    "services:\n" +
                    "   _defaults:\n" +
                    "       bind:\n" +
                    "           $<caret>i: ~\n"+
                    "   Foo\\Bar: ~" +
            PlatformPatterns.psiElement(Parameter.class)
    );

    assertNavigationMatch("services.yml", "" +
            "services:\n" +
            "   _defaults:\n" +
            "       bind:\n" +
            "           $<caret>i: ~\n"+
            "   foobar:\n" +
            "       class: Foo\\Bar\n" +
            PlatformPatterns.psiElement(Parameter.class)
    );
}
 
Example #7
Source File: ServiceArgumentParameterHintsProvider.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
private String createTypeHintFromParameter(@NotNull Project project, Parameter parameter) {
    String className = parameter.getDeclaredType().toString();
    if(PhpType.isNotExtendablePrimitiveType(className)) {
        return parameter.getName();
    }

    int i = className.lastIndexOf("\\");
    if(i > 0) {
        return className.substring(i + 1);
    }

    PhpClass expectedClass = PhpElementsUtil.getClassInterface(project, className);
    if(expectedClass != null) {
        return expectedClass.getName();
    }

    return parameter.getName();
}
 
Example #8
Source File: TypeHintSuggestionProvider.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@Nullable
@Override
public SuggestedNameInfo getSuggestedNames(PsiElement psiElement, PsiElement psiElement1, Set<String> set) {

    if(!(psiElement instanceof Parameter)) {
        return null;
    }

    List<String> filter = ContainerUtil.filter(PhpNameSuggestionUtil.variableNameByType((Parameter) psiElement, psiElement.getProject(), false), s -> !StringUtil.containsChar(s, '\\'));

    if(filter.size() == 0) {
        return null;
    }

    for (String item : filter) {

        for(String end: TRIM_STRINGS) {

            // ending
            if(item.toLowerCase().endsWith(end)) {
                item = item.substring(0, item.length() - end.length());
            }

            // remove starting
            if(item.toLowerCase().startsWith(end)) {
                item = WordUtils.uncapitalize(item.substring(end.length()));
            }
        }

        if(StringUtils.isNotBlank(item)) {
            set.add(item);
        }
    }

    return null;
}
 
Example #9
Source File: ServiceBuilderTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
private List<MethodParameter.MethodModelParameter> getMethodModelParameters() {
    PhpClass anyByFQN = PhpIndex.getInstance(getProject()).getAnyByFQN("\\Foo\\Bar").iterator().next();

    Method constructor = anyByFQN.getConstructor();
    assertNotNull(constructor);

    Parameter parameter = constructor.getParameters()[0];

    return Collections.singletonList(
        new MethodParameter.MethodModelParameter(constructor, parameter, 0, new HashSet<>(Collections.singletonList("foobar")), "foobar")
    );
}
 
Example #10
Source File: YamlGoToDeclarationHandlerTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void testNamedArgumentsNavigationForService() {
    assertNavigationMatch("services.yml", "" +
                    "services:\n" +
                    "    Foo\\Bar:\n" +
                    "       arguments:\n" +
                    "           $<caret>i: ~\n",
            PlatformPatterns.psiElement(Parameter.class)
    );
}
 
Example #11
Source File: ServiceActionUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
public static List<String> getXmlMissingArgumentTypes(@NotNull XmlTag xmlTag, boolean collectOptionalParameter, @NotNull ContainerCollectionResolver.LazyServiceCollector collector) {
    PhpClass resolvedClassDefinition = getPhpClassFromXmlTag(xmlTag, collector);
    if (resolvedClassDefinition == null) {
        return Collections.emptyList();
    }

    Method constructor = resolvedClassDefinition.getConstructor();
    if(constructor == null) {
        return Collections.emptyList();
    }

    int serviceArguments = 0;

    for (XmlTag tag : xmlTag.getSubTags()) {
        if("argument".equals(tag.getName())) {
            serviceArguments++;
        }
    }

    Parameter[] parameters = collectOptionalParameter ? constructor.getParameters() : PhpElementsUtil.getFunctionRequiredParameter(constructor);
    if(parameters.length <= serviceArguments) {
        return Collections.emptyList();
    }

    final List<String> args = new ArrayList<>();

    for (int i = serviceArguments; i < parameters.length; i++) {
        Parameter parameter = parameters[i];
        String s = parameter.getDeclaredType().toString();
        args.add(s);
    }

    return args;
}
 
Example #12
Source File: TwigTypeInsertHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private boolean needParameter(Method method) {

        for(Parameter parameter: method.getParameters()) {
            if(!parameter.isOptional()) {
                return true;
            }
        }

        return false;
    }
 
Example #13
Source File: YamlGoToDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private Collection<? extends PsiElement> namedArgumentGoto(@NotNull PsiElement psiElement) {
    Collection<PsiElement> psiElements = new HashSet<>();

    Parameter yamlNamedArgument = ServiceContainerUtil.getYamlNamedArgument(psiElement, new ContainerCollectionResolver.LazyServiceCollector(psiElement.getProject()));
    if (yamlNamedArgument != null) {
        psiElements.add(yamlNamedArgument);
    }

    return psiElements;
}
 
Example #14
Source File: ContainerBuilderStubIndex.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void processMethod(@NotNull Method method, @NotNull Map<String, ContainerBuilderCall> map) {
    Set<CharSequence> containerParameters = StreamEx.of(method.getParameters())
        .filter(ContainerBuilderStubIndex::isContainerParam)
        .map(Parameter::getNameCS)
        .toSet();
    if (containerParameters.isEmpty()) return;
    MyInstructionProcessor processor = new MyInstructionProcessor(map, containerParameters);
    for (PhpInstruction instruction : method.getControlFlow().getInstructions()) {
        instruction.process(processor);
    }
}
 
Example #15
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 #16
Source File: ServiceArgumentParameterHintsProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
private String attachMethodInstances(@NotNull Function function, int parameterIndex) {
    Parameter[] constructorParameter = function.getParameters();
    if(parameterIndex >= constructorParameter.length) {
        return null;
    }

    Parameter parameter = constructorParameter[parameterIndex];

    return createTypeHintFromParameter(function.getProject(), parameter);
}
 
Example #17
Source File: YamlXmlServiceInstanceInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
static void registerInstanceProblem(@NotNull PsiElement psiElement, @NotNull ProblemsHolder holder, int parameterIndex, @NotNull Method constructor, @NotNull ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector) {
    String serviceName = getServiceName(psiElement);
    if(StringUtils.isBlank(serviceName)) {
        return;
    }

    PhpClass serviceParameterClass = ServiceUtil.getResolvedClassDefinition(psiElement.getProject(), getServiceName(psiElement), lazyServiceCollector);
    if(serviceParameterClass == null) {
        return;
    }

    Parameter[] constructorParameter = constructor.getParameters();
    if(parameterIndex >= constructorParameter.length) {
        return;
    }

    PhpClass expectedClass = PhpElementsUtil.getClassInterface(psiElement.getProject(), constructorParameter[parameterIndex].getDeclaredType().toString());
    if(expectedClass == null) {
        return;
    }

    if(!PhpElementsUtil.isInstanceOf(serviceParameterClass, expectedClass)) {
        holder.registerProblem(
            psiElement,
            "Expect instance of: " + expectedClass.getPresentableFQN(),
            new YamlSuggestIntentionAction(expectedClass.getFQN(), psiElement)
        );
    }
}
 
Example #18
Source File: TypeHintSuggestionProvider.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@Nullable
@Override
public SuggestedNameInfo getSuggestedNames(PsiElement psiElement, PsiElement psiElement1, Set<String> set) {

    if(!(psiElement instanceof Parameter)) {
        return null;
    }

    List<String> filter = ContainerUtil.filter(PhpNameSuggestionUtil.variableNameByType((Parameter) psiElement, psiElement.getProject(), false), s -> !StringUtil.containsChar(s, '\\'));

    if(filter.size() == 0) {
        return null;
    }

    for (String item : filter) {

        for(String end: TRIM_STRINGS) {

            // ending
            if(item.toLowerCase().endsWith(end)) {
                item = item.substring(0, item.length() - end.length());
            }

            // remove starting
            if(item.toLowerCase().startsWith(end)) {
                item = WordUtils.uncapitalize(item.substring(end.length()));
            }
        }

        if(StringUtils.isNotBlank(item)) {
            set.add(item);
        }
    }

    return null;
}
 
Example #19
Source File: FluidTypeInsertHandler.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
private boolean needParameter(Method method) {

        for (Parameter parameter : method.getParameters()) {
            if (!parameter.isOptional()) {
                return true;
            }
        }

        return false;
    }
 
Example #20
Source File: ContainerBuilderStubIndex.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
private static boolean isContainerParam(@NotNull Parameter parameter) {
    String parameterType = parameter.getDeclaredType().toString();
    return SET.contains(StringUtils.stripStart(parameterType, "\\"));
}
 
Example #21
Source File: TwigExtensionInsertHandler.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement, @NotNull TwigExtension twigExtension) {
    // {{ form_javasc|() }}
    // {{ form_javasc| }}
    if(PhpInsertHandlerUtil.isStringAtCaret(context.getEditor(), "(")) {
        return;
    }

    FunctionInsertHandler.getInstance().handleInsert(context, lookupElement);

    // if first parameter is a string type; add quotes
    for (PsiElement psiElement : PhpElementsUtil.getPsiElementsBySignature(context.getProject(), twigExtension.getSignature())) {
        if(!(psiElement instanceof Function)) {
            continue;
        }

        Parameter[] parameters = ((Function) psiElement).getParameters();

        // skip Twig parameter, we need first function parameter
        int parameter = 0;
        if(twigExtension.getOption("needs_context") != null) {
            parameter++;
        }

        if(twigExtension.getOption("needs_environment") != null) {
            parameter++;
        }

        if(parameters.length <= parameter) {
            continue;
        }

        if(!isString(parameters[parameter].getType())) {
            continue;
        }

        // wrap caret with '' so we have foobar('<caret>')
        PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), "''");
        context.getEditor().getCaretModel().moveCaretRelatively(-1, 0, false, false, true);

        return;
    }
}
 
Example #22
Source File: ServiceActionUtil.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@NotNull
public static List<String> getYamlMissingArgumentTypes(Project project, ServiceActionUtil.ServiceYamlContainer container, boolean collectOptionalParameter, @NotNull ContainerCollectionResolver.LazyServiceCollector collector) {
    PhpClass resolvedClassDefinition = ServiceUtil.getResolvedClassDefinition(project, container.getClassName(), collector);
    if(resolvedClassDefinition == null) {
        return Collections.emptyList();
    }

    Method constructor = resolvedClassDefinition.getConstructor();
    if(constructor == null) {
        return Collections.emptyList();
    }

    int serviceArguments = -1;
    if(container.getArgument() != null) {
        PsiElement yamlCompoundValue = container.getArgument().getValue();

        if(yamlCompoundValue instanceof YAMLCompoundValue) {
            List<PsiElement> yamlArrayOnSequenceOrArrayElements = YamlHelper.getYamlArrayOnSequenceOrArrayElements((YAMLCompoundValue) yamlCompoundValue);
            if(yamlArrayOnSequenceOrArrayElements != null) {
                serviceArguments = yamlArrayOnSequenceOrArrayElements.size();
            }
        }

    } else {
        serviceArguments = 0;
    }

    if(serviceArguments == -1) {
        return Collections.emptyList();
    }

    Parameter[] parameters = collectOptionalParameter ? constructor.getParameters() : PhpElementsUtil.getFunctionRequiredParameter(constructor);
    if(parameters.length <= serviceArguments) {
        return Collections.emptyList();
    }

    final List<String> args = new ArrayList<>();

    for (int i = serviceArguments; i < parameters.length; i++) {
        Parameter parameter = parameters[i];
        String s = parameter.getDeclaredType().toString();
        args.add(s);
    }

    return args;
}
 
Example #23
Source File: MethodParameter.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public MethodModelParameter(Method method, Parameter parameter, int index, Set<String> possibleServices, String currentService) {
    this(method, parameter, index, possibleServices);
    this.isPossibleService = true;
    this.currentService = currentService;
}
 
Example #24
Source File: MethodParameter.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public MethodModelParameter(Method method, Parameter parameter, int index, Set<String> possibleServices) {
    this.method = method;
    this.index = index;
    this.parameter = parameter;
    this.possibleServices = possibleServices;
}
 
Example #25
Source File: MethodParameter.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public Parameter getParameter() {
    return this.parameter;
}
 
Example #26
Source File: ParameterResolverConsumer.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public ParameterResolverConsumer(@NotNull Project project, @NotNull Consumer<Parameter> consumer) {
    this.project = project;
    this.consumer = consumer;
}
 
Example #27
Source File: XmlHelper.java    From idea-php-symfony2-plugin with MIT License 2 votes vote down vote up
/**
 * Consumer for method parameter match
 *
 * service_name:
 *   class: FOOBAR
 *   calls:
 *      - [onFoobar, [@fo<caret>o]]
 */
public static void visitServiceCallArgumentMethodIndex(@NotNull XmlAttributeValue xmlAttribute, @NotNull Consumer<Parameter> consumer) {
    visitServiceCallArgument(xmlAttribute, new ParameterResolverConsumer(xmlAttribute.getProject(), consumer));
}
 
Example #28
Source File: YamlHelper.java    From idea-php-symfony2-plugin with MIT License 2 votes vote down vote up
/**
 * Consumer for method parameter match
 *
 * service_name:
 *   class: FOOBAR
 *   calls:
 *      - [onFoobar, [@fo<caret>o]]
 */
public static void visitServiceCallArgumentMethodIndex(@NotNull YAMLScalar yamlScalar, @NotNull Consumer<Parameter> consumer) {
    YamlHelper.visitServiceCallArgument(yamlScalar, new ParameterResolverConsumer(yamlScalar.getProject(), consumer));
}