<commit_before>package io.mangoo.templating.freemarker.methods;

import java.util.List;
import java.util.regex.Matcher;

import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import io.mangoo.routing.Route;
import io.mangoo.routing.Router;
import io.mangoo.utils.TemplateUtils;

/**
 *
 * @author svenkubiak
 *
 */
@SuppressWarnings("rawtypes")
public class RouteMethod implements TemplateMethodModelEx {
    private static final int MIN_ARGUMENTS = 1;

    @Override
    public TemplateModel exec(List arguments) throws TemplateModelException {
        String url = "";
        if (arguments.size() >= MIN_ARGUMENTS) {
            String controller = ((SimpleScalar) arguments.get(0)).getAsString();
            Route route = Router.getReverseRoute(controller);
            if (route != null) {
                url = route.getUrl();
                Matcher matcher = TemplateUtils.PARAMETER_PATTERN.matcher(url);
                int i = 1;
                while (matcher.find()) {
                    String argument = ((SimpleScalar) arguments.get(i)).getAsString();
                    url = url.replace("{" + matcher.group(1) + "}", argument); 
                    i++;
                }
            } else {
                throw new TemplateModelException("Reverse route for " + controller + " could not be found!");
            }
        } else {
            throw new TemplateModelException("Missing at least one argument (ControllerClass:ControllerMethod) for reverse routing!");
        }

        return new SimpleScalar(url);
    }
}<commit_msg>sonar refactorings<commit_after>package io.mangoo.templating.freemarker.methods;

import java.util.List;
import java.util.regex.Matcher;

import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import io.mangoo.routing.Route;
import io.mangoo.routing.Router;
import io.mangoo.utils.TemplateUtils;

/**
 *
 * @author svenkubiak
 *
 */
@SuppressWarnings("rawtypes")
public class RouteMethod implements TemplateMethodModelEx {
    private static final int MIN_ARGUMENTS = 1;

    @Override
    public TemplateModel exec(List arguments) throws TemplateModelException {
        String url;
        if (arguments.size() >= MIN_ARGUMENTS) {
            String controller = ((SimpleScalar) arguments.get(0)).getAsString();
            Route route = Router.getReverseRoute(controller);
            if (route != null) {
                url = route.getUrl();
                Matcher matcher = TemplateUtils.PARAMETER_PATTERN.matcher(url);
                int i = 1;
                while (matcher.find()) {
                    String argument = ((SimpleScalar) arguments.get(i)).getAsString();
                    url = url.replace("{" + matcher.group(1) + "}", argument); 
                    i++;
                }
            } else {
                throw new TemplateModelException("Reverse route for " + controller + " could not be found!");
            }
        } else {
            throw new TemplateModelException("Missing at least one argument (ControllerClass:ControllerMethod) for reverse routing!");
        }

        return new SimpleScalar(url);
    }
}
{'fraction_non_alphanumeric': 0.10343805572021339, 'fraction_numerical': 0.0023710729104919974, 'mean_word_length': 2.57521186440678, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5399845', 'n_tokens_mistral': 910, 'n_tokens_neox': 882, 'n_words': 203}