<commit_before><?php
/**
 *
 */

namespace Mvc5\Service;

use Mvc5\Exception;

final class Builder
    extends \ReflectionClass
{
    /**
     * @var array|self[]
     */
    protected static $class = [];

    /**
     * @var \ReflectionMethod
     */
    protected $constructor;

    /**
     * @var \ReflectionParameter[]
     */
    protected $params;

    /**
     * @param string $name
     * @param array $args
     * @param callable $callback
     * @return object
     */
    static function create(string $name, array $args, callable $callback)
    {
        $class = static::reflectionClass($name);

        if (!$class->constructor()) {
            return $class->newInstanceWithoutConstructor();
        }

        if ($args && !is_string(key($args))) {
            return new $name(...$args);
        }

        $matched = [];
        $params  = $class->params();

        foreach($params as $param) {
            if (array_key_exists($param->name, $args)) {
                $matched[] = $args[$param->name];
                continue;
            }

            if ($param->isOptional()) {
                $param->isDefaultValueAvailable() &&
                    $matched[] = $param->getDefaultValue();
                continue;
            }

            if (null !== ($hint = $param->getClass()) && null !== $match = $callback($hint->name)) {
                $matched[] = $match;
                continue;
            }

            if (null !== $match = $callback($param->name)) {
                $matched[] = $match;
                continue;
            }

            Exception::runtime('Missing required parameter $' . $param->name . ' for ' . $name);
        }<commit_msg>variadic constructor with named args<commit_after><?php
/**
 *
 */

namespace Mvc5\Service;

use Mvc5\Exception;

final class Builder
    extends \ReflectionClass
{
    /**
     * @var array|self[]
     */
    protected static $class = [];

    /**
     * @var \ReflectionMethod
     */
    protected $constructor;

    /**
     * @var \ReflectionParameter[]
     */
    protected $params;

    /**
     * @param string $name
     * @param array $args
     * @param callable $callback
     * @return object
     */
    static function create(string $name, array $args, callable $callback)
    {
        $class = static::reflectionClass($name);

        if (!$class->constructor()) {
            return $class->newInstanceWithoutConstructor();
        }

        if ($args && !is_string(key($args))) {
            return new $name(...$args);
        }

        $matched = [];
        $params  = $class->params();

        foreach($params as $param) {
            if ($param->isVariadic()) {
                $matched = array_merge($matched, array_values($args));
                break;
            }

            if (array_key_exists($param->name, $args)) {
                $matched[] = $args[$param->name];
                unset($args[$param->name]);
                continue;
            }

            if ($param->isOptional()) {
                $param->isDefaultValueAvailable() &&
                    $matched[] = $param->getDefaultValue();
                continue;
            }

            if (null !== ($hint = $param->getClass()) && null !== $match = $callback($hint->name)) {
                $matched[] = $match;
                continue;
            }

            if (null !== $match = $callback($param->name)) {
                $matched[] = $match;
                continue;
            }

            Exception::runtime('Missing required parameter $' . $param->name . ' for ' . $name);
        }
{'fraction_non_alphanumeric': 0.15630205441421433, 'fraction_numerical': 0.001110494169905608, 'mean_word_length': 1.5920863309352518, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '6309081', 'n_tokens_mistral': 985, 'n_tokens_neox': 948, 'n_words': 201}