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

The following examples show how to use com.jetbrains.php.lang.psi.elements.Method#getParameters() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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;
}